Mongo附加到现有密钥($ addToSet)

时间:2015-12-17 18:50:20

标签: javascript json node.js mongodb mongoose

我有这个猫鼬模式:

var listingSchema = new Schema({
        street          : String,
        buildingNumber  : Number,
        apartmentNumber : Number,
        UsersAndQuestions: [{
            userID: { type: String, unique: true},
            questionID: [String],
            _id : false
    }],
});

我只想更新一个UsersAndQuestions的新条目,该条目包含一个userID String和一个questionID,它也是一个String PUT(但需要插入数组中)。

我正在使用此app.put('/api/listing/:street/:buildingNumber/:apartmentNumber/addUserInput/:userid/:listingid/:questionid') 请求:

app.put('/api/listing/:street/:buildingNumber/:apartmentNumber/addUserInput/:userid/:listingid/:questionid', function (req, res) {
    console.log("adding user and question he answered to listing schema");
    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), questionID: ObjectId(questionToAdd)}}}
        , function (err, listing) {
            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!");
                res.send("Success!");
            }
        })
});

所以我掌握了所有必要的参数。

通常,当我想更新模式中的字段时,我使用了我编写的代码:

userID

这可以防止相同UsersAndQuestions条目的完全重复。

我的问题是:如果questionID 存在,我希望追加 userID那个questionID { "_id": "567300e694745bfce5bf6ed2", "street": "Rines", "buildingNumber": 1, "apartmentNumber": 1, "UsersAndQuestions": [ { "userID": "564b343fbc2d4310156c6bf9", "questionID": [ "5661db59b0b1b91c1d63643d" ] } ] } 数组。怎么办呢?

示例:

到目前为止,我的JSON是这样的:

PUT

我执行的userID使用相同的listingID,相同questionID,但不同 { "_id": "567300e694745bfce5bf6ed2", "street": "Rines", "buildingNumber": 1, "apartmentNumber": 1, "UsersAndQuestions": [ { "userID": "564b343fbc2d4310156c6bf9", "questionID": [ "5661db59b0b1b91c1d63643d" ] }, { "userID": "564b343fbc2d4310156c6bf9", "questionID": [ "5669d90a058ceddc158e97e2" ] } ] } 我得到了:

{
  "_id": "567300e694745bfce5bf6ed2",
  "street": "Rines",
  "buildingNumber": 1,
  "apartmentNumber": 1,
  "UsersAndQuestions": [
    {
      "userID": "564b343fbc2d4310156c6bf9",
      "questionID": [
        "5661db59b0b1b91c1d63643d", "5669d90a058ceddc158e97e2"
      ]
    }
  ]
}

想要得到的是:

OnSignedIn

做我想做的事的正确语法是什么?

非常感谢! :)

1 个答案:

答案 0 :(得分:0)

为了完整起见,我将在此处添加答案。

您需要添加对象以设置UsersAndQuestions:

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){

            })
    }
})

我会用两个查询来做到这一点:

var value = "The beautiful home"