Friday 9 June 2017

How to include array of objects into Mongoose during save()?

I have an array of objects that need to be inserted into new document when doing Modal.save(). What will be the best practise to achieve this?

How can I insert this:

var array = [
  { "id" : "001", "itemName" : "itemName001" },
  { "id" : "002", "itemName" : "itemName002" },
]

into this:

var newItemCollection = new Collection ({
    collectionName: req.body.collectionName,
    items: [
         { id : array.id, itemName: array.itemName, itemDescription: "Foo was here..." }
    ]
});
newItemCollection.save();

So it will return something like this:

{
   "collectionName" : "Collection A",
   "items" : [
              { "id" : "001", "itemName" : "itemName001", "itemDescription" : "Foo was here..." },
              { "id" : "002", "itemName" : "itemName002", "itemDescription" : "Foo was here..." },
   ]
}



via S.K. Kong

No comments:

Post a Comment