python - Count all the documents in MongoDB containing specific item in array -


i new mongodb , try understand how can have access , count instances of documents in database have 'name1' (in array of names). should use - aggregation, find? tried failed time, can't find proper query it. example of document below. i'm using pymongo connection driver.

{      '_id':objectid('599983d47fec3323a8526d74'),    'name':[         {            'name1':{               'params':[                  {                     'a':'b'                }             ]          },          {               'name2':{                  'params':[                     {                        'a':'b'                   }                ]             }          ]       } 

your data model severely flawed. key should never used value force weird stuff in code.

hence, more appropriate data model be

{    "_id": new objectid(),    "names": [      {        "name": "name1",         "params": {            "a":"b",            "foo":"bar"          }        }      ]    } 

querying model becomes breeze

db.yourcoll.find({"names.name":"name1"}) 

note: there no point in making params array. can access individual parameters via object properties of params object.


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -