gpt4 book ai didi

Mongoose 查询忽略不在模式中的字段

转载 作者:行者123 更新时间:2023-12-05 05:18:05 27 4
gpt4 key购买 nike

这是我的架构:

const Post = mongoose.model('Post', {
title: String,
authorId: String,
})

我想查询它:

Post.find({ authorId: '123', someField: 1 })

结果是 [],我怎样才能让 mongoose 忽略不在模式中的查询字段?

最佳答案

它有一个标志 strictQuery ( https://mongoosejs.com/docs/guide.html#strictQuery )

For backwards compatibility, the strict option does not apply to the filter parameter for queries. Mongoose has a separate strictQuery option to toggle strict mode for the filter parameter to queries.

const mySchema = new Schema({ field: Number }, {
strict: true,
strictQuery: true // Turn on strict mode for query filters
});

const MyModel = mongoose.model('Test', mySchema);

// Mongoose will strip out `notInSchema: 1` because `strictQuery` is `true`
MyModel.find({ notInSchema: 1 });

关于Mongoose 查询忽略不在模式中的字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48276860/

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