Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upsert option not working #120

Closed
Henk8 opened this issue Feb 12, 2016 · 6 comments
Closed

Upsert option not working #120

Henk8 opened this issue Feb 12, 2016 · 6 comments

Comments

@Henk8
Copy link

Henk8 commented Feb 12, 2016

I have implemented this new library and the save function is deprecated. I wan't to use the insertOne with the upsert option, but it fails.

$this->_connection = new MongoDB\Client($host);
$this->_db = $this->_connection->{$this->_database};

$this->_collection = $this->_db->{$this->_table};
$this->_collection->createIndex(array("_id" => 1));

$result = $this->_collection->insertOne(array("_id" => $this->key($id, $group), "group" => $group, "data" => $data, "timeStamp" => time() + $this->options['lifetime']), ['upsert' => true]);

This will return a BulkWrite error with code 0 and no message.
What is wrong with the code?

@jacqueswaller
Copy link

I've only ever used upsert with the update function. Like my understanding is "update, but insert if not exists". So presumably the upsert option is ignored for inserts.

Also I think errors are handled as exceptions now (UpdateResult has no API for getting error messages). If you try/catch and check the exception's error message, I'd bet a nickel that your insertOne is failing because you already have a document with that ID. So yeah use updateOne 😄

@Henk8
Copy link
Author

Henk8 commented Feb 15, 2016

I do use the try/catch construction. The error message is: BulkWrite error.
So I moved to the updateOne construction and now the message is: First key in $update argument is not an update operator.

The code is:

 $result = $this->_collection->updateOne(array("_id" => $this->key($id, $group)), 
                                array("_id" => $this->key($id, $group), "group" => $group, "data" => $data, "timeStamp" => time() + $this->options['lifetime']), 
                                ['upsert' => true]); 

What am I doing wrong. Can you perhaps give me some example code that works, that would really help 😉

@alcaeus
Copy link
Member

alcaeus commented Feb 15, 2016

If you're not using update operators, you're not updating a document but replacing it. Thus, use the replaceOne method instead of updateOne.

@Henk8
Copy link
Author

Henk8 commented Feb 15, 2016

That seems to do the trick.
The code is now:

 $result = $this->_collection->replaceOne(["_id" => $this->key($id, $group)],
                                             ["_id" => $this->key($id, $group), "group" => $group, "data" => $data, "timeStamp" => time() + $this->options['lifetime']],
                                             ['upsert' => true]);

Thanks!

@derickr
Copy link
Contributor

derickr commented Feb 22, 2016

@alcaeus provides the right answer. I'm marking this as closed.

The library's documentation at http://mongodb.github.io/mongo-php-library/classes/collection/#crud-operations says "CRUD is an acronym for Create, Read, Update, and Delete. The Collection class implements MongoDB’s cross-driverCRUD specification, which defines a common API for collection-level read and write methods." It links to https://github.com/mongodb/specifications/blob/master/source/crud/crud.rst#update-vs-replace-validation, which discusses the differences between update and replace.

I do think, that the library's documentation should be improved on this. It's in the repository at https://github.com/mongodb/mongo-php-library/blob/master/docs/classes/collection.md#crud-operations if you want to give this a shot yourself.

@jmikola
Copy link
Member

jmikola commented Feb 22, 2016

Created PHPLIB-178 to track this task.

@jmikola jmikola closed this as completed Feb 22, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants