1

I want to query against a condition that the string concatenation of two fields is larger that a given value,something like the following,but I don't know how to write the queryCondition. Could someone help on this?

long t = 1479690653366;
String id = "5832499d63594c3b24030c19";

//There are _id and time fields in the document
DBCollection collection = ...
collection.find(<queryCondition>);

The logic of <queryCondition> is to find the documents whose concatenation of _time and _id column is larger than the concatenation of time and id, that is 14796906533665832499d63594c3b24030c19
2
  • can you explain the need for this query? there might be a more appropriate way of accomplishing what you want Commented Nov 21, 2016 at 7:30
  • Thanks @marmor. Basically, what I want is find sort limit. I want to query against timestamp field, but there are many documents with same timestamp value in my case(eg, 10k+). So, I want to combine the timestamp field and the _id field, and using collection.query to only find the documents that I have not fetched (I will checkpoint the largest timestamp and the _id that I have processed) Commented Nov 21, 2016 at 7:37

1 Answer 1

3

I don't think you need string concatenation here.

You want docs that have _time > 1479690653366, as well as docs that have _time == 1479690653366, but only if their _id > 5832499d63594c3b24030c19.

In that case you can query:

collection.find({ $or : [ 
    _time : { $gt : 1479690653366}, 
    { _time : "1479690653366", _id : { $gt : ObjectId("5832499d63594c3b24030c19") } } 
]});

Or in Java syntax, something like:

DBObject or_part1 = new BasicDBObject("_time", new BasicDBObject("$gt", 1479690653366));
DBObject or_part2 = new BasicDBObject("_time", 1479690653366).append("_id", new BasicDBObject("$gt", new ObjectId("5832499d63594c3b24030c19")));
BasicDBList or = new BasicDBList();
or.add(or_part1);
or.add(or_part2);
DBObject query = new BasicDBObject("$or", or);
collection.find(query);
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks @marmor, your answer looks really promising, let me try, Thanks very much!
Thanks @marmor,it works fine. a small thing in your code is that DBObject or_part2 = new BasicDBObject("_time", 1479690653366).append("_id", new ObjectId("5832499d63594c3b24030c19")); should be DBObject or_part2 = new BasicDBObject("_time", 1479690653366).append("$gt", new BasicDBObject("_id",new ObjectId("5832499d63594c3b24030c19")));
yep, actually I think it should be: .append("_id", new BasicDBObject("$gt" ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.