Push values into array of mongodb database through (sails js) waterline

Shiva Gouraram picture Shiva Gouraram · Aug 10, 2013 · Viewed 8.2k times · Source

node js,sails js,waterline. I need to update(or push) values into the below schema after insert

I am using sailsjs with waterline and mongodb.

{
"countries": {
"states": [
{
"statename": "state",
"districts": [
{
"distname": "district",
"cities": [
{
"cityname": "Hyderabad",
"places": [
                {
                  "placename": "hitechcity"
                }
              ]

          }
        ]
      }
    ]
  }
]
}
}

I need to know how to update it i need something like this after update

{
"countries": {
"states": [
{
"statename": "state",
"districts": [
{
"distname": "district",
"cities": [
{
"cityname": "Hyderabad",

              "places": [
                {
                  "placename": "hitechcity"
                },
                {
                  "placename": "someother place"
                }
              ]

          }
        ]
      }
    ]
  }
]
}
}

please someone help me.

Answer

mikermcneil picture mikermcneil · Aug 11, 2013

Great question! You'll want to use addToCollection():

await User.addToCollection(23, 'roles')
.members([3, 5, 6]);

Done on my phone so sorry about any typos :)

Edited Aug 7, 2018 to reflect best practices in Sails v1. More info: https://sailsjs.com/documentation/reference/waterline-orm/models/add-to-collection