简体   繁体   中英

Update a record in mongodb with php

I want to update a record and I have already known its _id in mongodb. I followed this : MongoDB $set not updating record

$idVal ="some value";

$new_data =array('$set'=> array("Domain"=>$domain), 
array("author"=>$author),array("title"=>$title));

$collectionList->update(array("_id"=>$idVal), $new_data);

The error message is error message127.0.0.1:27017: Unknown modifier: 0

What is wrong with my query? thank you in advance!

Set $idVal with any number and try..

Example:

$idVal =123456;

$new_data =array('$set'=> array("Domain"=>$domain), 
array("author"=>$author),array("title"=>$title));

$collectionList->update(array("_id"=>$idVal), $new_data);

You specified wrong update data. If you take a closer look at $new_data array, you will notice that it is an array with 3 keys:

$new_data = array(
    '$set' => array("Domain"=>$domain),
    0 => array("author"=>$author),
    1 => array("title"=>$title)
);

That's why you've got an error "Unknown modifier: 0" , because 0 is not a mongodb update operator.

In your case $new_data array should be:

$new_data = array(
    '$set' => array(
        "Domain"=>$domain, 
        "author"=>$author,
        "title"=>$title
    )
);

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