Spring Data MongoDB date query.

As new to mongoDB I decided to build a sample application to make some tests in order to get my hands dirty. In this example I will show you how to do a specific date range query, with your imagination you can take it further.

Lets first create a document with the following structure:

package com.oreilly.springdata.mongodb.core;

import java.math.BigInteger;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import lombok.Data;

import org.springframework.data.annotation.PersistenceConstructor;
import org.springframework.data.mongodb.core.mapping.Document;
public class Room extends AbstractDocument {

private int bedcount;

private int bedtype1;

private int bedtype2;

private int bedtype3;

private int bedtype4;

private String filetype;

private byte noofrooms;

private byte occupancy;

private Object photo;

private int rateid;

private String roomname;

private int roomno;

private String status;

private BigInteger roomTypeID;

private List occupiedDates;

private Map attributes = new HashMap();

public Room() {

public Room(int bedcount, int bedtype1, int bedtype2, int bedtype3,
int bedtype4, String filetype, byte noofrooms, byte occupancy,
Object photo, int rateid, String roomname, int roomno,
String status, BigInteger roomTypeID, Map attributes) {
this.bedcount = bedcount;
this.bedtype1 = bedtype1;
this.bedtype2 = bedtype2;
this.bedtype3 = bedtype3;
this.bedtype4 = bedtype4;
this.filetype = filetype;
this.noofrooms = noofrooms;
this.occupancy = occupancy;
this.photo = photo;
this.rateid = rateid;
this.roomname = roomname;
this.roomno = roomno;
this.status = status;
this.roomTypeID = roomTypeID;
this.attributes = attributes;


Now assuming that you have made a repository, a mongoTemplate etc.

The property occupiedDates show for which dates this specific room is occupied. Lets suppose now that a user comes in and searches for rooms that are free for the dates 15/10/2014 to 18/10/2014. What you basically want now is a query which will return all the rooms that in occupiedDates do not have any of the dates below:

Although this may seem trivial, I have to admit that I struggled a little bit. So here is the sollution using DBObjects:

Date to = null;
Date from = null;
try {
to = dateFormat.parse(“16-08-2014 00:00:00”);
from = dateFormat.parse(“15-08-2014 00:00:00”);
} catch (ParseException e) {
// TODO Auto-generated catch block
long start = System.currentTimeMillis();

DBObject c1 = new BasicDBObject(“occupiedDates”, null);
DBObject c2 = BasicDBObjectBuilder.start().push(“occupiedDates”).push(“$not”).
push(“$elemMatch”).add(“$gte”, from).add(“$lte”, to).get();
Criteria c = Criteria.where(“$or”).is(Arrays.asList(c1, c2));
Query query = new Query().addCriteria(c);
List rooms = mongoTemplate.find(query, Room.class);

long end = System.currentTimeMillis();
long took = end – start * 1000;
System.out.println(“DEBUG: Logic took ” + took);

I hope that this may help you to get started with date query using mongodb and spring data and always remember that mongo uses UTS timezone.