How to compare two strings in mongoDB spring data?

六月ゝ 毕业季﹏ 提交于 2019-12-29 02:12:10

问题


i'm trying to compare two string in mongoDB spring Data.
My Code:

@GET
     @Path("/reqvolatility")
     @Produces(MediaType.APPLICATION_JSON)

     public  long getReqVolatility() throws JsonParseException, JsonMappingException, IOException{

     String query = "{},{_id:0,levelId:1,reqID:1,creationTime:1,lastModified:1}";
     Query query1 = new BasicQuery(query); 
     query1.addCriteria(Criteria.where("creationTime").ne("lastModified"));
     long reqvolatility = getMongoOperation().count(query1,RequirmentVO.class);

     return reqvolatility;

    }

In the above code "creationTime" & "lastModified" columns in mongoDB.I'm comparing those two fields, but its not giving correct count. Is this correct? if it is wrong, How can i compare two fileds?


回答1:


Standard query operations do not compare the values of one field against another. In order to do this, you need to employ the JavaScript evaluation server side which can actually compare the two field values:

Assuming both fields are ISODate instances

 BasicQuery query = new BasicQuery(
        new BasicDBObject("$where", "this.creationTime.getTime() != this.lastModified.getTime()")
    );


来源:https://stackoverflow.com/questions/42838473/how-to-compare-two-strings-in-mongodb-spring-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!