【问题标题】:Ignoring a field mongodb request忽略字段 mongodb 请求
【发布时间】:2019-01-12 21:34:07
【问题描述】:

假设我们在 mongodb 集合中有这样的对象:

{
  _id: 00000001
  colors: ["green", "yellow"],
  houses: [
    {
      number: 1,
      owner: "John"
    },
    {
      number: 2,
      owner: "John"
    },
    {
      number:3,
      owner: "Dave"
    }
  ]
},
{
  _id: 00000002
  colors: ["green", "red"],
  houses: [
    {
      number: 15,
      owner: "Dave"
    },
  ]
}

因此,要获取颜色数组包含绿色的每个对象,我需要编写的查询看起来像这样:collection.find({colors: "green"}); 现在,如果我想获取 John 拥有房屋的所有对象,我将如何制定这样的查询? 基本上我要问的是,如果我的查询是collection.find({houses: {owner: "John", number: ?}}),我需要用什么替换"?" 来告诉mongo我不在乎数字有什么值。 或者也许还有另一种我没有想到的方法? 感谢您的任何帮助! (顺便说一句,这是一个虚构的例子,因此为什么 ID 看起来很奇怪,而对象本身似乎不是很有用。)

【问题讨论】:

    标签: arrays node.js mongodb


    【解决方案1】:

    要查询可以使用dot notation 的对象数组,请尝试:

    db.collection.find({ "houses.owner": "John"}})
    

    【讨论】:

    • 确实有效,非常感谢,正是我想要的:)
    猜你喜欢
    • 2019-07-12
    • 1970-01-01
    • 1970-01-01
    • 2021-02-12
    • 1970-01-01
    • 2014-02-14
    • 2013-03-07
    • 1970-01-01
    • 2014-06-20
    相关资源
    最近更新 更多