Amrut Gaikwad
Amrut Gaikwad

Reputation: 552

MongoDB query with multiple conditions

I have data with multiple documents :

{
 "_id" : ObjectId("57b68dbbc19c0bd86d62e486"),
 "empId" : "1"
 "type" : "WebUser",
 "city" : "Pune"
}
{
 "_id" : ObjectId("57b68dbbc19c0bd86d62e487"),
 "empId" : "2"
 "type" : "Admin",
 "city" : "Mumbai"
}
{
 "_id" : ObjectId("57b68dbbc19c0bd86d62e488"),
 "empId" : "3"
 "type" : "Admin",
 "city" : "Pune"
}
{
 "_id" : ObjectId("57b68dbbc19c0bd86d62e489"),
 "empId" : "4"
 "type" : "User",
 "city" : "Mumbai"
}

I want to get data according to my multiple conditions :

condition 1:- {"type" : "WebUser", "city" : "Pune"}

condition 2:- {"type" : "WebUser", "city" : "Pune"} & {"type" : "User", "city" : "Mumbai"}

I want below result when run condition 1 :

    {
     "_id" : ObjectId("57b68dbbc19c0bd86d62e486"),
     "empId" : "1"
     "type" : "WebUser",
     "city" : "Pune"
    }

When I run second condition :

{
  "_id" : ObjectId("57b68dbbc19c0bd86d62e486"),
  "empId" : "1"
  "type" : "WebUser",
  "city" : "Pune"
}
{
  "_id" : ObjectId("57b68dbbc19c0bd86d62e489"),
  "empId" : "4"
  "type" : "User",
  "city" : "Mumbai"
}

I want above result by one query,

Currently I am using below aggregate query,

 db.emp.aggregate([
     { $match: { '$and': [
         {"type" : "WebUser", "city" : "Pune"}, 
         {"type" : "User", "city" : "Mumbai"}
     ] } },
     { $group: { _id: 1, ids: { $push: "$empId" } } }
 ])

Above query work for first condition & fails for other. Please help me.

Upvotes: 27

Views: 194528

Answers (4)

Mr. Sukhdev
Mr. Sukhdev

Reputation: 21

  1. Display the document where in the “StudName” has value “Ajay Rathod”.

    db.Student.find({name:"ajay rathod"})

    { "_id" : ObjectId("5fdd895cd2d5a20ee8cea0de"), "
    
  2. Retrieve only Student Name and Grade.

    db.Student.find({},{name:1,grade:1,_id:0})

    { "name" : "dhruv", "grade" : "A" }
    { "name" : "jay", "grade" : "B" }
    { "name" : "abhi", "grade" : "C" }
    { "name" : "aayush", "grade" : "A" }
    { "name" : "sukhdev", "grade" : "B" }
    { "name" : "dhruval", "grade" : "B" }
    { "name" : "ajay rathod", "grade" : "D" }
    

Upvotes: 0

Ram
Ram

Reputation: 153

You can use mongo db $or operator.

    db.emp.find({ $or: [
      { "type": "WebUser", "city": "Pune" },
      { "type": "user", "city": "Mumbai"}
    ]})

You can pass conditions in the array.

For more reference see mongo docs

Upvotes: 13

Julesezaar
Julesezaar

Reputation: 3406

If you are looking for the AND operator

This example checks if a field exists AND is null

db.getCollection('TheCollection').find({
    $and: [
        {'the_key': { $exists: true }},
        {'the_key': null}
    ]
})

This example checks if a field has 'value1' OR 'value2'

db.getCollection('TheCollection').find({
    $or: [
        {'the_key': 'value1'},
        {`the_key': 'value2'}
    ]
})

When just checking for null, the return contains non-existing fields plus fields with value null

db.getCollection('TheCollection').find({'the_key': null})

Upvotes: 29

chridam
chridam

Reputation: 103445

For the second condition, you can use the $in operator in your query as:

db.emp.find({
    "type" : { "$in": ["WebUser", "User"] },
    "city" : { "$in": ["Pune", "Mumbai"] }
})

If you want to use in aggregation:

 db.emp.aggregate([
     { 
        "$match": {
            "type" : { "$in": ["WebUser", "User"] },
            "city" : { "$in": ["Pune", "Mumbai"] }
        }
     },
     { "$group": { "_id": null, "ids": { "$push": "$empId" } } }
 ])

or simply use the distinct() method to return an array of distinct empIds that match the above query as:

var employeeIds = db.emp.distinct("empId", {
    "type" : { "$in": ["WebUser", "User"] },
    "city" : { "$in": ["Pune", "Mumbai"] }
});

Upvotes: 34

Related Questions