简体   繁体   中英

Iterating multiple documents using cursor in Mongo

I am facing an issue while iterating documents in mongo database using Java code. I am iterating records on the basis of User_ID value as "demo" in the collection. While iteration its storing the most recent record instead of all matching data. The code is as below:

public BasicDBObject Demo1(String User_ID) throws Exception {
        DB db = ConnectToDB.getConnection();
        DBCollection collection = db.getCollection("demo");
        BasicDBObject document = new BasicDBObject();
        BasicDBObject Project_Detail = new BasicDBObject();
        document.put("User_ID", User_ID);
        field.put("ProjectName", 1);
        field.put("ProjectNumber", 1);
        field.put("_id", 0);
        DBCursor cursor = collection.find(document, field);
while (cursor.hasNext()) {
                    BasicDBObject object=new BasicDBObject();
                    object = (BasicDBObject) cursor.next();

                    Project_Detail.append("ProjectName", object.get("ProjectName"));
                    Project_Detail.append("ProjectNumber", object.get("ProjectNumber"));
                    System.out.println("Project_Detail value is:" + Project_Detail);

                }
return Project_Detail;
}

My mongo collection is as below, which is having two documents:

{
    "_id" : ObjectId("59dc85905f0446bfb39417f5"),
    "User_ID" : "demo",
    "ProjectNumber" : "1",
    "ProjectName" : "Project1",
 },
{
    "_id" : ObjectId("59dc85a15f0446bfb39417f9"),
    "User_ID" : "demo",
    "ProjectNumber" : "2",
    "ProjectName" : "Project2",
 }

When I run the above code, it shows the following result for given User_ID: "demo":

 {
    "ProjectName": "Project2",
    "ProjectNumber": "2"}

But the desired result should be :

{
    "ProjectName": "Project1",
    "ProjectNumber": "1"},
{
    "ProjectName": "Project2",
    "ProjectNumber": "2"}

It seems like it is updating the record in second counter and hence so updating the value in the second document in the second iteration. Please specify If I can fetch both the matching records and store these in the result. Thanks for any help.

In second iteration you are replacing values of same key. That's why only one value remains in result. You should use BasicDBList to store multiple BasicDBObject :

Eg

BasicDBList Project_Detail = new BasicDBList();
while (cursor.hasNext()) {
                    BasicDBObject object=new BasicDBObject();
                    object = (BasicDBObject) cursor.next();
                    BasicDBObject temp = new BasicDBObject();
                    temp.append("ProjectName", object.get("ProjectName"));
                    temp.append("ProjectNumber", object.get("ProjectNumber"));
                    Project_Detail.add(temp);

                }

Convert BasicDBList to BasicDBObject :

    BasicDBObject result = new BasicDBObject();
    result.putAll(Project_Detail.toMap());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM