我有两个收藏品如下。
集合1:
{
"_id" : ObjectId("5b618589d6edcc135c1aee0a"),
"name" : "ABC",
"special" : "Golmal",
"dim" : "2122",
}
集合2:
文件1:
{
"_id" : ObjectId("5b6023d9589ff6bfb5dd75d7"),
"date" : "2018/08/13",
"special" : "Golmal",
}
{
"_id" : ObjectId("5b602447589ff6bfb5dd7606"),
"date" : "2018/08/13",
"special" : "Godman",
}
在这里我想比较一下
"special":"Golmal"
集合1中存在集合2文档,我要从集合1文档中显示/获取所有相关日期。
aggregate
&安培;
$lookup
getAvailableList(): Promise<any> {
return this._mongoUtility.Db
.then(db => {
let list= db.collection('list');
let data= db.collection('data');
list.aggregate([{$lookup:
{
from: data,
localField: "special",
foreignField: "special",
as: "matches"
}
},
{
$match: { "matches": { $ne: [{}] } }
}
]);
return Promise.resolve();
})
.catch(err => {
return Promise.reject({message: 'Not found', err: err});
});
}
但我要失去理智了。
.find
&安培;
forEach
let abc = [];
list.find({}).toArray()
.then(arr => {
arr.forEach(obj1 => {
data.find({special: obj1.special}).toArray()
.then(secondArr => {
console.log(secondArr);
if (secondArr.length > 0) {
abc.push(obj1);
}
});
});
});
return Promise.resolve();
但没有结果。我需要JSON格式的值。因为MongoDB没有连接。有什么办法可以破解吗?