Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get nested fields with MongoDB shell

I've "users" collection with a "watchlists" field, which have many inner fields too, one of that is "arrangeable_values" (the second field within "watchlists").

I need to find for each user in "users" collection, each "arrangeable_values" within "watchlists".

How can I do that with mongodb shell ?

Here is an example of data model :

> 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
    }
    ]
}
like image 537
Luca G. Soave Avatar asked Dec 17 '22 01:12

Luca G. Soave


1 Answers

For the document above, the following find() query would extract both the "nickname" of the document, and its associated "arrangeable_values" (where the document is in the users collection):

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

The result you get for your single document example would be:

{ "_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" } } 
] }
like image 145
Geoff Avatar answered Dec 31 '22 21:12

Geoff