How to get POJO objects from db.runCommand#find results?


I am doing the examples from quick start

//the code from the link example,set registry etc

MongoDatabase db = mongoClient.getDatabase("sample_training");
MongoCollection<Grade> grades = db.getCollection("grades", Grade.class);

grades.find();  //works i get cursor of Grade instances

Document find_command = new Document().append("find","grades");
db.runCommand(find_command,Grade.class);  //doesnt work,i get 1 Grade instance with null fields
db.runCommand(find_command);  //doesnt work,i get a cursor with Documents not Grade instances


1)how to fix the runCommand to get a cursor Document where “firstBatch” will be an Arraylist of Grade instances (i need it because i am making a library)

2)when i use POJO i have perfomance penalty?
i mean at insert time it goes Grade → Document → BSON or Grade->BSON?
at read(decode) it goes BSON->Document->Grade or BSON->Grade?

Thank you

Hello @Takis, see this post for db.runCommand example code using Java Driver:

Hello thank you for trying to help me.

The default return of a cursor command is

{"cursor" : {"firstBatch" : [Document1,Document2 ... ], "id" : 0, "ns" "test.test"}, "ok" : 1.0}

I want to get back

{"cursor" : {"firstBatch" : [Grade1,Grade2 ... ], "id" : 0, "ns" "test.test"}, "ok" : 1.0}

And the main question is how to tell to the driver,to use Grade class instead of Document
when returning the runCommand result

I don’t know, how it can happen. But, the collection.find method returns Grade objects - I think the result you are looking for, i.e., a list of grade objects.

The runCommand:

returns a document that contains the cursor information, including the cursor id and the first batch of documents.

One way is to get the batch of documents and somehow map them individually to the Grade objects. But, thats a lot of work I think.

1 Like