Brij Raj Singh - MSFT
Brij Raj Singh - MSFT

Reputation: 5113

mongo document with only keys inside keys, how to do a find on key range

I have a Document which looks like this

{
    "2014" : {
        "11" : {
            "20" : {
                "Counts" : {
                    "c" : 2
                }
            },
            "21" : {
                "Counts" : {
                    "c" : 20
                }
            },
            "22" : {
                "Counts" : {
                    "c" : 27
                }
            }
        }
    },
    "_id" : "53d6883a2dc307560d000004",
    "createdate" : ISODate("2014-11-21T07:15:26.109Z")
}

which is basically keeping a count on the year{month{day{counts{c=countval}}} structure, How do i do a find of data on a date range like returning counts on between 21 and 22 november

I tried something like this

db.installObjs.find({"2014.11.20.Counts.c":{$ne:null}},{"2014.11.20.Counts.c":1})

but as you can see it works for a certain date only

Upvotes: 2

Views: 169

Answers (1)

BatScream
BatScream

Reputation: 19700

You can build your query in the following pattern. It is a native mongo driver javascript code, but on transformation to python should work correctly.

var month = 11;
var range = [20,21,22];
var year = 2014;
var project = {};
var find = {};
range.forEach(function(i){
var str = year+"."+month+"."+i+"."+"Counts"+"."+"c";
find[str] ={"$exists":true};
project[str] = 1;
})
db.collection.find(find,project);

Upvotes: 2

Related Questions