Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 如何在Mongoose模型中检查字段是否设置为true_Node.js_Mongodb_Mongoose - Fatal编程技术网

Node.js 如何在Mongoose模型中检查字段是否设置为true

Node.js 如何在Mongoose模型中检查字段是否设置为true,node.js,mongodb,mongoose,Node.js,Mongodb,Mongoose,我需要编写一个查询字符串,只获取那些Mongoose模型中“enabled”字段设置为true的值,但我编写的查询似乎不起作用。它返回数据库中的所有内容,无论我将其更改为什么 尝试了查询字符串的变体 const PostsSchema = new Schema({ title: String, description: String, enabled: { type: Boolean, default: true } }); 这是我的查询字符串: query = { $and:[ {$text

我需要编写一个查询字符串,只获取那些Mongoose模型中“enabled”字段设置为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 } }
    ]);

在聚合中,您还可以对值进行排序、分组、限制和跳过。

如果您正在查找一个函数,该函数在集合中只获取设置为true的启用值, 这是实现这一目标的途径之一

db.posts.aggregate ([
    { $match: { enabled: true } }
    ]);

在聚合中,您还可以对值进行排序、分组、限制和跳过。

您已经在那里编写了
{“enabled”:{$ne:true}
,这意味着它只在启用时获取那些文档:false,但在启用时需要文档。请更正查询或更新问题。您已经在那里编写了
{“enabled”:{$ne:true}
,这意味着它只获取那些启用了的文档:false,但期望文档为true。请更正查询或更新问题。