Show
// Drop the collections
db.testUsers.drop();
db.testCollection.drop();
// Create test data
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,
// name: true, <---- this will cause an error
email:true,
role:true,
nested: true
}
},
{
$redact: {
$cond: {
if: {
$eq: [
{
$size:"$nested"
}
,
0
]
},
then: "$$DESCEND",
else: "$$PRUNE"
}
}
}
];
// This works fine
var status1 = db.testUsers.aggregate(pipeline);
printjson(statistics.result);
// try to project the name field
pipeline[1].$project.name = true
// this should error
var status2 = db.testUsers.aggregate(pipeline);
// I ran this in the new aggregate debugger in Mongodb Compass (1.14.0-beta.3) and it output this error:
// The argument to $size must be an array, but was of type: missing