1

Any one help me to create mongo query for deleting where "name" : "gdfgdfgdfg" embedded document

The object stored as below in Mongo db.

{
    "_id": ObjectId("50656f33a4e82d3f98291eff"),
    "description": "gdfgdfgdfg",
    "menus": [
    {
        "name": "gdfgdfgdfg"**,
        "description": "dfgdgd",
        "text": "dfgdfg",
        "key": "2",
        "onSelect": "yyy",
        "_id": ObjectId("50656f3ca4e82d3f98291f00")
    },
    {
        "name": "dfg",
        "description": "dfgdfgdfgdf",
        "text": "dfgdgf",
        "key": "1",
        "onSelect": "uuuu",
        "_id": ObjectId("50656f44a4e82d3f98291f01")
    }
    ]
}

Any one help me, I'm new to Mongo

eduardo a
  • 152
  • 4
  • 17
user1705980
  • 183
  • 1
  • 1
  • 9

3 Answers3

1

In the JavaScript shell you can do this:

var query = {"_id": ObjectId("50656f33a4e82d3f98291eff")};
db.collection.update(query, {'$pull':{ menus: {name : 'gdfgdfgdfg'} } });

or use the Id.

db.collection.update(query, {'$pull': { menus: {"_id": ObjectId("50656f3ca4e82d3f98291f00")} } });

With the Java Driver should be something like this:

BasicDBObject query = new BasicDBObject("_id", new ObjectId("50656f33a4e82d3f98291eff"));

BasicDBObject docToRemove = new BasicDBObject("name", "gdfgdfgdfg");

BasicDBObject updateCommand = new BasicDBObject("$pull", new BasicDBObject("menus", docToRemove));

collection.update(query, updateCommand);
Gianfranco P.
  • 10,049
  • 6
  • 51
  • 68
  • how to update i have tried like this BasicDBObject updateCommand = new BasicDBObject("$set", new BasicDBObject("menus", docToRemove)); – user1705980 Sep 28 '12 at 12:15
  • the update() function is from the collection. `DBCollection collection = db.getCollection("test"); collection.update(query, updateCommand);` – Gianfranco P. Sep 28 '12 at 14:48
0

Mongo won't let you delete the embedded document. What you have to do is take the object from the collection, delete the one object in the list, and then save it back into the database.

obj = db.collection.findOne({"_id": ObjectId("50656f33a4e82d3f98291eff")});
menus = obj.menus.splice(0,1); // Or some other way to manually delete
                               // the one item in the list
db.collection.update({"_id": ObjectId("50656f33a4e82d3f98291eff")},
                     {$set: {menus: menus}});

It's complicated, see here.

EDIT: If you don't know the index, you can try this:

obj = db.collection.findOne({"_id": ObjectId("50656f33a4e82d3f98291eff")});
var i = 0;
for(i=0;i<obj.menus.length;i++) {
    if(obj.menus[i].name === "gdfgdfgdfg")
        break;
}
menus = obj.menus.splice(i,1);
db.collection.update({"_id": ObjectId("50656f33a4e82d3f98291eff")},
                     {$set: {menus: menus}});
Community
  • 1
  • 1
Lanbo
  • 15,118
  • 16
  • 70
  • 147
  • thanks. it helps me,is it possible to delete the document without splicing using the "_id": ObjectId("50656f3ca4e82d3f98291f00").it need some thing concrete – user1705980 Sep 28 '12 at 10:50
  • I have tried this below code but its deleting whole document WriteResult result = db.collection.remove(new BasicDBObject("_id", ObjectId("50656f33a4e82d3f98291eff")), WriteConcern.NORMAL)); – user1705980 Sep 28 '12 at 10:55
  • In mongodb, embedded documents are *not* treated the same way as collection documents. There is no query to delete single array members. You have to do it manually. – Lanbo Sep 28 '12 at 10:56
-1
$conditionArray=array("_id"=>ObjectId("50656f33a4e82d3f98291eff"));
$dataArray=array("description"=>"");
$db->$collectionName->update($conditionArray,array('$unset' =>$dataArray))->limit(1);
  • thanks, But i want to delete only the first document { "name": "gdfgdfgdfg"**, "description": "dfgdgd", "text": "dfgdfg", "key": "2", "onSelect": "yyy", "_id": ObjectId("50656f3ca4e82d3f98291f00") } in the menus. – user1705980 Sep 28 '12 at 10:39
  • This is nonsense. It only deletes the description field. – Lanbo Sep 28 '12 at 10:44