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; 
}
Taryn
  • 242,637
  • 56
  • 362
  • 405
Abhilash PS
  • 734
  • 1
  • 11
  • 25

1 Answers1

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);
            }
        }
    }
}
Rob Moore
  • 3,343
  • 17
  • 18
  • Thanks @RobMoore, I am going to try that out. – Abhilash PS Jun 26 '13 at 06:40
  • 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`. – Abhilash PS Jun 27 '13 at 08:10
  • 1
    You can use the 'in' method on the query builder: http://www.allanbank.com/mongodb-async-driver/apidocs/com/allanbank/mongodb/builder/ConditionBuilder.html#in(com.allanbank.mongodb.builder.expression.Constant...) - e.g. ....and("userId").in(Expressions.constant(1), Expressions.constant(12,Expressions.constant(3)) – Rob Moore Jun 27 '13 at 21:43