I have a database looking like that:
{
"_id": ObjectId("50525e55467f67da3f8baf06"),
"client": "client1"
}{
"_id": ObjectId("505262f0deed32a758410b1c"),
"client": "client2"
}{
"_id": ObjectId("505269e2062dae91946e3c26"),
"client": "client3"
}{
"_id": ObjectId("5052f469341bdc4e8ac1b226"),
"client": "client4",
"products": [
{
"name": "product1"
},
{
"name": "product2"
}
]
}
My question is, how can I remove product2 from the product list of client4 without removing the client4 record? The output should look like this:
{
"_id": ObjectId("50525e55467f67da3f8baf06"),
"client": "client1"
}{
"_id": ObjectId("505262f0deed32a758410b1c"),
"client": "client2"
}{
"_id": ObjectId("505269e2062dae91946e3c26"),
"client": "client3"
}{
"_id": ObjectId("5052f469341bdc4e8ac1b226"),
"client": "client4",
"products": {
"name": "product1"
}
}
You can use $pull
db.coll.update({client:'client4'},{$pull : { products : { name : 'product1' }});