0

Please tell me what am I missing here:

this is the Doc I created:

 fatDoc = {
 name: "Babak",
 personID : 555,
 email : "babak@babak.name",
 music : ["pink floyd", "muse", "garfunkel"],
 food : ["free food", "yummy food", "mom food"],
 addresses:
   [ { type: "home",
       street: "123 Main",
       state: "NY",
       city: "brooklyn"
    },
    { type: "vacation",
      street: "456 sunshine",
      state: "CA",
      city: "SanFran"
    }
  ]   
 }

and this is the query I run on it:

db.coolkids.find({"addresses.type" : "home"}, {addresses:1}).pretty()

Question1: Why BOTH of addresses are getting returned as the result of this query? Question2: How can I change it to ONLY return the first doc which is the "home" address and not the second member of the array?

4

1 回答 1

1
  1. 您的查询与此文档匹配,并且您将返回恰好是一个数组的“地址”字段。在 2.0 中,返回少于完整数组的唯一方法是通过 $slice 运算符,但您需要知道所需元素的位置。

  2. 只取回一个与您的查询匹配的元素的方法是使用新的聚合框架(在开发版本 2.1 中可用,将在 2.2 中发布)。

聚合框架将允许您“$unwind”数组,以便您可以只取回包含与您的过滤器匹配的数组元素的文档。

于 2012-06-02T07:25:50.783 回答