mongodb javascript:返回仅包含匹配子文档的文档

mongodb javascript:返回仅包含匹配子文档的文档,javascript,mongodb,Javascript,Mongodb,有人知道如何在javascript中返回只包含匹配子文档的文档吗 e、 g.以下是数据库记录: [ {"name":"bestbuy",notes:["article IT", "article 2"]}, {"name":"microsoft",notes:["article IT", "another IT", "article 5"]}, {"name":"IBM",notes:["article 8", "article 9"]} ] 我的问题是: collection.f

有人知道如何在javascript中返回只包含匹配子文档的文档吗

e、 g.以下是数据库记录:

[
  {"name":"bestbuy",notes:["article IT", "article 2"]},
  {"name":"microsoft",notes:["article IT", "another IT", "article 5"]},
  {"name":"IBM",notes:["article 8", "article 9"]}
]
我的问题是:

collection.find({"company.notes":/IT/}, function(err,result){})
结果是:

[
  {"name":"bestbuy",notes:["article IT", "article 2"]},
  {"name":"microsoft",notes:["article IT", "another IT", "article 5"]},
]
[
  {"name":"bestbuy",notes:["article IT"]},
  {"name":"microsoft",notes:["article IT", "another IT"]}
]
但我的预期结果是:

[
  {"name":"bestbuy",notes:["article IT", "article 2"]},
  {"name":"microsoft",notes:["article IT", "another IT", "article 5"]},
]
[
  {"name":"bestbuy",notes:["article IT"]},
  {"name":"microsoft",notes:["article IT", "another IT"]}
]
有什么想法吗? 谢谢

您可以使用聚合

收益率:

{ "name" : "microsoft", "notes" : [ "article IT", "another IT" ] }
{ "name" : "bestbuy", "notes" : [ "article IT" ] }

可能是重复的谢谢,这是更具体的javascript我想谢谢,我没有尝试,因为我发现数据库模式是不同的,所以我更新了我的问题。你能解释一下吗?还有一个问题:为什么需要使用2“$match”s?@aaron这是对你第一个问题的回答。如果您的db模式不同,请接受答案并提出新问题。你不应该提出问题。然后我尝试了,代码抛出了一个令人头痛的问题。。输出为:集合已打开D:\Test\node\u modules\mongodb\lib\mongodb\connection\base.js:246 throw message;^TypeError:object不是位于D:\Test\node\u modules\mongodb\lib\mongodb\collection\aggregation.js:317:7var results=collection.aggregate([{$match:{“notes”:/IT/}},{$undend:$notes”},{$match:{notes:{IT/},{$group:{id:'$\u id',name:{$first:'$name},notes:{$push:'$notes'}},{$project:{'name':1,'notes':1,_id:0}}]);