-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Hi, Guys, when will you support upserts? #31
Comments
I can add a second optional options parameter to the update method that allows you to pass mongo specific options. |
cool, you acts fast. |
Could you give an example on the documenation on how to do this. Will help a lot. |
Done :) |
This was referenced Nov 26, 2018
3 tasks
mnphpexpert
added a commit
to mnphpexpert/laravel-mongodb
that referenced
this issue
Sep 2, 2024
mnphpexpert
added a commit
to mnphpexpert/laravel-mongodb
that referenced
this issue
Sep 2, 2024
# for free
to join this conversation on GitHub.
Already have an account?
# to comment
refer:
http://docs.mongodb.org/manual/core/update/#update-operations-with-the-upsert-flag
http://php.net/manual/en/mongocollection.update.php
Example #2 MongoCollection::update() upsert examples
Upserts can simplify code, as a single line can create the document if it does not exist (based on $criteria), or update an existing document if it matches.
In the following example, $new_object contains an atomic modifier. Since the collection is empty and upsert must insert a new document, it will apply those operations to the $criteria parameter in order to create the document.
drop(); $c->update( ``` array("uri" => "/summer_pics"), array('$inc' => array("page hits" => 1)), array("upsert" => true) ``` ); var_dump($c->findOne()); ?>The above example will output something similar to:
array(3) {
drop(); $c->update( ``` array("name" => "joe"), array("username" => "joe312", "createdAt" => new MongoDate()), array("upsert" => true) ``` ); var_dump($c->findOne()); ?>["_id"]=>
object(MongoId)#9 (0) {
}
["uri"]=>
string(12) "/summer_pics"
["page hits"]=>
int(1)
}
If $new_object does not contain atomic modifiers (i.e. $ operators), upsert will use $new_object as-is for the new document. This matches the behavior of a normal update, where not using atomic modifiers causes the document to be overwritten.
The above example will output something similar to:
array(3) {
["_id"]=>
object(MongoId)#10 (0) {
}
["username"]=>
string(6) "joe312"
["createdAt"]=>
object(MongoDate)#4 (0) {
}
}
The text was updated successfully, but these errors were encountered: