Milan Mangar
Milan Mangar

Reputation: 41

mongodb query issue for contructing where clause

I am completely new to mongoDB,I have this below query:

jds=jd.aggregate( [
   {
     "$group": {
        "_id": {"house_NAME":"$house_NAME"},
        "count": { "$sum": 1 }
     }
   },
   { "$match": { "count": { "$gt": 0 } } }
   ] )

which returns count of each house name present in the collection.

my collection is somewhat like below :

record_id  house_NAME  status

1          Thomas    Open
2          Panther   Close
3          Thomas    Close

what I want is to only return the value whose status is "Open", I want to add "and" clause in above query so it return the count of only those documents whose status "Open". I don't know how exactly to do it.

I am stucked in it .any help will be greatly appreciated !

Thanks in advance !

Upvotes: 2

Views: 46

Answers (1)

Ashh
Ashh

Reputation: 46491

You can add a $match stage at the start of the pipeline

jds=jd.aggregate([
  { "$match": { "status": "Open" }},
  { "$group": {
    "_id": { "house_NAME": "$house_NAME" },
    "count": { "$sum": 1 }
  }},
 { "$match": { "count": { "$gt": 0 }}}
])

Upvotes: 2

Related Questions