"not contains" query in SailsJS Waterline - node.js

How can I create the not contains query that returns true if the item does not contain a certain value. I'm running mongoDB if it is relevant.
I can use the contains successfully however receive an error when I introduce the not. I've tried it with find and where but get the same regex error both times. I can add the exact error if its relevant.
Working version:
model.find({attribute: {'contains': value}})
.exec(function(err, users) {
// happy code
});
The following complains about some regex error:
model.find({attribute: {not: {'contains': value}}})
.exec(function(err, users) {
// sad code
});
There was an issue raised and closed in 2013 about this. Maybe something changed recently?
https://github.com/balderdashy/waterline/issues/22

Alexei, I don't think this is supported by waterline currently. There is a related issue which is marked as feature and that you can track:
https://github.com/balderdashy/waterline/issues/666

So far, you can only use native feature of Mongodb I think.
Model.native(function(err, collection) {
collection.find({
"attribute" : {
$ne : value, //Not Equal
//OR
$nin : [value1, value2]// Not In
}
}).toArray(function(err, docs) {
if (err) return callback(err, docs);
res.json(null, docs);
});
});

Related

loopback destroyAll for couch db not working with query

I am trying out loopback-connector-couch database operator for deleting multiple records from CouchDB in one shot.
I have records _id in an array
Following the API spec for Loopback model as provided in the below api doc
https://apidocs.strongloop.com/loopback/#persistedmodel-destroyall
persist_model.destroyAll({id:{or:["49c199312b7dce75d69124f9e377a682","49c199312b7dce75d69124f9e377a682"]}},function(err,res){
console.log('records deleted success:'+res);
});
When executing the above code, it does not throw any error but its not deleting any documents
Would appreciate any help on this :)
The correct way is to use inq operator if you want to compare the value with multiple value stored in an array as follows:
persist_model.destroyAll({ id: { inq: [ "49c199312b7dce75d69124f9e377a682", "49c199312b7dce75d69124f9e377a682" ]}},
function(err, res) {
if (err) {
throw err;
}
console.log('records deleted success:' + res);
}
);
You can read more about inq operator here.

Mongoose update not working with embedded objects

I'm working in the MEAN stack, and I'm stuck on a Mongoose issue. Updates are working fine for individual documents(single key:value), but when I attempt to update a nested object with a new object, it's just deleting the original object and not inserting the new one.
Using an identical query directly in mongo, within the terminal, works perfectly. But from my Mongoose model, I get the above behavior.
My final desired query is more complicated and uses many variables, so I've simplified the code to the following to highlight the issue:
Setup.update({name: "main"}, {$set: {"schedule.sunday.eleven_pm": { associates: 111, supervisors: 111}}}, function(err){
if(err){
console.log(err);
}
else{
console.log('successfully updated main schedule setup')
Setup.find({}, function(err, setup){
if(err){
console.log(err);
}
else{
res.json(setup);
}
})
}
});
Gives me this in my db:
"eleven_pm" : {
}
But from terminal, the same query (cut and pasted from my mongoose query, just added 'db.setups' to beginning:
db.setups.update({name: "main"}, {$set: {"schedule.sunday.eleven_pm": { associates: 111, supervisors: 111}}})
Gives me this, the desired result:
"eleven_pm" : {
"associates" : 111,
"supervisors" : 111
}
I've tried writing this as a findOneAndUpdate(), but encountered the same behavior. Am I doing something unorthodox here?
Any help greatly appreciated.
A small change in my database structure was not amended in my Mongoose "Schema", and the difference between the two led to the unusual query behavior explained above.

Saving subdocuments with mongoose

I have this:
exports.deleteSlide = function(data,callback){
customers.findOne(data.query,{'files.$':1},function(err,data2){
if(data2){
console.log(data2.files[0]);
data2.files[0].slides.splice((data.slide-1),1);
data2.files[0].markModified('slides');
data2.save(function(err,product,numberAffected){
if(numberAffected==1){
console.log("manifest saved");
var back={success:true};
console.log(product.files[0]);
callback(back);
return;
}
});
}
});
}
I get the "manifest saved" message and a callback with success being true.
When I do the console.log when I first find the data, and compare it with the console.log after I save the data, it looks like what I expect. I don't get any errors.
However, when I look at the database after running this code, it looks like nothing was ever changed. The element that I should have deleted, still appears?
What's wrong here?
EDIT:
For my query, I do {'name':'some string','files.name':'some string'}, and if the object is found, I get an array of files with one object in it.
I guess this is a subdoc.
I've looked around and it says the rules for saving subdocs are different than saving the entire collection, or rather, the subdocs are only applied when the root object is saved.
I've been going around this by grabbing the entire root object, then I do loops to find the actual subdoc I that I want, and after I manipulate that, I save the whole object.
Can I avoid doing this?
I'd probably just switch to using native drivers for this query as it is much simpler. (For that matter, I recently dropped mongoose on my primary project and am happy with the speed improvements.)
You can find documentation on getting access to the native collection elsewhere.
Following advice here:
https://stackoverflow.com/a/4588909/68567
customersNative.update(data.query, {$unset : {"slides.1" : 1 }}, function(err){
if(err) { return callback(err); }
customersNative.findAndModify(data.query, [],
{$pull: {'slides' : null } }, {safe: true, 'new' : true}, function(err, updated) {
//'updated' has new object
} );
});

mongodb, get the result line after update

I'm working with mongodb, node.js and socket.io and I'm trying to reduce the number off access to the database. I need to update a line ; and after to return the updated line this is how I do :
db.collection('users').update({_id:targetID}, {$set: { 'property': 'value' }}, {safe:true}, function(err, result) {
db.collection('users').find({_id:targetID}).toArray(function(error, results){
//a socket.io resend the content
});
});
It works, but I really fell like I'm having a useless step here. The callback of the update function seems to be a boolean.
BTW, is there a better documentation than this one : http://docs.mongodb.org/manual/applications/update/ ? I'd like to find a list of properties and methods. The {safe:true} for instance. It seems not working without it but I can't find it in the reference.
Maybe I'm completely wrong and this is not the way I should do it. If you have a better idea... :)
You can use findAndModify to do this efficiently:
db.collection('users').findAndModify(
{_id: targetID}, [],
{$set: { 'property': 'value' }},
{new: true}, // Return the updated doc rather than the original
function(err, result) {
// result contains the updated document
}
);

Node.js MongoDB Upsert update

I'm writing a little application which scores keywords. So if "beirut" and "education" get entered in, if they haven't been seen before, I want to create a mongo entry, and give them a score of 1. If they have, I want to increment their score by one. I'm trying to do this with one update command, but I think I might be doing it wrong.
Ranking is the object representing the database
"key" is the keyword
rankingdb.update(
{keyword:key},
{keyword:key, {$inc:{score:1}}},
{upsert:true, safe:false},
function(err, data) {
if (err) {
console.log(err);
}
else {
console.log("score succeeded");
}
}
);
SyntaxError: Unexpected token {
Can you not create a brand new document with an increment?
Your general approach is right, but as the error message suggests, you've got a syntax problem in your code.
Try this instead:
rankingdb.update(
{keyword: key},
{$inc: {score: 1}},
{upsert: true, safe: false},
function(err,data){
if (err){
console.log(err);
}else{
console.log("score succeded");
}
}
);
When an upsert needs to create a new object it combines the fields from the selector (first parameter) and the update object (second parameter) when creating the object so you don't need to include the keyword field in both.
Note that update() is deprecated in the 2.0 driver, so you should now use either updateOne() or updateMany().

Resources