我想在用户id和receivedData公司的基础上查询mongoose中的条件来查找/填充子文档。但是当我运行查询时,它会获取所有记录,但它只能获取一条记录。下面是模型和代码。在receivedData数据和companyid中是对数据和公司模型的两个引用。
用户模型:
Delta1.5_DOS_2D_TBM.data
}
Node.js代码:
{
"_id" : ObjectId("5534306515a734345d550c29"),
"password" : "123456789",
"email" : "abc@gmail.com",
"receivedData" : [
{
"from" : ObjectId("5535357e95ce31e04a994e2a"),
"data" : ObjectId("553549abea87c000454fa623"),
"companyid" : ObjectId("54d90293ec135f35379b53b7"),
"_id" : ObjectId("5535556fea87c000454fa626"),
"sent" : ISODate("2015-04-20T19:37:19.460Z")
},
{
"from" : ObjectId("5535357e95ce31e04a994e2a"),
"data" : ObjectId("55354f73ea87c000454fa624"),
"companyid" : ObjectId("53feeaa2e4c1087e53bf4a2a"),
"_id" : ObjectId("55f68911498231e434df53cd"),
"sent" : ISODate("2015-09-14T08:45:05.139Z")
}
],
以下是显示的输出
models.User.find({_id: mongoose.Types.ObjectId('5534306515a734345d550c29'), receivedData: {$elemMatch: {companyid: mongoose.Types.ObjectId('53feeaa2e4c1087e53bf4a2a')}}})
.populate('receivedData.data receivedData.from')
.select('receivedData.data receivedData.from receivedData.companyid')
.exec(function (err, current_user) {
console.log(current_user);
})
但我希望以下输出有一条记录:
[ { _id: 5534306515a734345d550c29,
receivedData:
[ { from: 5535357e95ce31e04a994e2a,
data: 553549abea87c000454fa623,
companyid: 54d90293ec135f35379b53b7 },
{ from: 5535357e95ce31e04a994e2a,
data: 55354f73ea87c000454fa624,
companyid: 53feeaa2e4c1087e53bf4a2a } ]
我不知道它为什么显示两条记录。非常感谢任何帮助。