Create an Index for a document inside array inside document in MongoDB

Let’s say I have this document: { "_id": objectId(), "products": [ { "product_id": "" } ] } I would like to make that the key product_id of documents in the array products must be unique for that document in the collection, (i don’t want it to affect other documents in the collection). I tried to… Read More Create an Index for a document inside array inside document in MongoDB

How to generate unique id for each element of an array field in MongoDB

How to create a unique ID for each element of an array field, where uniqueness is maintained globally for all documents of the collection? Is it possible to specify create a unique index for this field? >Solution : You can make use of ObjectId data type. ObjectIds are 12-byte values that are guaranteed to be… Read More How to generate unique id for each element of an array field in MongoDB