简体   繁体   中英

Remove subfields from mongoDb document

I have documents like this

{
   "_id": ObjectId("4ffa96436ccc195553000055"),
   "on": {
     "4e8614f66ccc19aa490006e3": {
       "hid": ObjectId("4e8614f66ccc19aa490006e3"),
       "mts": NumberInt(1352979215)
    },
     "4e8614f06ccc19d9340003e8": {
       "hid": ObjectId("4e8614f06ccc19d9340003e8"),
       "mts": NumberInt(1352979216)
    },
     "4e8614346ccc19aa490006df": {
       "hid": ObjectId("4e8614346ccc19aa490006df"),
       "mts": NumberInt(1352979218)
    },
     "505af2e66ccc19541d0005a9": {
       "hid": ObjectId("505af2e66ccc19541d0005a9"),
       "mts": NumberInt(1352979220)
    },
     "505af2d76ccc19f11300109a": {
       "hid": ObjectId("505af2d76ccc19f11300109a"),
       "mts": NumberInt(1352979221)
    }
  }
}

Sometimes i need to remove subfields from "on" field. I do this so:

   $this->collection->update(
        array(
            '_id'  => new MongoId('4ffa96436ccc195553000055'),
            "on.4e8614f66ccc19aa490006e3" => array('$exists' => true),
        ),
        array(
            '$unset'  => array(
                "on.4e8614f66ccc19aa490006e3" => 1
            )
        )
    );

But field don`t delete. What am I doing wrong?

PS I checked for errors after send query, and i got this error Modifiers and non-modifiers cannot be mixed Code:10154

Thanks everyone who helped me with this question. I found problem. When i wrote question i used simple version of update query. This is full version

    c($this->table)->update(
        array(
            '_id'  => new MongoId($uid),
            "on.{$strHid}" => array('$exists' => true),
        ),
        array(
            'mts' => time()
            '$unset'    => array(
                "on.{$strHid}" => 1
            )
        )
    ); 

My trouble was in this part of code. Here i use modifiers and non-modifiers.

        array(
            'mts' => time()
            '$unset'    => array(
                "on.{$strHid}" => 1
            )
        )

I rewrote it like this and everything ok

        array(
            '$set'      => array(
                'mts' => time()
            ),
            '$unset'    => array(
                "on.{$strHid}" => 1
            )
        )

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