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:
- guaranteed unique
- contains timestamp of creation
- 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
Post a Comment