Query1: i was trying to combine two collection with selected field of both collections using mongodb Aggregate $lookup as follows
db.col1.aggregate([
{
Your $lookup
stage simply returns multiple entries in the user
field which then get flattened into individual subdocuments that have the same _id
value. You can validate this by replacing the $out
stage with a $match
instead:
db.col1.aggregate([{
$match: { _id: ObjectId('5b16305d145a5117552836ec') }
}
The key here is to understand how $unwind
works. Imagine you have the following document:
{
_id: 1,
users: [ 'a', 'b' ]
}
Once you $unwind
this you will get the following two documents:
{
_id: 1,
users: 'a'
},
{
_id: 1,
users: 'b'
}
So you get the same _id
value for two different documents!
Now, when you specify _id: 0
in your projection you remove the _id
field from all documents which will cause MongoDB to automatically create new _id
s which of course then works.
Ok for duplicated id explanation. In addition, if you want to keep user._id, add id:"$_id"
to your last project stage.
But for the second query with _id:0, what do you mean by 'no error and no output :
---EDIT--- With these data :
col1 :
{
"_id" : 1.0,
"field1" : "a",
"field2" : "b"
}
col2 :
{
"_id" : 1.0,
"field1" : "a",
"field2" : "c"
}
the right query to achieve what you need is the following :
db.col1.aggregate(
[
// Stage 1
{
$lookup: {
from: "col2",
localField: "field1",
foreignField: "field1",
as: "user"
}
},
// Stage 2
{
$unwind: {
path : "$user",
}
},
// Stage 3
{
$project: {
"userfield": "$user.field1",field1:1
}
},
]
);
Will output :
{
"_id" : 1.0,
"field1" : "a",
"userfield" : "a"
}