来自CLI的嵌套Mongo查询

时间:2013-09-09 18:54:01

标签: mongodb

对于以下数据(取自MongoDB in Action),如何在Mongo Shell中查询addresses.street获取addresses.home任何文档(在我的情况下,这里只有1){{1}等于“家”?

期望的结果:{"street" : "1 E. 23rd Street"}

{ _id: ObjectId("4c4b1476238d3b4dd5000001")
username: "kbanker",
addresses: [
{ 
  name: "home",
  street: "588 5th Street",
  city: "Brooklyn",
  state: "NY",
  zip: 11215},
{ 
  name: "work",
  street: "1 E. 23rd Street",
  city: "New York",
  state "NY",
  zip 10010},
]}

1 个答案:

答案 0 :(得分:0)

您必须在投影模式下使用$ elemMatch:DOCS

db.collection.find({},{addresses: {$elemMatch:{'name':'home'}}, 'addresses.street':1, _id:0})
{ "addresses" : [ { "street" : "588 5th Street" } ] }