0

using mongo db library for ci link my user collection is as follows

{

 "_id" : ObjectId("5a9cf393be713df40e00002d"),
 "id" : "1",
 "name" : "ali",
 "category" :  {
    "back" : "12",
    "front" : "2"
 }

}

Delete front : 2 field from my collection i write this

  $where = array('_id'=> new MongoId($id));
      $data = array(
        'category' => array(
            'front' => ''
        )
    );
      $update = array('$unset' =>  $data);


     $this->mongoci->db->user->update($where,$update);

after execute this query the result will be like this

{

 "_id" : ObjectId("5a9cf393be713df40e00002d"),  
 "category" : {    
    "front" : "2"
 }
}

i want this result

{

"_id" : ObjectId("5a9cf393be713df40e00002d"),
"id" : "1",
"name" : "ali",
"category" : {
    "back" : "12"

}
}

bumped to the homepage by Community 2 days ago

This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.

0

Did you tried like this query

db.library.update(
  {},
  { $unset: { <field1>: "", ... } },
  false, true
)

As per MongoDB BOL Here The specified value in the $unset expression (i.e. "") does not impact the operation.

If the field does not exist, then $unset does nothing (i.e. no operation).

When used with $ to match an array element, $unset replaces the matching element with null rather than removing the matching element from the array. This behavior keeps consistent the array size and element positions.

Note: To specify a <field> in an embedded document or in an array, use dot notation.

For further your ref Here and Here

  • $filter = array(); $where = array('_id'=> new MongoId($id)); $remove_document = array( 'category' => array( 'front' => '' ) ); $options = array( 'multiple' => 'true' ); $this->mongoci->db->user->update($filter, $remove_document, $options); i try this but it cannot work – UNknow Mar 5 '18 at 10:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.