Mongoose deleting (pull) a document within an array, does not work with ObjectID

后端 未结 7 1516
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-27 16:35

I have the following mongoose schema:

user = {
    \"userId\" : \"myId\",
    \"connections\":
    [{
        \"dateConnectedUnix\": 1334567891,
        \"is         


        
7条回答
  •  自闭症患者
    2020-11-27 17:18

    To use update with ObjectId, you should use ObjectId object instead of string representation :

    var ObjectId = require('mongoose').Types.ObjectId;
    
    userAccounts.update({'connections._id': new ObjectId('1234-someId-6789') }, 
                    {$pull: { 'connections._id': new ObjectId('1234-someId-6789') }}, 
                    function (err,val) {
                        console.log(val)
                    });
    

提交回复
热议问题