【问题标题】:Nodejs-Mongodb-native subdocument filter not working properlyNodejs-Mongodb-native 子文档过滤器无法正常工作
【发布时间】:2021-07-27 19:04:31
【问题描述】:

我正在尝试通过 MongoDB-Native 驱动程序从 Nodejs 代码中获取来自 Mongodb 的数据。这是我收藏的模型:

{
  name: 'Himkar Dwivedi',
  role: 'Software Engineer',
  address: {
    flat: '163',
    building: 'B6',
    area: 'DLF',
    contact: {
      type: 'phone',
      num: '910xxxx882'
    }
  }
}

我从以下查询中得到了正确的响应:

{ role: 'Software Engineer',   name: 'Himkar Dwivedi',   'address.flat': '163' }

但我需要形成这样的查询:(真正的 json 查询对象)

{ role: 'Software Engineer',   name: 'Himkar Dwivedi',   address:{flat: '163' }}

这没有给我任何回应。请帮我解决。

【问题讨论】:

  • 您的第二个查询将仅匹配整个 address 子文档为 {flat: '163'} 的文档(没有其他字段)。这就是为什么您需要使用点表示法。你问的是这个吗?
  • 感谢 Johnny,我使用 DOT 解决了我的问题。但它应该支持纯json查询

标签: json node.js mongodb mongodb-query node-mongodb-native


【解决方案1】:

因为当你使用这个查询address:{flat: '163' }mongo 搜索这样一个文档时:

{
  name: 'Himkar Dwivedi',
  role: 'Software Engineer',
  address: {
    flat: '163'
  }
}

【讨论】:

    猜你喜欢
    • 2013-09-15
    • 2015-02-14
    • 2013-08-16
    • 2012-11-16
    • 2011-04-06
    • 1970-01-01
    相关资源
    最近更新 更多