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

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

I have the following mongoose schema:

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


        
7条回答
  •  感情败类
    2020-11-27 17:04

    mongoose: 4.11.11
    What have worked for me is the following syntax:

    const removeTansactionFromUser = (userId, connectionId) => {
        return User.findByIdAndUpdate(userId, { $pull: { "connections": connectionId} }, {'new': true} );
    };
    

    Mongoose support id in string format or ObjectId format.
    Tip: new ObjectId(stringId) to switch from string to ObjectId

提交回复
热议问题