1

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 objectIds).

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 objectIds in a single document in such a way?


Here is related question, it showed that I may use Schema.ObjectId.

xakepp35
  • 2,878
  • 7
  • 26
  • 54
  • yes it's safe. take a look at the mongoose docs. Its recommended. when you declare the type , use Schema.Type.ObjectId – ALPHA Jan 20 '19 at 03:01
  • @THEWOLF thanks, i tried, that workes. Array of [ObjectID] is also supported, i was able to do `$push`es into it – xakepp35 Jan 20 '19 at 03:19

0 Answers0