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

寵の児 提交于 2019-12-21 04:46:08

问题


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.


回答1:


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




回答2:


I found that with Sails I could not use mikermcneil answer. I had to go native:

Runtestunit.native(function(err, runtestunit){
     runtestunit.find({sessionID : sessionData.id_}).toArray(function(err, results) {
         if (err) return res.serverError(err);
         runtestunit.update({ _id: results[0]._id },
           { $push: { screenshots: filename } },
           function(err, screenshots) {
           if(err) sails.log.err( err)
         else sails.log.info("Item pushed")
       })
    });
});

FYI I'm querying my data by a sessionData.id_ key



来源:https://stackoverflow.com/questions/18161056/push-values-into-array-of-mongodb-database-through-sails-js-waterline

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!