Query1: i was trying to combine two collection with selected field of both collections using mongodb Aggregate $lookup as follows
db.col1.aggregate([
{
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"
}