Tech Nerd
Tech Nerd

Reputation: 93

Finding an object with multiple items in mongoose

I am having certain issue with querying mongodb using mongoose, but unable to find my required output. Below is the data I am looking for the solution.

I am trying to find with two params here one is location and second is type

{
    "_id" : ObjectId("5f04dcf8e123292518a863ae"),
    "location" : "Australia",
    "name" : "June Bill",
    "filters" : [ 
        {
            "_id" : ObjectId("5f047efe9fc7ad000f44f990"),
            "name" : "Q1",
            "type" : "Platinum",
            "conditions" : [ 
                {
                    "And" : [ 
                        {
                            "_id" : "objectid",
                            "field" : "location",
                            "value" : "Australia",
                            "operator" : "equal"
                        },
                        {
                            "_id" : "objectid",
                            "field" : "name",
                            "value" : "admin",
                            "operator" : "equal"
                        }
                    ]
                }
            ]
        }, 
        {
            "_id" : ObjectId("5f04d51c0ce40120bbb7dc6e"),
            "name" : "Q2",
            "type" : "Gold",
            "conditions" : [ 
                {
                    "_id" : ObjectId("5f04d51c0ce40120bbb7dc6f"),
                    "field" : "ocation",
                    "value" : "Australia",
                    "operator" : "equal"
                }, 
                {
                    "_id" : ObjectId("5f04d51c0ce40120bbb7dc70"),
                    "field" : "start_date",
                    "value" : "2020-06-01T00 3A00",
                    "operator" : "equal"
                },
            ]
        }
    ],
    "__v" : 0
},

{
    "_id" : ObjectId("5f04dcf8e123292518a863ae"),
    "location" : "Brazil",
    "name" : "June Bill",
    "filters" : [ 
        {
            "_id" : ObjectId("5f047efe9fc7ad000f44f990"),
            "name" : "Q1",
            "type" : "Silver",
            "conditions" : [ 
                {
                    "And" : [ 
                        {
                            "_id" : "objectid",
                            "field" : "location",
                            "value" : "Australia",
                            "operator" : "equal"
                        },
                        {
                            "_id" : "objectid",
                            "field" : "name",
                            "value" : "admin",
                            "operator" : "equal"
                        }
                    ]
                }
            ]
        }, 
        {
            "_id" : ObjectId("5f04d51c0ce40120bbb7dc6e"),
            "name" : "Q2",
            "type" : "Gold",
            "conditions" : [ 
                {
                    "_id" : ObjectId("5f04d51c0ce40120bbb7dc6f"),
                    "field" : "location",
                    "value" : "Australia",
                    "operator" : "equal"
                }, 
                {
                    "_id" : ObjectId("5f04d51c0ce40120bbb7dc70"),
                    "field" : "start_date",
                    "value" : "2020-06-01T00 3A00",
                    "operator" : "equal"
                },
            ]
        }
    ],
    "__v" : 0
}

here I am trying to find with location and in filters with its type How Can i do this in mongoose?

I tried

Model.find({'location': 'Brazil', 'filters':{ "$in" : ["Silver"]} });

But it didn't work, Can any one help me achieving actual result?

Upvotes: 0

Views: 99

Answers (2)

Talha Noyon
Talha Noyon

Reputation: 854

Use this way:

db.collection.find({
  "location": "Brazil",
  "filters.type": "Silver"
})

MongoDb Playground

Upvotes: 0

namar sood
namar sood

Reputation: 1590

You can use the . to query the embedded filed or if you want to match multiple fields you can use $elemMatch

db.collection.find({
  "location": "Brazil",
  "filters.type": {
    "$in": [
      "Silver"
    ]
  }
},
{
  "filters.$": 1
})

MongoDB Playground

If you want to filter out the matched result you can use $ operator in projection

Upvotes: 1

Related Questions