Last active
August 29, 2015 14:28
-
-
Save chrisguitarguy/c774959cf1b96be7d754 to your computer and use it in GitHub Desktop.
Does $setOnInsert not work with a projection?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| $mongo = new \MongoClient(); | |
| $col = $mongo->testDb->testCollection; | |
| $col->drop(); | |
| // only returns the `_id` attribute, despite the "fields" argument | |
| var_dump($col->findAndModify([ | |
| 'test' => 'one', | |
| ], [ | |
| '$setOnInsert' => ['test' => 'one'], | |
| ], ['test'], [ | |
| 'upsert' => true, | |
| 'new' => true, | |
| ])); | |
| $col->drop(); | |
| // includes the `test` attribute as well as `_id` | |
| var_dump($col->findAndModify([ | |
| 'test' => 'two', | |
| ], [ | |
| '$setOnInsert' => ['test' => 'two'], | |
| ], null, [ | |
| 'upsert' => true, | |
| 'new' => true, | |
| ])); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| local> mongo test123 | |
| MongoDB shell version: 3.0.2 | |
| connecting to: test123 | |
| Server has startup warnings: | |
| 2015-08-21T16:35:12.578-0400 I CONTROL [initandlisten] | |
| 2015-08-21T16:35:12.578-0400 I CONTROL [initandlisten] ** WARNING: soft rlimits too low. Number of files is 256, should be at least 1000 | |
| > db.test.find() | |
| > db.testCollection.findAndModify({query: {test: 'one'}, update: {$setOnInsert: {test: 'one'}}, fields: ['test'], upsert: true, new: true }); | |
| { "_id" : ObjectId("55db7e0566a857b7d4f13d1f") } | |
| > db.testCollection.findAndModify({query: {test: 'three'}, update: {$setOnInsert: {test: 'three'}}, upsert: true, new: true }); | |
| { "_id" : ObjectId("55db7e1f66a857b7d4f13d20"), "test" : "three" } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment