kevinlinxp
12/31/2016 - 6:23 AM

Java:MongoDB:query

Java:MongoDB:query

@Query("{'deviceId':?0, 'masterValveErrorOccurredDate':{$exists: true}, 'masterValveErrorFixedDate':{$exists: false}, 'eventType':'MASTER_VALVE_ERROR_EVENT'}")

@Query("{'deviceId':?0, 'recordType':'RECORD'}")

Spring Data MongoDB query methods

After findByBirthdateAfter(Date date) {"birthdate" : {"$gt" : date}}

GreaterThan findByAgeGreaterThan(int age) {"age" : {"$gt" : age}}

GreaterThanEqual findByAgeGreaterThanEqual(int age) {"age" : {"$gte" : age}}

Before findByBirthdateBefore(Date date) {"birthdate" : {"$lt" : date}}

LessThan findByAgeLessThan(int age) {"age" : {"$lt" : age}}

LessThanEqual findByAgeLessThanEqual(int age) {"age" : {"$lte" : age}}

Between findByAgeBetween(int from, int to) {"age" : {"$gt" : from, "$lt" : to}}

In findByAgeIn(Collection ages) {"age" : {"$in" : [ages…]}}

NotIn findByAgeNotIn(Collection ages) {"age" : {"$nin" : [ages…]}}

IsNotNull, NotNull findByFirstnameNotNull() {"firstname" : {"$ne" : null}}

IsNull, Null findByFirstnameNull() {"firstname" : null}

Like, StartingWith, EndingWith findByFirstnameLike(String name) {"firstname" : name} ( name as regex)

Containing on String findByFirstnameContaining(String name) {"firstname" : name} (name as regex)

Containing on Collection findByAddressesContaining(Address address) {"addresses" : { "$in" : address}}

Regex findByFirstnameRegex(String firstname) {"firstname" : {"$regex" : firstname }}

(No keyword) findByFirstname(String name) {"firstname" : name}

Not findByFirstnameNot(String name) {"firstname" : {"$ne" : name}}

Near findByLocationNear(Point point) {"location" : {"$near" : [x,y]}}

Near findByLocationNear(Point point, Distance max) {"location" : {"$near" : [x,y], "$maxDistance" : max}}

Near findByLocationNear(Point point, Distance min, Distance max) {"location" : {"$near" : [x,y], "$minDistance" : min, "$maxDistance" : max}}

Within findByLocationWithin(Circle circle) {"location" : {"$geoWithin" : {"$center" : [ [x, y], distance]}}}

Within findByLocationWithin(Box box) {"location" : {"$geoWithin" : {"$box" : [ [x1, y1], x2, y2]}}}

IsTrue, True findByActiveIsTrue() {"active" : true}

IsFalse, False findByActiveIsFalse() {"active" : false}

Exists findByLocationExists(boolean exists) {"location" : {"$exists" : exists }}

MongoDB, mongo command line

db.event.find({deviceId:6}, {_class:0}).pretty()