[SERVER-1699] Fill in _id value for inserted docs Created: 30/Aug/10  Updated: 03/Feb/17  Resolved: 03/Feb/17

Status: Closed
Project: Core Server
Component/s: Shell
Affects Version/s: 1.6.1
Fix Version/s: None

Type: Improvement Priority: Major - P3
Reporter: Scott Hernandez (Inactive) Assignee: DO NOT USE - Backlog - Platform Team
Resolution: Won't Fix Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
related to SERVER-7033 Provide details about successes and e... Closed
Backwards Compatibility: Fully Compatible
Participants:

 Description   

The _id value should be set in the document by the shell (driver) before it is sent to the server.

> var test =

{a:1}

> db.test11.insert(test)
> test

{ "a" : 1 }

— I would like to see the _id field after the insert

> var test =

{a:1}

> db.test11.insert(test)
> test

{ "_id" : ObjectId("...."), "a" : 1 }

 Comments   
Comment by Tim van Elsloo [ 13/Jul/11 ]

This actually does happen when you insert new objects using the PHP-driver, which is great. Since writes are very fast, most of the time _id is already set, even if you didn't provide the $safe-param (which you should do, if your application relies on this).

Now, back to the shell-driver. This probably isn't working because (afaik) JS doesn't support pass-by-reference, which is needed to accomplish what you want. For example:

PHP:
$obj = array('foo' => 'bar');
change(&$obj); // in PHP5, the &-symbol isn't needed anymore.
print_r($obj);

function change($obj) {
$obj['foo'] = 'foo';
}

This will print something like array('foo' => 'foo');

However, this isn't possible in JS (there's no &-symbol either):
var obj =

{'foo': 'bar'}

;
change(obj);
alert(obj.foo);

function change(obj) {
obj.foo = 'foo';
}

This will alert 'bar', because there's no such thing in JS.

This would be great, but isn't possible without modifying the underlying js-engine.

Generated at Thu Feb 08 02:57:46 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.