27

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.

4 Answers 4

34

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"] }
});
Sign up to request clarification or add additional context in comments.

2 Comments

Above query work as "OR" condition. I need "AND". i.e. Result only return on exact match of both condition.
The comma operator behaves as an $and implicity.
29

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})

2 Comments

-1 db.getCollection('TheCollection').find({ 'the_key': 'value1', 'the_key': 'value2' }) will evaluate to db.getCollection('TheCollection').find({ 'the_key': 'value2' }) because javascript will not allow an object with two keys using same name. Also, mongoDB will evaluate multiple parameters as AND logic, not OR.
I thought I tested and it worked but anyway. I edited it. Thanks for pointing it out
13

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

Comments

0
  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" }
    

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.