$match
で集計を使用できます 条件と$lookup
に一致させる ローカルフィールドをマッピングするにはuser
user
に コレクション_id
フィールド:
db.a.aggregate(
[{
$match: {
"type": ObjectId("50ed90f5a70defef23000002"),
"config.name": "alpha"
}
}, {
$lookup: {
from: "users",
localField: "user",
foreignField: "_id",
as: "users"
}
}, {
$unwind: "$users"
}]);
Javascriptでは、mongoose
たとえば、これは:
YourModel.aggregate(
[{
$match: {
"type": ObjectId("50ed90f5a70defef23000002"),
"config.name": "alpha"
}
}, {
$lookup: {
from: "users",
localField: "user",
foreignField: "_id",
as: "users"
}
}, {
$unwind: "$users"
}],
function(err, result) {
console.log("lastname : " + result.users.lastname);
});