0

I have the following collection example:

{
  _id: 'asdfjklsfo',
  name: 'My name',
  sections: [
    {
      title: 'my title'
    },
    {
      title: 'second title'
    }
  ]
}

And I want all the sections to have a unique id so that I can easily add/remove sections.

How can I create a unique id for each section?

This is the current method I have to add a new section:

collection.findAndModify({id: id}, {$push:{sections:{title: 'New title'}}}, {new: true})

I'm using Monk as my DBI

Catfish
  • 18,876
  • 54
  • 209
  • 353

1 Answers1

0

I eventually found the answer in the Monk documentation under Casting.

users.id() // returns new generated ObjectID

In this case users is a collection from var users= db.get('users') so just calling .id() generates a unique id.

So in my case, I just had to do

collection.findAndModify({_id: id}, {$push:{sections:{section_id: collection.id(), name: name, description: description}}})
Catfish
  • 18,876
  • 54
  • 209
  • 353