59

I have some data that looks like this:

[
    {
        "_id" : ObjectId("4e2f2af16f1e7e4c2000000a"),
        "advertisers" : [
            {
                "created_at" : ISODate("2011-07-26T21:02:19Z"),
                "category" : "Infinity Pro Spin Air Brush",
                "updated_at" : ISODate("2011-07-26T21:02:19Z"),
                "lowered_name" : "conair",
                "twitter_name" : "",
                "facebook_page_url" : "",
                "website_url" : "",
                "user_ids" : [ ],
                "blog_url" : "",
            },

and I was thinking that a query like this would give the id of the advertiser:

var start  = new Date(2011, 1, 1);
> var end  = new Date(2011, 12, 12);
> db.agencies.find( { "created_at" : {$gte : start , $lt : end} } , { _id : 1 , program_ids : 1 , advertisers { name : 1 }  } ).limit(1).toArray();

But my query didn't work. Any idea how I can add the fields inside the nested elements to my list of fields I want to get?

Thanks!

Genadinik
  • 18,153
  • 63
  • 185
  • 284

3 Answers3

105

Use dot notation (e.g. advertisers.name) to query and retrieve fields from nested objects:

db.agencies.find({
 "advertisers.created_at": {
   $gte: start,
   $lt: end
  }
 },
{
 _id: 1,
  program_ids: 1,
  "advertisers.name": 1
 }
}).limit(1).toArray();

Reference: Retrieving a Subset of Fields and Dot Notation

nur farazi
  • 1,197
  • 12
  • 32
lnmx
  • 10,846
  • 3
  • 40
  • 36
  • Everything is under data in the question. I.e. The conditions are wrong – AD7six Jun 04 '12 at 17:26
  • 11
    Should be using `findOne`, rather than `find` with `limit(1)`. – mmm Dec 21 '14 at 22:41
  • Thanks. Maybe there is more tiny way to make the process than use : {"object.nestedObject": nestedObjectValue} ? can we do something like: {"object.nestedObject"} without repeating the "nestedObject part like in es6 ? – HoCo_ Oct 10 '18 at 11:17
4
db.agencies.find( 
{ "advertisers.created_at" : {$gte : start , $lt : end} } , 
{ program_ids : 1 , advertisers.name : 1   } 
).limit(1).pretty();
Krzysztof Boduch
  • 675
  • 9
  • 12
Mak Ashtekar
  • 154
  • 1
  • 11
2

There is one thing called dot notation that MongoDB provides that allows you to look inside arrays of elements. Using it is as simple as adding a dot for each array you want to enter.

In your case

    "_id" : ObjectId("4e2f2af16f1e7e4c2000000a"),
    "advertisers" : [
        {
            "created_at" : ISODate("2011-07-26T21:02:19Z"),
            "category" : "Infinity Pro Spin Air Brush",
            "updated_at" : ISODate("2011-07-26T21:02:19Z"),
            "lowered_name" : "conair",
            "twitter_name" : "",
            "facebook_page_url" : "",
            "website_url" : "",
            "user_ids" : [ ],
            "blog_url" : "",
        },
        { ... }

If you want to go inside the array of advertisers to look for the property created_at inside each one of them, you can simply write the query with the property {'advertisers.created_at': query} like follows

db.agencies.find( { 'advertisers.created_at' : { {$gte : start , $lt : end} ... }
  • 1
    Not only that you copied the accepted answer but you have copied it badly, `advertisers { name : true }` is not valiud syntax even if you were to correct the physical errors MongoDB does not read a projection document that way – Sammaye Oct 12 '14 at 17:23