先谢谢你的帮助我有两个收藏品,我想找到一个特定字段值的区别我试过$ match但它不起作用
实施例
收藏A:
/*1/
{
{ "Origin" : "xx",
"Destination" : "yy",
"Duration" : 180}
/*2/
.....
}
收集B
/*1/
{
{ "Origin" : "xz",
"Destination" : "yy",
"Duration" : 20,
"Departure time ": 21:00,
"Arrival time " : 21:20}
/*2/
....
}
我想获得两个集合之间的共同起源和目的地。预期产出:
/*1/
{
{ "Origin" : "xx",
"Destination" : "yy",
}
/*2/
.....
}
我尝试了这个,但它不起作用:
db.A.aggregate([
{ $lookup:
{ from: "B", localField: "origin", localField : "destination", foreignField: "origin", foreignField : "destination", as: "flight_docs" } } ])
答案 0 :(得分:1)
您的聚合存在一些问题。首先,区分大小写很重要。其次,你不应该有两个localFields和两个foreignFields。第二个字段覆盖第一个字段。我希望您的查询看起来像这样:
WorkItem.Fields["FieldName"]
哪会产生这样的结果(通过匹配Destination字段找到所有外国文档的本地文档):
db.stack.aggregate(
[{
$lookup: {
"from": "B",
"localField": "Destination",
"foreignField": "Destination",
"as": "flight_docs"
}
}]
);