简体   繁体   中英

mongodb pull object from an array not working using mongoose

I am trying to remove a comment object from an array in a mongodb using the $pull operator and it seems like I have the syntax correct but it is not modifying anything.

I have looked through all the examples given on Stack to make but it still keeps responding with

{ n: 0,
nModified: 0,
opTime:
{ ts:
  Timestamp { _bsontype: 'Timestamp', low_: 1, high_: 1548664023 },
 t: 1 },
electionId: 7fffffff0000000000000001,
ok: 1,
operationTime:
 Timestamp { _bsontype: 'Timestamp', low_: 1, high_: 1548664023 },
'$clusterTime':
 { clusterTime:
  Timestamp { _bsontype: 'Timestamp', low_: 1, high_: 1548664023 },
  signature: { hash: [Binary], keyId: [Long] } } }

this is the field I currently have in the DB

 {
"_id" : ObjectId("5be23d8aa365d853ddfd6f15"),
"__v" : 0,
"restaurant" : {
    info about restaurant
},
"comments" : [
    {
        "id" : "61DSLu7fFcUZ2chA8-A6HQ",
        "user" : "test",
        "comment" : "test"
    },
    {
        "comment" : "testing",
        "user" : "testing",
        "id" : ObjectId("5c3cd3a5647f180484a5ca18")
    },
    {
        "restaurant_id" : "61DSLu7fFcUZ2chA8-A6HQ",
        "comment" : "tacos",
        "name" : "test",
        "user_id" : ObjectId("5c48fdf47e9ed81b08536602")
    },
    {
        "restaurant_id" : "61DSLu7fFcUZ2chA8-A6HQ",
        "comment" : "tacos",
        "name" : "test",
        "comm_id" : ObjectId("5c49019f8528f31b2adfb914")
    },
    {
        "restaurant_id" : "61DSLu7fFcUZ2chA8-A6HQ",
        "comment" : "hello",
        "name" : "test",
        "comm_id" : ObjectId("5c490237fd6e781b52f801fe")
    }
],
"likes" : {
    "likes" : 6
}

Currently my model shows within my restaurants model

comments: [{
    restaurant_id : String,
    comment : String,
    name : String,
    comm_id : String,
  }]

the update method I have currently

db.restaurants.updateOne({restaurant_id: rest_id},
    { $pull: { comments: { $in: [{comment: "hello"}] } }
  }, { safe: true })

and also have tried

db.restaurants.updateOne({restaurant_id: rest_id},
    { $pull: { comments: { $in: {"comment": "hello"} } }
  }, { safe: true })

as well as

db.restaurants.updateOne({restaurant_id: rest_id},
    { $pull: { comments: { comment: "hello"} } }
 }, { safe: true })

and similar variation. I can't seem to pinpoint my mistake. the response seems like it is finding the correct restaurant field, but my $pull operator just isn't working properly. Is there something wrong with my syntax or does it not work in this scenario.

Ideally, I will use the comm_id field to remove the object from the array, but I am using comment: "hello" just to test.

Is it possibly because I have different fields in the first few comments?

do it simple it will works

db.restaurants.updateOne({restaurant_id: rest_id},
    { $pull: { comments.comment: "hello"}  }
 }, { safe: true })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM