问题描述
我有这个猫鼬模式:
I have this mongoose schema:
var listingSchema = new Schema({
street : String,
buildingNumber : Number,
apartmentNumber : Number,
UsersAndQuestions: [{
userID: String,
questionID: [String]
}]
});
我只想用到UsersAndQuestions
的新条目来更新它,该条目将由userID
和questionID
组成,但userID
是String
,但questionID
也是String
.插入数组).
And I just want to update it with a new entry to UsersAndQuestions
which will consist of a userID
which is a String
, and a questionID
which is also a String
(but needs to be inserted into an array).
我正在使用以下PUT
请求:
I am using this PUT
request:
app.put('/api/listing/:street/:buildingNumber/:apartmentNumber/addUserInput/:userid/:listingid/:questionid')
所以我手边有所有必要的参数.
So I have all the necessary parameters in hand.
通常,当我想更新架构中的字段时,我使用了我编写的以下代码:
Usually, when I wanted to update a field in a schema I used this code that I wrote:
app.put('/api/listing/:street/:buildingNumber/:apartmentNumber/addReportedUser/:userid/:listingid', function (req, res) {
var listingToUpdate = req.params.listingid;
var idToAdd = req.params.userid;
Listing.update({_id: ObjectId(listingToUpdate)},
{$addToSet: {reportedUsersIDs: ObjectId(idToAdd)}}
, function (err) {
if (err) {
res.send("There was a problem adding the reportedUserID to the listing" + err);
}
else {
console.log("Success adding reportedUserID to listing!");
}
})
});
您可以看到我使用了$addToSet
,并且效果很好.但是现在我想向数组中的字段添加两个参数.我考虑过要做这样的事情:
You can see I used $addToSet
and it worked well. But now I want to add two parameters to a field which is an array. I thought about doing something like this:
app.put('/api/listing/:street/:buildingNumber/:apartmentNumber/addUserInput/:userid/:listingid/:questionid', function(req,res){
var listingToUpdate = req.params.listingid;
var idToAdd = req.params.userid;
var questionToAdd = req.params.questionid;
Listing.update({_id: ObjectId(listingToUpdate)},
{$addToSet: {UsersAndQuestions.userID : ObjectId(idToAdd), UsersAndQuestions.questionID : ObjectId(questionToAdd)}}
, function (err) {
if (err) {
res.send("There was a problem adding the user and question to the listing" + err);
}
else{
console.log("Success adding user and question to the listing!");
}
})
});
但是我显然得到了SyntaxError
.
But I'm obviously getting a SyntaxError
.
做我尝试做的正确语法是什么?
What is the correct syntax for doing what I tried to do?
非常感谢! :)
推荐答案
您需要添加对象来设置UsersAndQuestions:
You need to add object to set UsersAndQuestions:
{$addToSet: {UsersAndQuestions: { userID: idToAdd, questionID: questionToAdd } }}
更新.
我会通过两个查询来做到这一点:
I would do it with two queries:
Listing.update({_id: ObjectId(listingToUpdate), 'UsersAndQuestions.userID': idToAdd},
{"$addToSet": {"UsersAndQuestions.$.questionID": questionToAdd}}
, function (err, result) {
if(result.n === 0){
//we haven't found document with the userId - idToAdd
//we need to insert to UsersAndQuestions document with this user
Listing.update({_id: ObjectId(listingToUpdate)},
{$addToSet: {UsersAndQuestions: { userID: idToAdd, questionID: questionToAdd } }},
function(err, res){
})
}
})
这篇关于Mongo $ addToSet与多个值正确的语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!