1

Hi I am using the Java API for connecting to my DB. I am using the new Classes to connecto since the ones in almost all examples are marked deprecated I use MongoClient to connect to MongoDatabase db. After that I do:

MongoCollection<Document> coll = db.getCollection("collName");
AggregateIterable<Document> res = collection.aggregate(myquery);
for(Document d : res)
{
    System.out.println(d.toJson());
}

It doesn't print anything. I tried to use result.first() too but it only prints null.

Now the reason I don't post specifics about my data an query is the following. If I look in the the log file in /var/log/mongodb I see the translated query. If I post THE EXACT query into the mongo shell. It just works like a charm. I found a post on here the says that the order of my aggregate functions could be a problem but that doesn't make sense if it works if pasted in the shell.

So here is my little altered query

Document sort = new Document("$sort", new Document("Day", 1));

    Document matchBeforeUnwindDayOfTheWeek = new Document("Day", day);

    Document matchBeforeUnwindVar1 = new Document("'value.Data'", new Document("$elemMatch",
            new Document("var1", new Document("$gt", minvar1).append("$lt", maxvar1))));        
    Document matchBeforeUnwindVar2 = new Document("'value.Data'", new Document("$elemMatch",
            new Document("var2", new Document("$gt", mingvar2).append("$lt", maxvar2))));

    List<Document> matchBeforeUnwindAnd = new LinkedList<Document>(); 
    matchBeforeUnwindAnd.add(matchBeforeUnwindDayOfTheWeek);
    matchBeforeUnwindAnd.add(matchBeforeUnwindVar1);
    matchBeforeUnwindAnd.add(matchBeforeUnwindVar2);

    Document matchBeforeUnwind = new Document("$match", new Document("$and", matchBeforeUnwindAnd));

    Document unwind = new Document("$unwind", "$value.Data");

    Document matchAfterUnwindVar1 = new Document("'value.Data.var1'",
            new Document("$gt", minvar1).append("$lt", minvar1));
    Document matchAfterUnwindVar2 = new Document("'value.Data.var2'",
            new Document("$gt", minvar2).append("$lt", maxvar2));

    List<Document> matchAfterUnwindAnd = new LinkedList<Document>(); 
    matchAfterUnwindAnd.add(matchAfterUnwindVar1);
    matchAfterUnwindAnd.add(matchAfterUnwindVar2);

    Document matchAfterUnwind = new Document("$match", new Document("$and", matchAfterUnwindAnd));

    Document groupFields = new Document("_id", "$_id");
    groupFields.put("Grouped", new Document("$push", "$value.Data"));

    Document group = new Document("$group", groupFields);

    List<Document> query = new LinkedList<Document>();
    query.add(sortByDayOfTheWeek);
    query.add(matchBeforeUnwind);
    query.add(unwind);
    query.add(matchAfterUnwind);
    query.add(group);

    AggregateIterable<Document> result = collection.aggregate(query);

Edit: I tried it with a very simple query like the one posted below. Queries still translate fine in the log file.

4
  • 2
    Well the problem is likely in the aggregation pipeline definition, and that is completely absent in your question. Please edit your question. Commented Jul 31, 2015 at 10:01
  • Yes but how could that be? I mean I can see my pipeline in the log file after it executes and if I copy paste it in the shell it just works. Commented Jul 31, 2015 at 10:02
  • 1
    I think the point I was making is "we" cannot see it. How about sharing as maybe ( likely ) there is a problem there that you don't see but someone else will Commented Jul 31, 2015 at 10:51
  • Ok sorry about that but I had to change a couple of things in the code but here it is Commented Jul 31, 2015 at 11:30

2 Answers 2

1

With the ref. of mongo java driver you should change for statement as below :

import com.mongodb.*;
import com.mongodb.Block;
import com.mongodb.client.AggregateIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;
import org.bson.types.ObjectId;
import static java.util.Arrays.asList;

 MongoClient mongoClient = new MongoClient("localhost", 27017);
 MongoDatabase database = mongoClient.getDatabase("demo");
 MongoCollection < Document > collection = database.getCollection("collectionName");
 AggregateIterable < Document > res = collection.
 aggregate(asList(
     new Document("$match", new Document("_id", new ObjectId("55a8ad7f68d7f0852ea1c8a7")))));

 res.forEach(new Block < Document > () {
       @Override
     public void apply(final Document document) {
         System.out.println(document.toJson());
     }
 });
Sign up to request clarification or add additional context in comments.

6 Comments

I tried that but it did not help. If I look into the logfile I see that the reslen is very small for the return to the java application. So it doesn't seem to be a problem with me processing but the return value.
Ohh and I am fairly sure the server really is doing the aggregation since the time needed are pretty much the same for the shell and the API
is better way to post your query formation code, because I tested the above code and it work fine.
Is it possible that the following could create a problem. I use BasicDBObjects for the query, but the return value is documents. Is it possible that that could cause problems?
so problem in your query formation changed to BasicDBObjects to new Document for reference check edited answer.
|
0

The error was the '' characters. I thought I needed them because of the .-operator. Oh well. Thank you for all your help

Comments

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.