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

Hi, Guys, when will you support upserts? #31

Closed
lsc20051426 opened this issue Aug 29, 2013 · 4 comments
Closed

Hi, Guys, when will you support upserts? #31

lsc20051426 opened this issue Aug 29, 2013 · 4 comments

Comments

@lsc20051426
Copy link

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) {
["_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.

drop(); $c->update( ``` array("name" => "joe"), array("username" => "joe312", "createdAt" => new MongoDate()), array("upsert" => true) ``` ); var_dump($c->findOne()); ?>

The above example will output something similar to:

array(3) {
["_id"]=>
object(MongoId)#10 (0) {
}
["username"]=>
string(6) "joe312"
["createdAt"]=>
object(MongoDate)#4 (0) {
}
}

@jenssegers
Copy link
Contributor

I can add a second optional options parameter to the update method that allows you to pass mongo specific options.

@lsc20051426
Copy link
Author

cool, you acts fast.

@abishekrsrikaanth
Copy link

Could you give an example on the documenation on how to do this. Will help a lot.

jenssegers added a commit that referenced this issue Sep 27, 2013
@jenssegers
Copy link
Contributor

Done :)

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

3 participants