首先也是最重要的是,我已经很长时间没有做开发了,更不用说使用Mongo了,但我还没有找到一个解决方案来解决一个可能很简单的问题.
在上下文中,我正在try 获取嵌套对象的ID以备将来使用.客户集合如下所示:
{
"_id": ObjectId("1223334abc"),
"mainNumber": "123",
"notificationAccount" : [
"channel": "Apple",
"notificationAccountId": "700002",
]
}
我有一个包含mainNumbers
个帐户的数组,并试图将所有匹配的帐户放入一个空数组中.所以现在看起来是这样的:
mainNumbers = ["123", "234", "345", etc]
accountIds = [];
for(mainNumber of mainNumbers){
customerProfile = db.getCollection("customers").find({mainNumber})
if (customerProfile != null {
customerProfile.map(function(singleCustomerProfile){
accountIds.push(singleCustomerProfile.notificationAccount.notificationAccountId);
print('${mainNumber} ${singleCustomerProfile.notificationAccount.notificationAccountId}');
});
} else }
print(''${mainNumber} was not found.);
}
}
这将打印出所有mainNumbers
个没有身份证.所以我拿出打印行,所以:
customerProfile.map(function(singleCustomerProfile){
accountIds.push(singleCustomerProfile.notficiationAccount.notificationAccountId);
});
print('${mainNumber} - ${accountIds}')
没有错误,但我只是一遍又一遍地得到mainNumber
分.
我感觉像是:
- 我错过了一些简单的对
notificationAccount
和我的try 是徒劳的 - 可能有一种更好/更干净的方法来在查询时获得数组的DO输出.
欢迎任何帮助.