Question : mongodb lookup get one field
Answered by : concerned-curlew-ikw0cqzlrryi
db.orders.aggregate([ { $lookup: { from: "stati", localField: "status", foreignField: "id", as: "strStatus" } }, { $set: { strStatus: { $arrayElemAt: ["$strStatus.str", 0] } } }
])
Source : https://www.mongodb.com/community/forums/t/how-to-add-single-field-using-lookup/104792/4 | Last Update : Fri, 30 Dec 22
Question : only a field from lookup mongodb
Answered by : tomer-zaks
//MONGO
//add the following:
{$project: { id: 1 } }
//Example
//to see only ids that appear in col1 but not in col2
db.co1.aggregate(
[ { $lookup: { from: "col1", localField: "id", foreignField: "id", as: "matched_docs" } }, { $match: { "matched_docs": { $eq: [] } } }, {$project: { id: 1 } }
]);
Source : | Last Update : Tue, 16 Aug 22