简体   繁体   中英

Why does this query not update mongodb field value?

I have a document containing books array

"_id" : ObjectId("517b80cf9de5ee780b000003"),
"publisher_name" : "ABC",
"books" : [{
  "_id" : ObjectId("517b80cf9de5ee780b000000"),
  " title" : "<b>AAA</b>",
  "published" : 1
}, {
  "_id" : ObjectId("517b80cf9de5ee780b000001"),
  " title" : "<b>BBB</b>",
   "published" : 1
}, {
  "_id" : ObjectId("517b80cf9de5ee780b000002"),
  " title" : "<b>CCC</b>",
  "published" : 1
}],

I like to update the field "published" setting it to "0"

$theObjId = new MongoId($bookid); 
$collection->update(array("_id" => $theObjId ), array('$set' => array('published'=> '0')));

But the query I use does not update it.

I solved. I should have used the $ positional operator.

$theObjId = new MongoId($bookid); 
$collection->update(array("_id" => $theObjId ), array('$set' => array('books.$.published'=> '0')));

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