In MongoDB, object ID is quite likely unique across collections.
So is it possible to query a Mongo database for a document using the object ID alone, without knowing what collection it's in?
Thanks
As you and @Sammaye is stating in the comments the ObjectID will likely be unique because (taken from the mongodb site)
ObjectId is a 12-byte BSON type, constructed using:
a 4-byte value representing the seconds since the Unix epoch,
a 3-byte machine identifier,
a 2-byte process id, and
a 3-byte counter, starting with a random value.
So it is pretty much impossible for it to be the same. (I can't seem to find if the 3-byte counter is the same across all collections or if there is a different one per collection)
For simplicity's sake lets say ObjectID is unique for every record in the database.
There is no "one query" that will search the whole database for the record. Mongodb has no default way to achieve what you are asking.
For a non-trivial solution see this question
Update after comment
if you don't care to query the database multiple times, you can
(This is just descriptive of how to do it since you are not stating what programming language you are using to give you an example)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With