Mongodb query help (spatial query)
New here? Learn about Bountify and follow @bountify to get notified of new bounties! x

We store a bunch of events/meetings at a specific office in a mongodb table and they have dates like this

"dates" : {
"startDate" : 201502011600,
"endDate" : 201602010200,
"displayTime" : 201511240600

We have a need to query our db for each office to figure out if it has no meetings (today) or for the next 5 days... basically to highlight that we can schedule it.

Having a tough time wrapping my head around this query since today + 5 days is basically a vector and each event at each building is another vector I feel like I need something complex for this so... Id like to see if I can get some help here.

here is the full json document of an event
Qdev over 5 years ago
awarded to tomtoump

Crowdsource coding tasks.

2 Solutions

Hello, just create date, last date, format them and look for gte lte in that dates range.
works in mongodb shell.

var nowDate = new Date(2015,01,01);
lastDate = new Date();
db.test.find({"dates.startDate":{$gte: Number(nowDate.toLocaleFormat("%Y%m%d%H%M")), $lte:Number(lastDate.toLocaleFormat("%Y%m%d%H%M"))} })
Winning solution

Here you go:

var sinceDate = new Date(),
    untilDate = new Date();

untilDate.setDate(untilDate.getDate() + 5);

function formatDate (date) {
    return parseInt(
             + ('0' + (date.getMonth() + 1)).slice(-2) 
             + ('0' + date.getDate()).slice(-2)
             + ('0' + date.getHours()).slice(-2)
             + ('0' + date.getMinutes()).slice(-2)

    "dates.startDate": { $lt: formatDate(untilDate) },
    "dates.endDate"  : { $gt: formatDate(sinceDate) }
View Timeline