1

I have a mongodb structure like.

The structure of the place model

{.....
  amenities: [
    {
      name: "wifi",
      is_free: true
    },
    {
      name: "projector",
      is_free: false,
      price: "50"
    },
    ...........
    ...........
    ...........
  ]
.....}

I want to do a query where, I want to search for a place which is having some set of amenities exist in the place irrespective of the price & is_free fields. How can I do it inside the mongodb / mongoose query.

I want list of places which are having set of amenities like wifi and projector as a output.

8
  • Your question is not clear. So what you have tried and expected result. Commented Jun 24, 2016 at 6:24
  • you want to find out those amenities who has price & is_free both Commented Jun 24, 2016 at 6:29
  • Price and is_free doesn't matter. I want the place which are having amenities like 'wifi' and 'projector'. Commented Jun 24, 2016 at 6:31
  • I have modified the question. Please have a look. @Shrabanee Commented Jun 24, 2016 at 6:33
  • 1
    Possible duplicate of How to search in array of object in mongodb Commented Jun 24, 2016 at 6:38

3 Answers 3

4
db.places.find({$and: [{'amenities.name': 'wifi'}, {'amenities.name':'projector'}])

It will find the places which have all amneties specified in an array ['wifi', 'projector'].

Hope it helps.

Sign up to request clarification or add additional context in comments.

5 Comments

not only having amenities. But it should have specific amenities. Your answer will not solve my problem
Find those places which have all ameneties specified in an array like: ['wifi', 'loudspeakers', 'phone']. Is it what you need?
Yes! That is what I want. But your query doesn't do that. It will search only amenities filed exists or not.
@AkhilP, the question was not explanatory enough. However, I have edited my answer. Please have a look if it helps.
I think it will work without $and also. Just use the two conditions two find.
1

Please try executing following query

db.places.find({
    amenities : {
        $exist : true
    },
    amenities : {
        $elemMatch : {
            name : {
                $in : ['wifi', 'projector']
            }
        }
    }
})

In above mentioned query $exists operator will check for existence of amenities field in document and $elemMatch operator will match amenities array containing name field with following values

(1) wifi

(2) projector

2 Comments

have in mind that this will return only first matched element elemMatch specific :-)
It is returning documents which are matching at least one amenity in the $in : ['wifi', 'projector']. But I want documents which must have bothe ['wifi', 'projector']
1

Try below query for one option of name:

db.places.find({amenities: {$exists: true}, 'amenities.name':'wifi'})

Also can try this query, if multiple options to check for name:-

var findQuery = {amenities: {$exists: true}, 'amenities.name':'wifi' , 'amenities.name':'projector'}

db.places.find(findQuery);

Hope this will help you.

2 Comments

It doesn't solve my problem. Please have a look at multiple options it is resulting an empty array. Even my database is having matches with the query.
Updated the query as per your requirement. I think your question is not clear enough to let someone help you in one go.

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.