please take a look at my MongoDB 2.4 database: trip table:
{_id: 1,
"actions": [{ "type": "Flight" }, { "type": "Go" }, { "type": "Train" }, { "type": "Sleep" }]
},
{_id: 2,
"actions": [{ "type": "Go" }, { "type": "Sleep" }, { "type": "Taxi" }]
},
{_id: 3,
"actions": [{ "type": "Flight" }]
},
{_id: 4
}
I want to remove action which type is "Flight", "Taxi", or "Train". This is my script:
db.trip.update({"actions": {$exists: true}}, {$pull: {"actions": {"type": {$in: ["Flight", "Taxi", "Train"]}}}});
or:
db.trip.update({"actions": {$exists: true}}, {$pull: {"actions": {"type": "Flight"}}});
db.trip.update({"actions": {$exists: true}}, {$pull: {"actions": {"type": "Train"}}});
db.trip.update({"actions": {$exists: true}}, {$pull: {"actions": {"type": "Taxi"}}});
No way can solve this requirement. I don't know what is difference of mongoDB on MacOS and centOS. I've test this on 2 OS, test on terminal and MongoHub. Please help me!
The objects after updated will be:
{_id: 1,
"actions": [{ "type": "Go" }, { "type": "Sleep" }]
},
{_id: 2,
"actions": [{ "type": "Go" }, { "type": "Sleep" }]
},
{_id: 3,
"actions": []
},
{_id: 4
}
Update your queries as :
db.trip.update({"actions.type": "Flight"}, {$pull: {"actions": {"type": "Flight"}}});
db.trip.update({"actions.type": "Train"}}, {$pull: {"actions": {"type": "Train"}}});
db.trip.update({"actions.type": "Taxi"}}, {$pull: {"actions": {"type": "Taxi"}}});
or
db.trip.update({"actions" : {$in : [{type : "Flight"}, {type: "Taxi"}, {type : "Train"}]}},{$pull:{"actions" : {$in : [{type : "Flight"}, {type: "Taxi"}, {type : "Train"}]}}},false,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.