0

I have a data set like this:

{
    "_id" : ObjectId("5bacc98431481e0520856df8"),
    "action" : {
        "count" : 0,
        "shop" : [ 
            {
                "uid" : ObjectId("5c0b396a50b8a627c4172a2b"),

            }, 
            {
                "uid" : ObjectId("5c0b3b471ed11f2124e1e3a8"),

            },
            {
                "uid" : ObjectId("5c0b3b471ed11f2124e1e3a9"),

            }
         ]
      }
}

How will I remove the above object whose uid is ObjectId("5c0b3b471ed11f2124e1e3a8") through mongodb Query?

I used with the approach which is not perfect according to me. My approach is like this:

db.CollectionName.find({_id: ObjectId("5bacc98431481e0520856df8")})
.then(data => {
  if (data.length > 0) {
        let xData = data[0].notifications.shop;
        let xuid = ObjectId("5c0b3b471ed11f2124e1e3a8");
        let filterData = xData.filter(
          x => x.uid!= xuid
        );
        User.update(
          { _id: ObjectId("5bacc98431481e0520856df8")},
          {
            $set: {
              action: { shop: filterData }
            }
          }
        )
        .then(usr => {
          console.log("deleted successfully")
         })
         .catch(er => {
           console.log(er)
         })
  }
})
.catch(error => {
    console.log(error)
}) 

By this approach I remove the uid from an array which itself under an object. If anyone knows this type of task done through MongoDB Query then please let me know.

Any Help/Suggestion is really appreciated. Thanks in advance for the developer who attempted my Query.

2
  • Try this db.collection.findOneAndUpdate( { "_id": ObjectId('5bacc98431481e0520856df8') }, { "$pull": { "action.$.shop": { "uid": ObjectId('5c0b3b471ed11f2124e1e3a8') }}} ) Commented Dec 8, 2018 at 5:24
  • dear @AnthonyWinzlet i followed your query and get the error that is MongoError: The positional operator did not find the match needed from the query Commented Dec 8, 2018 at 5:30

1 Answer 1

1

The mongo statement to do this would be:

db.getCollection('YOURCOLECTION').update(
{'_id': ObjectId("5bacc98431481e0520856df8")}, 
{ "$pull": 
       { "action.shop": { "uid": ObjectId("5c0b3b471ed11f2124e1e3a8") }} 
})

You would be using $pull command in combination with the $update.

Sign up to request clarification or add additional context in comments.

5 Comments

Dear, I follow your query, according to this nothing is pulled on an array. i got the mongodb response that is : { n: 1, nModified: 0, opTime: { ts: Timestamp { bsontype: 'Timestamp', low: 2, high_: 1544246568 }, t: 374 }, electionId: 7fffffff0000000000000176, ok: 1, operationTime: Timestamp { bsontype: 'Timestamp', low: 2, high_: 1544246568 }, '$clusterTime': { clusterTime: Timestamp { bsontype: 'Timestamp', low: 2, high_: 1544246568 }, signature: { hash: [Object], keyId: 0 } } }
here nModified is 0 not 1
I tested it locally with your exact data on mongoDB 3.6 so I am pretty sure that the query is solid.
Make sure you have the correct collection in db.getCollection('YOURCOLECTION') etc
Yes You are right, actually I have done a mistypo error, thats the reason, anyways thanks and I accepted the answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.