0

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

期望的结果:{"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},
]}
4

1 回答 1

0

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

db.collection.find({},{addresses: {$elemMatch:{'name':'home'}}, 'addresses.street':1, _id:0})
{ "addresses" : [ { "street" : "588 5th Street" } ] }
于 2013-09-10T07:26:36.070 回答