Reputation: 1335
Thank you in advance for your help I have two collections and I want to find the difference between a set of values of a specific field I tried $match but it doesn't work
Example
Collection A :
/*1/
{
{ "Origin" : "xx",
"Destination" : "yy",
"Duration" : 180}
/*2/
.....
}
Collection B
/*1/
{
{ "Origin" : "xz",
"Destination" : "yy",
"Duration" : 20,
"Departure time ": 21:00,
"Arrival time " : 21:20}
/*2/
....
}
I want to get the common origin and destination between the two collections. Expected output:
/*1/
{
{ "Origin" : "xx",
"Destination" : "yy",
}
/*2/
.....
}
I tried this but it doesn't work :
db.A.aggregate([
{ $lookup:
{ from: "B", localField: "origin", localField : "destination", foreignField: "origin", foreignField : "destination", as: "flight_docs" } } ])
Upvotes: 0
Views: 292
Reputation: 1273
There a couple of issues with your aggregation. First of all, case-sensitivity matters. Second, you should not have two localFields and two foreignFields. The second field overwrites the first one. I would expect your query to look like this:
db.stack.aggregate(
[{
$lookup: {
"from": "B",
"localField": "Destination",
"foreignField": "Destination",
"as": "flight_docs"
}
}]
);
Which would give a result like this (The local document with all the foreign documents found by matching the Destination field):
{
"_id" : ObjectId("571546c7b9c99e9977f7d372"),
"Origin" : "xx",
"Destination" : "yy",
"Duration" : NumberInt(180),
"flight_docs" : [
{
"_id" : ObjectId("5715470eb9c99e9977f7d376"),
"Origin" : "xz",
"Destination" : "yy",
"Duration" : NumberInt(20)
}
]
}
Upvotes: 1