Todavía puedes usar aggregate()
aquí con MongoDB 3.2, pero solo usando $redact
en cambio:
db.boards.aggregate([
{ "$redact": {
"$cond": {
"if": {
"$and": [
{ "$ne": [ "$createdBy._id", "$owner._id" ] },
{ "$setIsSubset": [["$createdBy._id"], "$acl.profile._id"] }
]
},
"then": "$$KEEP",
"else": "$$PRUNE"
}
}}
])
O con $where
para el shell MongoDB 3.2, solo necesita mantener una copia con alcance de this
, y tu sintaxis estaba un poco mal:
db.boards.find({
"$where": function() {
var self = this;
return (this.createdBy._id != this.owner._id)
&& this.acl.some(function(e) {
return e.profile._id === self.createdBy._id
})
}
})
O en un entorno compatible con ES6 entonces:
db.boards.find({
"$where": function() {
return (this.createdBy._id != this.owner._id)
&& this.acl.some(e => e.profile._id === this.createdBy._id)
}
})
El agregado es la opción de mayor rendimiento de las dos y siempre debe ser preferible al uso de la evaluación de JavaScript
Y si sirve de algo, la sintaxis más nueva con $expr
sería:
db.boards.find({
"$expr": {
"$and": [
{ "$ne": [ "$createdBy._id", "$owner._id" ] },
{ "$in": [ "$createdBy._id", "$acl.profile._id"] }
]
}
})
Usando $in
en preferencia a $setIsSubset
donde la sintaxis es un poco más corta.
return (this.createdBy._id.valueOf() != this.owner._id.valueOf())
&& this.acl.some(e => e.profile._id.valueOf() === this.createdBy._id.valueOf())