mongodb 3.2 中集合之间的差异
difference between collections in mongodb 3.2
预先感谢您的帮助我有两个集合,我想找出特定字段的一组值之间的差异我尝试了 $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" } } ])
您的汇总存在一些问题。首先,区分大小写很重要。其次,你不应该有两个 localFields 和两个 foreignFields。第二个字段覆盖第一个字段。我希望您的查询看起来像这样:
db.stack.aggregate(
[{
$lookup: {
"from": "B",
"localField": "Destination",
"foreignField": "Destination",
"as": "flight_docs"
}
}]
);
这会给出这样的结果(通过匹配目标字段找到的包含所有外国文档的本地文档):
{
"_id" : ObjectId("571546c7b9c99e9977f7d372"),
"Origin" : "xx",
"Destination" : "yy",
"Duration" : NumberInt(180),
"flight_docs" : [
{
"_id" : ObjectId("5715470eb9c99e9977f7d376"),
"Origin" : "xz",
"Destination" : "yy",
"Duration" : NumberInt(20)
}
]
}
预先感谢您的帮助我有两个集合,我想找出特定字段的一组值之间的差异我尝试了 $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" } } ])
您的汇总存在一些问题。首先,区分大小写很重要。其次,你不应该有两个 localFields 和两个 foreignFields。第二个字段覆盖第一个字段。我希望您的查询看起来像这样:
db.stack.aggregate(
[{
$lookup: {
"from": "B",
"localField": "Destination",
"foreignField": "Destination",
"as": "flight_docs"
}
}]
);
这会给出这样的结果(通过匹配目标字段找到的包含所有外国文档的本地文档):
{
"_id" : ObjectId("571546c7b9c99e9977f7d372"),
"Origin" : "xx",
"Destination" : "yy",
"Duration" : NumberInt(180),
"flight_docs" : [
{
"_id" : ObjectId("5715470eb9c99e9977f7d376"),
"Origin" : "xz",
"Destination" : "yy",
"Duration" : NumberInt(20)
}
]
}