Rahul
Rahul

Reputation: 2600

How do I delete values from this document in MongoDB using Python

I am having a document which is structured like this

{
    "_id" : ObjectId("564c0cb748f9fa2c8cdeb20f"),
    "username" : "blah",
    "useremail" : "[email protected]",
    "groupTypeCustomer" : true,
    "addedpartners" : [
        "562f1a629410d3271ba74f74",
        "562f1a6f9410d3271ba74f83"
    ],
    "groupName" : "Mojito",
    "groupTypeSupplier" : false,
    "groupDescription" : "A group for fashion designers"
}

Now I want to delete one of the values from this 'addedpartners' array and update the document. I want to just delete 562f1a6f9410d3271ba74f83 from the addedpartners array

This is what I had tried earlier.

db.myCollection.update({'_id':'564c0cb748f9fa2c8cdeb20f'},{'$pull':{'addedpartners':'562f1a6f9410d3271ba74f83'}})

Upvotes: 2

Views: 158

Answers (2)

Soni Pandey
Soni Pandey

Reputation: 514

db.myCollection.update(
  { _id: ObjectId(id) },
  { $pull: { 'addedpartners': '562f1a629410d3271ba74f74' } }
);

Try with this

Upvotes: 2

Korhan Yüzbaş
Korhan Yüzbaş

Reputation: 116

db.myCollection.update({}, {$unset : {"addedpartners.1" : 1 }}) 
db.myCollection.update({}, {$pull : {"addedpartners" : null}})

No way to delete array directly, i think this is going to work, i haven't tried yet.

Upvotes: -1

Related Questions