I'm building a web app with Node.js using Sails.js framework and I need to update some fields on an array I have on my DB collection(MongoDB).
Here's my User model:
attributes: {
name : {
type : 'string'
},
encryptedPassword : {
type : 'string'
},
email: {
type : 'string',
email : true
},
photos : {
type: 'array'
}
On the photos array i'm adding user's photos information like the image URL, image name, and a field called selected which means if the user has selected this picture or not. Here's an example of a collection:
"photos" : [
{
"id" : "94036c20b12711e3abdf9162d9e75321",
"url" : "http://tmp.transloadit.com.s3.amazonaws.com/94036c20b12711e3abdf9162d9e75321.jpg",
"selected" : false
},
{
"id" : "9d5aa090b12711e3a4bb83478bef61eb",
"url" : "http://tmp.transloadit.com.s3.amazonaws.com/9d5aa090b12711e3a4bb83478bef61eb.jpg",
"selected" : false
}
]
And on a view, i'm rendering this photos and each photo has a checkbox input, so basically the idea is that the user goes through all the photos and select those he/she want, and after the form get submitted I receive the data of the photos selected and update the selected field to true
Here's my response object when I console.log(req.body) :
{
"photos-ids":[
"94036c20b12711e3abdf9162d9e75321",
"9d5aa090b12711e3a4bb83478bef61eb",
"ad65d5e0b12711e38644f5b433a44603"
],
"user-id":"532c86a3f0fd88560792b3dd"
}
So last, basically what I need to do is update the selected field to true on the photos that has the IDs from the photos-ids array of the user collection (user ID is the user-id field) that I received on the response. Any help on this? I would appreciate it
I know it's something like:
User.findOne(body.req.user-id).exec(function (err, user) {
/* update records here */
});
But to be honest I haven't found the way to get it.
Thanks