使用MongoDB shell获取嵌套字段

时间:2012-04-07 19:15:24

标签: mongodb mongodb-query database nosql

我的“用户”集合带有“关注列表”字段,其中也有许多内部字段,其中一个是“arrangeable_values”(“关注列表”中的第二个字段)。

我需要找到“users”集合中的每个用户,“watchlists”中的每个“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
    }
    ]
}

2 个答案:

答案 0 :(得分:9)

对于上面的文档,以下find()查询将提取文档的“昵称”及其关联的“arrangeable_values”(文档位于users集合中):

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 :(得分:1)

MongoDB查询返回整个文档。您正在查找文档内部数组内的字段,这将破坏find()

这里的问题是任何基本的find()查询都将返回所有匹配的文档。 find() 可以选择仅返回特定字段。但这不适用于您的子对象数组。您可以返回watchlists,但不能返回watchlist entries that match

目前您有两种选择:

  1. 编写一些循环遍历文档的客户端代码并进行过滤。请记住,shell实际上是一个javascript驱动程序,因此您可以在其中编写代码。
  2. 使用新的aggregation framework。这将有一个学习曲线,但它可以有效地提取您正在寻找的子项目。