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

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

I have the following mongoose schema:

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


        
7条回答
  •  执笔经年
    2020-11-27 17:15

    It seems that the above code would not work. It should not even have worked for the first example I gave.

    In the end I was supported by this answer here: MongoDB, remove object from array

    Here is my working code:

    userAccounts.update( 
          { userId: usr.userId },
          { $pull: { connections : { _id : connId } } },
          { safe: true },
          function removeConnectionsCB(err, obj) {
              ...
          });
    

提交回复
热议问题