Mongoose optional search query parameters?

mongoose find
mongoose search query
mongoose select fields
express optional parameter
mongoose query builder
mongoose commands
mongoose find sort
mongoose operators

I have a following situation. I need to build a mongoose query, based on certain arguments if present.

I.e. if object like this is passed

{
    player: "nickname",
    action: "capture"
}

the following search is performed:

Entry.find({
    player: obj.player,
    action: obj.action
}).
    exec(function(err, res){
        console.log(res);
    });

If I need to exclude "action" from search if action is not in the object, what should I do? Using ternary operator like action: (obj.action) ? obj.action:null doesn't work, as would search entries in DB where action is null.

Build up your query object programmatically:

var query = {
    player: 'player'
};

if (obj.action) {
    query.action = obj.action;
}

Entry.find(query).exec(function(err, res){
    console.log(res);
});

Mongoose optional search query parameters?, build a mongoose query, based on certain arguments if present. I.e. if object like this is passed { player: "nickname", action: "capture" }. the following search is � Mongoose optional search query parameters? I have a following situation. I need to build a mongoose query, based on certain arguments if present.

In case someone encounters same question, here's how I solved it:

var query = {
    player: 'player'
};

Entry.find({
    player: query.player,
    action: (query.action) ? query.action:/.*/
}).
    exec(function(err, res){
        console.log(res);
    });

Query, I have a following situation.I need to build a mongoose query, based on certain arguments if present.I.e. if object like this is passed. Mongoose optional search query parameters? (3 answers) Closed 2 years ago. I'm trying to do a basic filter (search) on multiple fields, and would like to ask how to

With ES6, you can do this with a splat & ternary like this:

Entry.find({
  player: obj.player,
  ...obj.action ? { action: obj.action } : {}
})
.exec(function(err, res){
    console.log(res);
});

Mongoose optional search query parameters?, This question already has an answer here: Mongoose optional search query parameters? 2 answers I'm trying to do a basic filter (search) on multiple fields, and� It is quite similar to the replaceOne() method. But there is a difference in options. In both of these methods, we can provide optional parameters. Here lies the difference between the two methods. In this article, we will discuss more about the findOneAndReplace() method in mongoose. We will use the details collection.

Mongoose filtering (search) on multiple fields [duplicate], A short post on how to query the database when the search field and String, test_2: String }); module.exports = mongoose.model('Student',� Getter/setter around the current mongoose-specific options for this query Below are the current Mongoose-specific options. populate: an array representing what paths will be populated. Should have one entry for each call to Query.prototype.populate() lean: if truthy, Mongoose will not hydrate any

How to build dynamic mongoose queries, Convert url query string to MongooseJs friendly query object including predefined : object for predefined queries/string templates [optional]� Mongoose actually has two find () functions. The above standalone example uses Model.find (), but there's also Query#find (). Query#find () is shorthand for Query.prototype.find (), find () is an instance method on the Query class. The Model.find () function returns an instance of Mongoose's Query class.

mongoose-query-parser, Contribute to feathersjs-ecosystem/feathers-mongoose development by creating an account on GitHub. and max page size; whitelist (optional) - A list of additional query parameters to Example: Find records with a case-insensitive search. A mongoose query can be executed in one of two ways. First, if you pass in a callback function, Mongoose will execute the query asynchronously and pass the results to the callback . A query also has a .then() function, and thus can be used as a promise.

Comments
  • Wow, didn't even think of that. Thanks a lot!
  • Does the order of fields in the query so created and the Entry model matter?
  • @JohnnyHK thanks.. and do you know if I want to search values greater than a particular range in this query-say there is a field called "budget" and we need to find all budgets greater than the given value?
  • A downside to this approach is that using the regex will hurt query performance. It's better to just omit the field from the query.
  • @JohnnyHK that's correct. 4 years on with ES6, you can avoid the regex with a splat & ternary