I've looked in official manual for explanations on "what is ObjectId
type is"
They state that if a document lacks object id then it will be created:
In MongoDB, each document stored in a collection requires a unique _id field that acts as a primary key. If an inserted document omits the _id field, the MongoDB driver automatically generates an ObjectId for the _id field.
Wording was too implicit. What if i define my own objectId fields with different name, like parentID
? Will it use it as document id, or create additional "internal" _id
field.
I want to create tree-like structure in database, where each object - a document - must have its own id, plus it may have a parent, and an array of siblings (which could be either empty, or filled with some objectId
s).
I require tree-like navigation for mongoose in express app, so i thought Schema.findById()
methods are the best way to go - they provide object lookup, as well as type cast checking, and ensures objectId
is also being unique per document (not required custom-written hashing function)
So Is it safe to use multiple objectId
s in a single document in such a way?
Here is related question, it showed that I may use Schema.ObjectId
.