Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mongoose "Find" with multiple conditions

I am trying to get data from my mongoDB database by using mongoose filters. The scenario is that each user object in the database has certain fields like "Region" or "Sector".

Currently I am getting all the users that contain the keyword "region" in there object like so:

 // Filter all healthcare bios by region
 app.get('/user',function(req, res) {

 // use mongoose to get all users in the database
 User.find({region: "NA"}, function(err, user) 
 {
    // if there is an error retrieving, send the error. nothing after res.send(err) will execute
    if (err)
    {
        res.send(err);
    }
    // return all todos in JSON format
    console.log(user);
    res.json(user);

});
});

How can put some conditions in mongoose that it return users that contain both "region" && "Sector" in their objects. Currently its only returning the user which have the region keyword in them.

I have tried using $and operator but I couldn't get it to work.

like image 941
Skywalker Avatar asked Nov 10 '15 09:11

Skywalker


People also ask

How do I apply multiple conditions in MongoDB?

In MongoDB, we can apply the multiple conditions using the and operator. By applying the and operation we will select all the documents that satisfy all the condition expressions. We can use this operator in methods like find(), update() , etc as per the requirement.

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.

Which method of a Mongoose model can be used to find out how many documents are in a given collection?

Model. count() method is deprecated in mongoose version 6.2. 0. If you want to count the number of documents in a collection, e.g. count({}) , use the estimatedDocumentCount() function instead.

What problem does Mongoose solve?

The problem that Mongoose aims to solve is allowing developers to enforce a specific schema at the application layer. In addition to enforcing a schema, Mongoose also offers a variety of hooks, model validation, and other features aimed at making it easier to work with MongoDB.


3 Answers

app.get('/user',function(req, res) {

 User.find({region: "NA",sector:"Some Sector"}, function(err, user) 
 {
    if (err)
    {
        res.send(err);
    }
    console.log(user);
    res.json(user);

 });
});

If you want data with either region:"NA" or sector:"Some Sector". you can use $or operator.

User.find({$or:[{region: "NA"},{sector:"Some Sector"}]}, function(err, user) 
 {
    if (err)
    {
        res.send(err);
    }
    console.log(user);
    res.json(user);

 });
like image 180
Vishnu Avatar answered Oct 17 '22 04:10

Vishnu


If you want results that contain any region or sector as long as both are present at the same time you need the following query in your User.find: {region: {$exists:true},sector: {$exists:true}}

, is the equivalent of $and as long as you are searching different fields.

like image 10
Ron Wertlen Avatar answered Oct 17 '22 03:10

Ron Wertlen


  const dateBetweenDates = await Model.find({
    $or: [
      {
    $and: [
      { From: { $gte: DateFrom } },
      { To: { $lte: DateTo } },
    ],    // and operator body finishes
    },
      { _id: req.user.id},
    ], //Or operator body finishes
  })
like image 4
Qamar Sandhu Avatar answered Oct 17 '22 05:10

Qamar Sandhu