MongoDB $pull syntax

后端 未结 1 1729
天涯浪人
天涯浪人 2021-01-24 14:43

I\'m having a (hopefully) small syntax problem with $pull in Mongodb.

bulk.find({_id: new mongo.ObjectID(req.session._id)}).updateOne({$pull: {
  firstArray: {i         


        
相关标签:
1条回答
  • 2021-01-24 15:18

    Your "secondArray" has a nested element structure, so you must identify the outer element you want to match in your query when using a positional $ operator in the update. You basically need something like this:

    bulk.find({ 
        "_id": new mongo.ObjectID(req.session._id), 
        "secondArray._id": "7423" 
    }).update({
        "$pull": { 
            "firstArray": { "_id": "153" },
            "secondArray.$.firstArrayIds": 153
        }
    });
    

    So there are in fact "two" id values you need to pass in with your request in addition to the general document id. Even though this is nested it is okay since you are only matching on the "outer" level and only on one array. If you tried to match the position on more than one array then this is not possible with the positional operator.

    0 讨论(0)
提交回复
热议问题