Mongoose - find(): 搜索选项中的对象不起作用 [英] Mongoose - find(): object inside search options is not working

查看:38
本文介绍了Mongoose - find(): 搜索选项中的对象不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像这样的猫鼬模式:

I have a mongoose Schema that looks like this:

var mySchema = new mongoose.Schema({
  ...
  metadata: {
    isDeleted: {
      type: Boolean,
      default: false
    },
    ...
  }
});

我想获取应用过滤器的 mongodb 数据库中的元素列表,所以我有以下对象:

I want to get the list of elements in my mongodb database applying a filter, so I have the following object:

var searchOptions = { metadata: { isDeleted: false } };

在执行查询之前,总是需要将 metadata.isDeleted 值设置为 false,与稍后将添加的其他参数分开:

that always needs to have that metadata.isDeleted value set to false, appart from other parameters that will be added later, before executing the query:

var objQuery = myModel.find(searchOptions, '-metadata');

起初,我在架构中的 metadata 对象之外有 isDeleted 属性,并且

At first, I had isDeleted attribute outside my metadata object in the Schema, and

var searchOptions = { isDeleted: false };

曾经完美地工作.但这是因为我决定将 isDeleted 放在我的 metadata 对象中,该对象不起作用并且无法弄清楚为什么......

used to work perfectly. But it is since I decided to have isDeleted inside my metadata object that is not working and can't figure out why...

推荐答案

鉴于您在架构列表中使用省略号,很可能在 元数据下有比 isDeleted 更多的属性 属性.所以你的对象应该是:

It seems pretty likely given your use of elipsis in your schema listing that there are more properties than isDeleted under the metadata property. So your object should be:

var searchOptions = { "metadata.isDeleted": false } };

这样做的原因是,否则查询正在寻找一个完全"和仅"在 metadata 键下命名的属性的文档:

The reason for this is that otherwise the query is looking for a document with "exactly" and "only" the properties named under the metadata key:

var searchOptions = { metadata: { isDeleted: false } };

如果不是这样,那么当然没有匹配.

And when that is not the case, then of course there is no match.

这篇关于Mongoose - find(): 搜索选项中的对象不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆