0
 {
CONTENT1:{
YDXM:[{
"name":"1",
"MBNH":"1"}
{"name":"2",
"MBNH":"2"}]
}

I want to delete the {"name":"1","MBNH":"1"}. How can I achieve this?

gnat
  • 6,213
  • 108
  • 53
  • 73
wurina
  • 1
  • 1

1 Answers1

0

Assuming that the following is your document and you want to delete the ENTIRE document:

{
    "CONTENT1": {
        "YDXM": [
            {
                "name": "1",
                "MBNH": "1"
            },
            {
                "name": "2",
                "MBNH": "2"
            }
        ]
    }
}

You could use this:

db.test.remove({"CONTENT1.YDXM.name" : "1", "CONTENT1.YDXM.MBNH" : "1"})

Now, if you want to extract the document {"name" : "1", "MBNH" : "1"} from the CONTENT1.YDXM array, you should use the $pull operator:

db.test.update({"CONTENT1.YDXM.name" : "1", "CONTENT1.YDXM.MBNH" : "1"}, { $pull :  { "CONTENT1.YDXM" : {"name" : "1", "MBNH" : "1"}  } }, false, true)

This will perform an update in all documents that matches with the first argument. The second argument, with the $pull operator, means that the mongodb will remove the value {"name" : "1", "MBNH" : "1"} from CONTENT1.YDXM array.

You could read more about $pull operator and the update command in this links: http://docs.mongodb.org/manual/reference/operator/pull/ http://docs.mongodb.org/manual/applications/update/

Miguel Cartagena
  • 2,576
  • 17
  • 20