使用匹配 returns null 填充查询

Populate query with match returns null

我有三个模式,需要将它们分开并且我不能使用子文档。重要的是这个

export var TestSchema = new mongoose.Schema({
    hash: { type: String, index: { unique: true }, default: common.randomHash },
    date: { type: Date, default: Date.now },
    result: { type: Object },
    user: { type: mongoose.Schema.Types.ObjectId, ref: 'User' },
    data: { type: Object },
    finished: Date,
    lang: { type: String, default: 'pt' },
    benchmark: { type: String, required: true },
    order: { type: mongoose.Schema.Types.ObjectId, ref: 'Transaction' },
    /* TODO: remove */
    name: { type: String } 
});

我有一个填充查询(它实际上是一个分页助手,但我正在切入正题):

TestModel.find({hide: {$ne: true}, user: id}).populate({
   path: 'user', 
   match: {$or: [
     {email: new RegExp(search, i)},
     {name: new RegExp(search, i)}, 
     {empresa: new RegExp(search, i)},
   ]}
}).exec().then(/*...*/)

当 populate.match 没有找到任何东西时,它将 user 设置为空。我尝试设置 find({'user':{$ne: null}}) 但它忽略了它。 (我想填充发生在查找调用之后,也许这就是原因)。

有什么方法可以在数据库层中过滤它,而不必依赖结果的迭代,检查是否为 null,然后过滤掉?

This answer on GitHub 澄清了由于 MongoDB 的工作方式,populate 是不可能的。但是,您应该可以使用 $lookup.