gpt4 book ai didi

mongodb - 使用匹配项填充查询返回 null

转载 作者:行者123 更新时间:2023-12-04 18:03:57 26 4
gpt4 key购买 nike

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

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 设置为 null。我尝试设置 find({'user':{$ne: null}}) 但它忽略了它。 (我想填充发生在查找调用之后,也许这就是原因)。

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

最佳答案

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

关于mongodb - 使用匹配项填充查询返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30201983/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com