简体   繁体   中英

Query subdocuments MongoDB Node.JS

I have a collection of objects which have a sub dictionary which contains data for a given institution, identified by the provider_id .

How can I query a collection, products and return only produce which contain a given provider id?

  "natl_total_cost": 1.27478784E9,
  "natl_average": 8338.487,
  "natl_report_count": 152880,
  "name": "Wax",
  "provider_cost_dict": {
     "340008": {
        "report_count": 181,
        "total_cost": 1335465,
        "average_cost": 7378.26
     },
     "340001": {
        "report_count": 643,
        "total_cost": 5026724,
        "average_cost": 7817.6113
     },
...

How could I query all of the products so that it only returns products which contain a given provider id in their provider_cost_dict ?

您可以像这样使用$ exists运算符

db.getCollection('yourColl').find({"provider_cost_dict.340008": { $exists: true }})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM