kevinblanco
kevinblanco

Reputation: 779

Update values into array of a database collection

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

Upvotes: 0

Views: 1194

Answers (1)

sgress454
sgress454

Reputation: 24948

Note that Sails v0.10 (currently in beta) supports associations, which would allow you to create a separate Photo model and associate it with User, giving each user an associated photo collection without needing embedded documents. You can install v0.10 with npm install sails@beta, and docs are here.

Having said that, you can't do exactly what you want with a single Sails method call. In fact, you can't even do it with a single MongoDB call. What you can do is:

User.findOne(req.param('user-id')).exec(function(err, user) {

    // Handle errors
    if (err) {return res.serverError(err);}
    if (!user) {return res.notFound();}

    // Set the specified photos "selected" key to true
    user.photos.forEach(function(photo) {
        // If the photo's ID is in the array that was sent, set
        // its "selected" key to "true"
        if (req.param('photo-ids').indexOf(photo.id) !== -1) {
            photo.selected = true;
        }
    });

    // Save the updated user
    user.save(function(err) {
        if (err) {return res.serverError(err);}

        // Return the updated record
        return res.json(user); 
    });

});

This uses Sails best practices to loop through the array of photos, update those that need updating, and save the result.

Upvotes: 1

Related Questions