Reputation: 4550
I wanted to know if there is any performance gain or loss in case I save any existing mongoId as string in any other document key. For example I have two collection ans i am saving one collection document Id i.e is _id to another document key as string . I am not going to use it as ref but it is for Viewing purpose and later i can use it to fetch the information from another collection. Example One doc
{_id : ObjectId() , Name : "Test"} // This is one document
Second doc in another collection
{_id : ObjectId , Detail : { AID : ObjectIdASString } } // ObjectIdAsString is Id from first document
Upvotes: 0
Views: 2052
Reputation: 23329
I would store it as ObjectId
, ObjectIds
require less space on disk. Plus, it would be easier and more efficient to sort your collection based on ObjectId
rather than strings. However, nothing scary about it, you can easily convert ObjectId
to string and vice versa using toString()
and valueOf()
methods respectively.
Upvotes: 1