2012-04-07 62 views
0

我有一個「watchlist」字段,裏面有很多內部字段,其中一個是「arrangeable_values」(「watchlist」中的第二個字段)。使用MongoDB shell獲取嵌套字段

我需要爲「用戶」集合中的每個用戶查找「watchlist」中的每個「arrangeable_values」。

我怎麼用mongodb shell做到這一點?

下面是數據模型的例子:

> db.users.findOne({'nickname': 'superj'}) 
{ 
    "_id" : ObjectId("4f6c42f6018a590001000001"), 
    "nickname" : "superj", 
    "provider" : "github", 
    "user_hash" : null, 
    "watchlists" : [ 
     { 
      "_id" : ObjectId("4f6c42f7018a590001000002"), 
      "arrangeable_values" : { 
       "description" : "My introduction presentation to node.js along with sample code at various stages of building a simple RESTful web service with journey, cradle, winston, optimist, and http-console.", 
       "tag" : "", 
       "html_url" : "https://github.com/indexzero/nodejs-intro" 
      }, 
      "avatar_url" : "https://secure.gravatar.com/avatar/d43e8ea63b61e7669ded5b9d3c2e980f?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-140.png", 
      "created_at" : ISODate("2011-02-01T10:20:29Z"), 
      "description" : "My introduction presentation to node.js along with sample code at various stages of building a simple RESTful web service with journey, cradle, winston, optimist, and http-console.", 
      "fork_" : false, 
      "forks" : 13, 
      "html_url" : "https://github.com/indexzero/nodejs-intro", 
      "pushed_at" : ISODate("2011-09-12T17:54:58Z"), 
      "searchable_values" : [ 
       "description:my", 
       "description:introduction", 
       "description:presentation", 
       "html_url:indexzero", 
       "html_url:nodejs", 
       "html_url:intro" 
      ], 
      "tags_array" : [ ], 
      "watchers" : 75 
     }, 
    { 
     "_id" : ObjectId("4f6c42f7018a590001000003"), 
     "arrangeable_values" : { 
      "description" : "A Backbone alternative idea", 
      "tag" : "", 
      "html_url" : "https://github.com/maccman/spine.todos" 
     }, 
     "avatar_url" : "https://secure.gravatar.com/avatar/baf018e2cc4616e4776d323215c7136c?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-140.png", 
     "created_at" : ISODate("2011-03-18T11:03:42Z"), 
     "description" : "A Backbone alternative idea", 
     "fork_" : false, 
     "forks" : 31, 
     "html_url" : "https://github.com/maccman/spine.todos", 
     "pushed_at" : ISODate("2011-11-20T22:59:45Z"), 
     "searchable_values" : [ 
      "description:a", 
      "description:backbone", 
      "description:alternative", 
      "description:idea", 
      "html_url:https", 
      "html_url:github", 
      "html_url:com", 
      "html_url:maccman", 
      "html_url:spine", 
      "html_url:todos" 
     ], 
     "tags_array" : [ ], 
     "watchers" : 139 
    } 
    ] 
} 

回答

7

對於上述文件,下面的查找()查詢將提取兩個文件的「暱稱」,及其相關的「arrangeable_values」(如該文件是在用戶的集合):

db.users.find({}, { "nickname" : 1, "watchlists.arrangeable_values" : 1 }) 

您得到的單個文檔示例的結果是:

{ "_id" : ObjectId("4f6c42f6018a590001000001"), "nickname" : "superj", 
"watchlists" : [  
    { "arrangeable_values" : { "description" : "My introduction presentation to node.js along with sample code at various stages of building a simple RESTful web service with journey, cradle, winston, optimist, and http-console.",  "tag" : "",  "html_url" : "https://github.com/indexzero/nodejs-intro" } }, 
    { "arrangeable_values" : { "description" : "A Backbone alternative idea", "tag" : "",  "html_url" : "https://github.com/maccman/spine.todos" } } 
] } 
1

MongoDB的查詢將返回整個文檔。您正在文檔內的數組內查找一個字段,這將打破find()

這裏的問題是,任何基本的find()查詢,將返回所有匹配的文件。 find()確實可以選擇僅返回特定字段。但是這不適用於你的數組子對象。您可以返回watchlists,但不是watchlist entries that match

目前的情況是,你有兩個選擇:

  1. 編寫通過文件循環,並做了一些過濾客戶端代碼。請記住,shell實際上是一個JavaScript驅動程序,因此您可以在其中編寫代碼。
  2. 使用新的aggregation framework。這將有一個學習曲線,但它可以有效地提取您正在尋找的子項目。