简体   繁体   中英

Why does the unset query not delete the specified field in mongoDB?

The query I run here produces a blank (no number, basically nothing) in the mongo shell interface:

> db.classrooms.update( { "c_type" : { $exists : true } }, { $unset : { "c_type" : 1 } }, false, true);

Also, I checked the collection rows that should have had c_type removed, but they still exist.

I am basically trying to delete a column/field in my collection with the unset command. Is there something wrong with my syntax?

Thank you!

Further to my comment, here's the example I tested:

MongoDB shell version: 2.0.6
connecting to: test

> db.classrooms.insert({"example": "field", "c_type" : "Open"});
> db.classrooms.insert({"example": "array", "c_type" : ['Available']});
> db.classrooms.insert({"example": "obj",   "c_type" : {'Booked' : 'Yes'}});

> db.classrooms.find()
{
    "_id" : ObjectId("502abd4a332f362f58906683"),
    "example" : "field",
    "c_type" : "Open"
}
{
    "_id" : ObjectId("502abd4e332f362f58906684"),
    "example" : "array",
    "c_type" : [
        "Available"
    ]
}
{
    "_id" : ObjectId("502abd53332f362f58906685"),
    "example" : "obj",
    "c_type" : {
        "Booked" : "Yes"
    }
}

> db.classrooms.update(
   { "c_type" : { $exists : true } },
   { $unset : { "c_type" : 1 } }, 
   false,  // upsert
   true);  // update multiple records

> db.classrooms.find()
{ "_id" : ObjectId("502abd4a332f362f58906683"), "example" : "field" }
{ "_id" : ObjectId("502abd4e332f362f58906684"), "example" : "array" }
{ "_id" : ObjectId("502abd53332f362f58906685"), "example" : "obj" }

Figured it out. Basically I had to double quote $exists and $unset :

> db.classrooms.update( { "c_type" : { "$exists" : true } }, { "$unset" : { "c_type" : 1 } }, 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM