我正在查询项目列表,只返回provider_cost_dict中包含给定id的项目。例如,如果我传递providerId = 10001,则只有provider_cost_dict中的条目与提供者id匹配的项才会返回。
如何修改代码,以便忽略所有与提供程序ID不匹配的提供程序代码?
这是我当前的代码:

var procedures = db.collection('procedures');

var query = {};
query['provider_cost_dict.' + req.query.providerId] = {$exists: true };

procedures.find({}).toArray(function(err, result) {
    // Send the result back via JSON.
    res.setHeader('Content-Type', 'application/json');
    res.send(JSON.stringify(result, null, 3));

});

以下是我的回答:
{
          "_id": "57c62cb53673aaf5f6beacf9",
          "natl_total_cost": 1274787840,
          "natl_average": 8338.487,
          "natl_report_count": 152880,
          "name": "COPD (WITH MAJOR COMPLICATIONS)",
          "provider_cost_dict": {
             "10001": {
                "report_count": 144,
                "total_cost": 957334,
                "average_cost": 6648.153
             },
             "10005": {
                "report_count": 200,
                "total_cost": 1321644,
                "average_cost": 6608.22
             },
             "10006": {
                "report_count": 214,
                "total_cost": 1345658,
                "average_cost": 6288.1216

如果我通过了10001,我怎么能让我的回报看起来像:
{
          "_id": "57c62cb53673aaf5f6beacf9",
          "natl_total_cost": 1274787840,
          "natl_average": 8338.487,
          "natl_report_count": 152880,
          "name": "COPD (WITH MAJOR COMPLICATIONS)",
          "provider_cost_dict": {
             "10001": {
                "report_count": 144,
                "total_cost": 957334,
                "average_cost": 6648.153
             }
}

最佳答案

您可以为查询指定一个projection,以便仅显示您所需的成本dict,如下所示

var query = { 'provider_cost_dict.10001': { $exists: true } };
var project = {
    'natl_total_cost': 1,
    'natl_average': 1,
    'natl_report_count': 1,
    'name': 1,
    'provider_cost_dict.10001': 1
};
procedures.find(query, project).toArray( ...

09-11 00:27