Push values ​​to mongodb database array via (sails js) waterline

node js, sails js, waterline. I need to update (or click) the values ​​in the diagram below after inserting

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 the update

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

please help me.

+7
mongodb waterline
source share
2 answers

Great question! You will want to find() and then save() corresponding model instance:

 User.findOne(23).exec(function (err, user) { user.roles.push({ /* whatever */ }); user.save(function (err) { /* all done */ }); }); 

Made on my phone, so sorry for any typos :)

+16
source share

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

 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 request my data using the sessionData.id_ key

+3
source share

All Articles