2

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

Brij Raj Singh - MSFT
  • 4,903
  • 7
  • 36
  • 55
  • Logically speaking, you cant do a range query on `string` literals on the server side. You need to do some post processing or write server side `javascript` functions. – BatScream Nov 21 '14 at 07:39
  • that sucks right i thought its an efficient means to store counts – Brij Raj Singh - MSFT Nov 21 '14 at 07:46
  • Its a bit complicated, You need to do some extra application layer programming in either constructing the query or post processing the results. Take a look at my answer in javascript. – BatScream Nov 21 '14 at 07:51

1 Answers1

2

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);
BatScream
  • 19,260
  • 4
  • 52
  • 68