Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB Spring Data Criteria Not operator

I have following code to search in mongo db using spring data mongodb ( version 1.2.3.RELEASE)

Criteria searchCriteria = Criteria.where("NAME").is("TestName")
        .and("ID").is("TestID").not().and("Age").is("23");

I got following query ( without not operator )

Query: { "NAME" : "TestName" , "ID" : "TestID", "Age" : "23" }

I was expecting following query

Query: { "NAME" : "TestName" , "$not" : { "ID" : "TestID"}, "Age" : "23" }

What am i doing wrong ? Any help is greatly appreciated. Thanks

like image 408
Viraj Avatar asked Oct 14 '13 05:10

Viraj


1 Answers

I used 'ne' instead.

 Criteria searchCriteria = Criteria.where("NAME").is("TestName").and("ID").ne("TestID").and("Age").is("23");

Mongo db 'not' is a logical operator. http://docs.mongodb.org/manual/reference/operator/query/not/

like image 98
Viraj Avatar answered Jan 25 '23 06:01

Viraj