Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do greater than syntax in a Mongoose.js query

How do I get a 'greater than' syntax to work for this Mongoose query?

var where = {};
where._id = req.wine.id;
where.sameAs = undefined;
where.scoreTotal > 0; //THIS NEEDS TO SET TO DO GREATER THAN 0
where.mode = 'group';
where.deleted = false;
Wine.find(where, callback).populate('user');

It keeps crashing my node server.

I'd like to keep this where object syntax instead of doing the inline where object syntax for readability sake. Could I do something like:

where.scoreTotal = $gt(0);
like image 663
tonejac Avatar asked Mar 09 '16 04:03

tonejac


People also ask

How do you do greater than in MongoDB?

MongoDB provides different types of comparison operators and greater than equals to operator($gte) is one of them. This operator is used to select those documents where the value of the field is greater than equals to(>=) the given value. You can use this operator in methods (like, find(), update(), etc.)

What is $GTE in Mongoose?

$gte selects the documents where the value of the field is greater than or equal to (i.e. >= ) a specified value (e.g. value .)

What is $in in Mongoose?

The value of the $in operator is an array that contains few values. The document will be matched where the value of the breed field matches any one of the values inside the array.

What does findOne return Mongoose?

Mongoose | findOne() Function The findOne() function is used to find one document according to the condition. If multiple documents match the condition, then it returns the first document satisfying the condition.


1 Answers

You can use query like

Person.
  find({
    occupation: /host/,
    'name.last': 'Ghost',
    age: { $gt: 17, $lt: 66 },
    likes: { $in: ['vaporizing', 'talking'] }
  }).
  limit(10).
  sort({ occupation: -1 }).
  select({ name: 1, occupation: 1 }).
  exec(callback);

or using query builder

Person.
  find({ occupation: /host/ }).
  where('name.last').equals('Ghost').
  where('age').gt(17).lt(66).
  where('likes').in(['vaporizing', 'talking']).
  limit(10).
  sort('-occupation').
  select('name occupation').
  exec(callback);
like image 131
codeGig Avatar answered Sep 30 '22 17:09

codeGig