我需要编写一个查询字符串,仅获取 Mongoose 模型中“启用”字段设置为 true 的值,但我编写的查询似乎不起作用。无论我将其更改为什么,它都会返回数据库中的所有内容。
尝试了查询字符串的变体。
const PostsSchema = new Schema({
title: String,
description: String,
enabled: {
type: Boolean,
default: true
}
});
这是我的查询字符串:
query = { $and:[ {$text: { $search: search_string }},{"enabled":{$ne:false}} ] };
opts = { score: { $meta: 'textScore' } };
sort = {
score: { $meta: 'textScore' }
};
let results = await Posts.find(query, opts)
.sort(sort).lean();
最佳答案
如果您正在寻找一个仅获取集合中启用设置为 true 的函数, 这是实现的方式之一。
db.posts.aggregate ([
{ $match: { enabled: true } }
]);
在聚合中,您还可以对值进行排序、分组、限制、跳过。
关于node.js - 如何检查 Mongoose 模型中的字段是否设置为 true,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55465796/