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

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

I have the following mongoose schema:

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


        
相关标签:
7条回答
  • 2020-11-27 17:18

    You can do it in mongoose 5.4.x and above

    const result = await User.findByIdAndUpdate(user_id,
                {
                    $pull: {
                        someArrayName: { _id: con_id }
                    }
                }, { new: true });
    
    
    if (result)
        console.log(result)
    

    The item from connections array will be removed based on provided property _id value

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