Question

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

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top