0

I am using java async driver for mongoDb not the default driver

I have a db 'test' with collection 'collect' with each document in the form given below where time is the 10 digit unix timestamp

{ "_id", "userId", "programId", "time" }

I wanted to write a query which is equivalent to the sql query given below where input1 is the current time in unix timestamp format and input2 is a userId as an input

SELECT * 
FROM collect 
WHERE time >= input1 AND userId = input2 
ORDER BY time DESC 
LIMIT 30

I did something like this

collection = db.getCollection("ProgramBookings"); Long Datetimesatmp =
new Date().getTime() / 1000;

Aggregate.Builder builder = new Aggregate.Builder();

builder = (builder.match(where("time").
          greaterThan(Datetimesatmp.toString()).and("userId")     .equals(userId.toString())).sort(desc("time"))).limit(30);

Aggregate d1 = builder.build();

here I meant only to retrieve list 'time' which follows the criteria. But I am stuck here couldn't find much helpful links after googling. I referred this link to do the above given code. Is there a simple way to do the same.

EDIT: And I want to add the values to a List of ProgramTime objects which is like

public class ProgramTime {

    private Integer userId;     
      private Integer programId;    
      private Long time; 
}

1 Answer 1

1

I don't think that the Aggregation framework is the right choice here. I would just do a straight 'find'. There is a Find class and nested Find.Builder class for constructing the more complex queries.

import static com.allanbank.mongodb.builder.QueryBuilder.where;

import com.allanbank.mongodb.MongoClient;
import com.allanbank.mongodb.MongoCollection;
import com.allanbank.mongodb.MongoFactory;
import com.allanbank.mongodb.MongoIterator;
import com.allanbank.mongodb.bson.Document;
import com.allanbank.mongodb.builder.Find;
import com.allanbank.mongodb.builder.Sort;

public class StackOverFlow {
    // SELECT * FROM collect
    // WHERE time >= input1 AND userId = input2
    // ORDER BY time DESC
    // LIMIT 30
    public static void query(long input1, String input2) {
        MongoClient client = MongoFactory
                .createClient("mongodb://localhost:27017/");

        // SELECT * FROM collect -- Kinda...
        MongoCollection collection = client.getDatabase("test").getCollection(
                "collect");

        Find.Builder builder = Find.builder();
        // WHERE time >= input1 AND userId = input2
        builder.query(where("time").greaterThan(input1).and("userId")
                .equals(input2));
        // ORDER BY time DESC
        builder.sort(Sort.desc("time"));
        // LIMIT 30
        builder.limit(30);

        try (MongoIterator<Document> iter = collection.find(builder)) {
            for (Document doc : iter) {
                System.out.println(doc);
            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks @RobMoore, I am going to try that out.
Hi @RobMoore, What if the query is to be like SELECT * FROM collect WHERE time >= input1 AND userId IN (1, 2, 3, ... ) ORDER BY time DESC LIMIT 30. How can I include IN (1,2,3 ...) instead of = input2.
You can use the 'in' method on the query builder: allanbank.com/mongodb-async-driver/apidocs/com/allanbank/… - e.g. ....and("userId").in(Expressions.constant(1), Expressions.constant(12,Expressions.constant(3))

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.