PHPDEV
PHPDEV

Reputation: 393

MongoDB query invalid

I am using the database MongoDB and I trying to run a query that will check two things.

The query is timestamp is greater than time and server is not equals to name.

It would look like this in an if statement

if (timestamp > date && !server.equalsIgnoreCase("name")

This is the code I'm using to query

List<BasicDBObject> queryList = new ArrayList<>();
queryList.add(new BasicDBObject("timestamp",new BasicDBObject("$gt", lastCheck)));
queryList.add(new BasicDBObject("server",new BasicDBObject("$not","Dev")));
BasicDBObject query = new BasicDBObject("$and",queryList);

I am getting this error when running the code

Caused by: com.mongodb.CommandResult$CommandFailure: command failed [count]: {                         "serverUsed" : "localhost/127.0.0.1:27017" , "errmsg" : "exception: invalid use of $not" , "code" : 13041 , "ok" : 0.0}

I know I am using the $not operator wrong but hopefully someone can help me

Upvotes: 0

Views: 714

Answers (1)

Vidya
Vidya

Reputation: 30300

Just use $ne (for not equal) instead as shown here.

Upvotes: 2

Related Questions