db.testUsers.drop();
db.testCollection.drop();
db.testUsers.insertMany( [
{
_id:ObjectId("54570f77968d6e492b0d68b0"),
email:"john@example.com",
password:"1z2j890",
name: {
first: "John",
last: "Smith"
}
},
{
_id:ObjectId("54570f77968d6e492b0d68b1"),
email:"jane@example.com",
password:"5sc46dr",
name: {
first: "Jane",
last: "Doe"
}
}
]);
db.testCollection.insert( {
_id:ObjectId('5b186cc4d1ded8d06a7c3baf'),
user:ObjectId("54570f77968d6e492b0d68b0"),
});
var pipeline = [
{
$lookup: {
from: 'testCollection',
localField: '_id',
foreignField: 'user',
as: 'nested'
}
},
{
$project: {
_id: true,
email:true,
role:true,
nested: true
}
},
{
$redact: {
$cond: {
if: {
$eq: [
{
$size:"$nested"
}
,
0
]
},
then: "$$DESCEND",
else: "$$PRUNE"
}
}
}
];
var status1 = db.testUsers.aggregate(pipeline);
printjson(statistics.result);
pipeline[1].$project.name = true
var status2 = db.testUsers.aggregate(pipeline);