mongodb - Create unique indexes for document's objects stored in an array -


how 1 create unique indexes document's objects stored in array?

{ _id: 'documentid', books: [   {     unique_id: 1,     title: 'asd',   },   {     unique_id: 2,     title: 'wsad',   }   ...  ]  } 

one thing can think of autoincrementing. or there mongo way so?

if remove _id field doc, mongo automatically add 1 you, is:

  1. guaranteed unique
  2. contains timestamp of creation
  3. lots of other features.

see here: https://docs.mongodb.com/v3.2/reference/method/objectid/

looking @ example object again, referring ids in books array? if so, can assign them objectids well, in document root's _id field:

doc.books.foreach(x => { x.unique_id = new objectid() } ); 

Comments

Popular posts from this blog

account - Script error login visual studio DefaultLogin_PCore.js -

xcode - CocoaPod Storyboard error: -