Reputation: 85
This code gives no error but update does not work .Update syntax works in mongodb .I generally want to update specific 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');
});
Upvotes: 4
Views: 5627
Reputation: 2295
Sails.js use Waterline as their ORM (Object-Relational-Mapper). So instead of using raw SQL statements (which maybe different based on which underlying database you choose), ORM provides an abstraction layer on top of the database and you can easily query, create or update records with the same source code. And it will be mapped to the underlying database statements by the different adapters.
So you don't have two write MongoDB specific queries, and you should use Waterline syntax. Refer to the Update method in the Waterline documentation. https://github.com/balderdashy/waterline-docs/blob/master/query-methods.md
.update( search criteria , values , [callback] )
update will attempt to update any records matching the criteria passed in. Criteria can be built using the Query Language.
Example:
User.update({ name: 'Walter Jr' }, { name: 'Flynn' })
.exec(function(err, users) {});
Here is an article that give you better understanding on what is ORM and how Sails.js model validation methods works, which may be useful to you. http://www.shanison.com/2014/07/11/sails-js-model-validation/
Upvotes: 0
Reputation: 24948
You're trying to use Mongo syntax with Waterline, but the ORM has its own syntax. See the docs for the Update method; it takes two arguments: an object of "where" criteria, and an object of keys/values to change in the found instances. So what you want is:
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');
});
Upvotes: 3