【问题标题】:MongoDB - How to find equals in collection and in embedded documentMongoDB - 如何在集合和嵌入文档中找到等于
【发布时间】:2012-12-06 21:40:12
【问题描述】:

Gurus - 我陷入了一种情况,我无法弄清楚如何从以下集合“配偶”中查询,该集合已嵌入文档“姓氏”并检查与该文档的“姓氏”是否相等:

{
  "_id" : ObjectId("50bd2bb4fcfc6066b7ef090d"),
  "name" : "Gwendolyn",
  "surname" : "Davis",
  "birthyear" : 1978,
  "spouse" : {
    "name" : "Dennis",
    "surname" : "Evans",
    "birthyear" : 1969
  },  

我需要查询:

所有具有相同姓氏的配偶的输出数据(如果 配偶之一未指定,假设它与 另一个名字)

我尝试过这样的事情:

db.task.find( {"surname" : { "spouse.surname" : 1 }} )

但它失败了)

请指导我如何在任何示例/示例中实现这一点?基于此将非常有帮助:-)

非常感谢!

【问题讨论】:

    标签: mongodb


    【解决方案1】:

    你有三个选择。

    1. 使用$where修饰符:

      db.task.find({$where: 'this.spouse.surname === this.surname'})

    2. 更新所有文档并添加特殊标志。之后,您将能够通过此标志查询文档。它比$where 更快,但需要更改您的数据。

    3. 使用MapReduce。这很复杂,但它几乎可以让你做任何事情。

    【讨论】:

      猜你喜欢
      • 2023-03-22
      • 2011-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-19
      相关资源
      最近更新 更多