How to update a specific field in mongodb sails

This code does not give any errors, but the update does not work. The update syntax works in mongodb. Usually I want to update certain fields based on some condition.

User.update(
    { ULId: "IC666" },
    {$set: {FHName: "Arijit Banerjee",Ward:"II"}}
).done(function(err) {
    if(err)
      res.end(err);     
    res.end('success');
});
+4
source share
2 answers

You are trying to use Mongo syntax with Waterline, but ORM has its own syntax. See documents for update method ; it takes two arguments: the criteria object "where" and the key / value object to change in the found instances. So you want:

User.update(
    // Find all users with ULId = IC666
    { ULId: "IC666" },
    // Update their FHName and Ward fields
    {FHName: "Arijit Banerjee",Ward:"II"}
).exec(function(err, users) {
    // In case of error, handle accordingly
    if(err) {return res.serverError(err);} 
    // Otherwise send a success message and a 200 status    
    return res.send('success');
});
+3
source

Sails.js Waterline ORM (Object-Relational-Mapper). SQL- ( ) ORM , , . .

, MongoDB, Waterline. Update Waterline. https://github.com/balderdashy/waterline-docs/blob/master/query-methods.md

.update( search criteria , values , [callback] )

update , , . .

:

User.update({ name: 'Walter Jr' }, { name: 'Flynn' })
.exec(function(err, users) {});

, , ORM Sails.js, . http://www.shanison.com/2014/07/11/sails-js-model-validation/

0

Source: https://habr.com/ru/post/1544868/


All Articles