[DOCS-7733] Comment on: "manual/reference/method/ObjectId.txt" Created: 23/Apr/16  Updated: 03/Nov/17  Resolved: 18/May/16

Status: Closed
Project: Documentation
Component/s: None
Affects Version/s: None
Fix Version/s: 01112017-cleanup

Type: Bug Priority: Major - P3
Reporter: Docs Collector User (Inactive) Assignee: Ravind Kumar (Inactive)
Resolution: Done Votes: 0
Labels: collector-298ba4e7
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Location: https://docs.mongodb.org/manual/reference/method/ObjectId/
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/49.0.2623.108 Chrome/49.0.2623.108 Safari/537.36
Referrer: https://www.google.co.il/
Screen Resolution: 1920 x 1080
repo: docs
source: reference/method/ObjectId


Participants:
Days since reply: 7 years, 39 weeks ago

 Description   

Hi there !
I've been searching for almost an hour today for a way to generate an object ID in the same way as the objectId() function works but via a query.
Is there a way to do that ?
If there is, it would be superb if there was a reference to it from this page.
Thanks for the help and the awesome MongoDB wok in general
Nir.



 Comments   
Comment by Ravind Kumar (Inactive) [ 18/May/16 ]

User wanted to create ObjectId in code without going to shell and parsing output of ObjectId().

Suggested the ObjectId() constructor using the driver associated to their code as a solution.

Comment by Ravind Kumar (Inactive) [ 25/Apr/16 ]

Hello Nir,

Your best option is to use the find() method , iterate through the cursor, and update each document using updateOne() and the $set operator.

For example,

> cursor = db.foo.find( {} )
{ "_id" : ObjectId("571e233374459484549aa81c"), "foo" : 0, "bar" : 0.24257829878479242 }
{ "_id" : ObjectId("571e233374459484549aa81d"), "foo" : 1, "bar" : 0.013469574507325888 }
{ "_id" : ObjectId("571e233374459484549aa81e"), "foo" : 2, "bar" : 0.38313884986564517 }
{ "_id" : ObjectId("571e233374459484549aa81f"), "foo" : 3, "bar" : 0.4146526902914047 }
{ "_id" : ObjectId("571e233374459484549aa820"), "foo" : 4, "bar" : 0.06776897283270955 }
{ "_id" : ObjectId("571e233374459484549aa821"), "foo" : 5, "bar" : 0.9931269292719662 }
{ "_id" : ObjectId("571e233374459484549aa822"), "foo" : 6, "bar" : 0.4843080462887883 }
{ "_id" : ObjectId("571e233374459484549aa823"), "foo" : 7, "bar" : 0.7653377656824887 }
{ "_id" : ObjectId("571e233374459484549aa824"), "foo" : 8, "bar" : 0.03183381538838148 }
{ "_id" : ObjectId("571e233374459484549aa825"), "foo" : 9, "bar" : 0.030935481656342745 }
> var cursor = db.foo.find( { "foo" : { $lt : 10 } } );
> while ( cursor.hasNext() ) {
... val = cursor.next();
... id = val._id;
... db.foo.updateOne( { "_id" : id }, { $set : { "bar" :ObjectId() } } );
... }
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
> db.foo.find()
{ "_id" : ObjectId("571e233374459484549aa81c"), "foo" : 0, "bar" : ObjectId("571e25b59a1f1f7488e54a02") }
{ "_id" : ObjectId("571e233374459484549aa81d"), "foo" : 1, "bar" : ObjectId("571e25b59a1f1f7488e54a03") }
{ "_id" : ObjectId("571e233374459484549aa81e"), "foo" : 2, "bar" : ObjectId("571e25b59a1f1f7488e54a04") }
{ "_id" : ObjectId("571e233374459484549aa81f"), "foo" : 3, "bar" : ObjectId("571e25b59a1f1f7488e54a05") }
{ "_id" : ObjectId("571e233374459484549aa820"), "foo" : 4, "bar" : ObjectId("571e25b59a1f1f7488e54a06") }
{ "_id" : ObjectId("571e233374459484549aa821"), "foo" : 5, "bar" : ObjectId("571e25b59a1f1f7488e54a07") }
{ "_id" : ObjectId("571e233374459484549aa822"), "foo" : 6, "bar" : ObjectId("571e25b59a1f1f7488e54a08") }
{ "_id" : ObjectId("571e233374459484549aa823"), "foo" : 7, "bar" : ObjectId("571e25b59a1f1f7488e54a09") }
{ "_id" : ObjectId("571e233374459484549aa824"), "foo" : 8, "bar" : ObjectId("571e25b59a1f1f7488e54a0a") }
{ "_id" : ObjectId("571e233374459484549aa825"), "foo" : 9, "bar" : ObjectId("571e25b59a1f1f7488e54a0b") }

If what you want to do is to mutate or otherwise modify the ObjectID within a query, I suggest using the aggregation framework:

db.foo.aggregate(
    [ 
        {$limit: 1 }, 
        { $project: 
            { myId: 
                { $literal: ObjectId() } 
            } 
        }
    ] 
)

You can then use additional aggregation stages, expressions, or accumulators to modify and mutate the ObjectID as required. See Aggregation Pipeline Operators

Let me know if this makes sense.

Generated at Thu Feb 08 07:54:53 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.