GithubHelp home page GithubHelp logo

Comments (6)

alcaeus avatar alcaeus commented on July 19, 2024 5

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

from mongo-php-library.

Henk8 avatar Henk8 commented on July 19, 2024 1

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!

from mongo-php-library.

jacqueswaller avatar jacqueswaller commented on July 19, 2024

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 πŸ˜„

from mongo-php-library.

Henk8 avatar Henk8 commented on July 19, 2024

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 πŸ˜‰

from mongo-php-library.

derickr avatar derickr commented on July 19, 2024

@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.

from mongo-php-library.

jmikola avatar jmikola commented on July 19, 2024

Created PHPLIB-178 to track this task.

from mongo-php-library.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.