4

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

我需要为“用户”集合中的每个用户找到“关注列表”中的每个“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
    }
    ]
}
4

2 回答 2

11

对于上面的文档,以下 find() 查询将提取文档的“昵称”及其关联的“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" } } 
] }
于 2012-04-09T12:57:50.463 回答
1

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

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

就目前而言,您有两种选择:

  1. 编写一些遍历文档并进行过滤的客户端代码。请记住,shell 实际上是一个 javascript 驱动程序,因此您可以在其中编写代码。
  2. 使用新的聚合框架。这将有一个学习曲线,但它可以有效地提取您正在寻找的子项目。
于 2012-04-08T04:24:20.707 回答