Eric
Eric

Reputation: 10646

Can we and should we create MongoID for nested objects inside document?

We have a collection of documents, each document has an array of objects

 {
      "_id":_MONGO_ID_,
      "property":"value",
      "list":[{...}, {...},  ...]
 }

But each object of the list also needs a unique id for the needs of our app.

 {"id":213456789, "somestuff":"somevlue" ...}

We do not wish to create a collection for these objects because they are small and would rather store them straight into the document.

Now the question. Right now we generate a unique id based on time which looks like the MongoID. We need an id to make it easier to target each object. Would it be a good idea to generate a MongoID for each object of the list instead? Any pros and cons?

Upvotes: 0

Views: 154

Answers (1)

Buzz Moschetti
Buzz Moschetti

Reputation: 7578

In general, it is wise to separate DB-specific resources from business/data domain resources. You always want to be able to manipulate the data completely independent of the host database and the drivers associated therewith. ObjectId() is relatively lightweight and in fact a BSON type, separate from the MongoDB core objects, but for true arms-length separation and an easier physical implementation, I would recommend a simple string instead. If you don't have extreme space/scale issues, UUIDv4 is good way to get a unique string.

Upvotes: 1

Related Questions