Sumit Deshpande
Sumit Deshpande

Reputation: 105

Accessing array elements from MongoDB in Java

  db.Profile_Pic.find().pretty();
{
    "_id" : ObjectId("51cc08b505ca33ead716df9b"),
    "profile_id" : 101,
    "profile_pic" : [
        "1.jpg"
    ],
    "product_img" : [
        "1.jpg",
        "2.jpg"
    ],
    "company_logo" : [
        "1.jpg",
        "2.jpg"
    ]
}

I want to select individual elements from "product_img" array. I have written following code in java,`

While(f.hasNext())
{
String t=f.next().get("product_img").toString();
}

Above code return me the whole array of "product_img".

[ "1.jpg" , "2.jpg"]

I want to fetch individual element from "product_img". `

Upvotes: 0

Views: 404

Answers (1)

peshkira
peshkira

Reputation: 6239

Ok, since it works, I will transform my comment in an answer :)

You can try

BasicDBList list = (BasicDBList) f.next().get("product_img"); 

and then check the values in the list.

Here are the JavaDocs: http://api.mongodb.org/java/current/com/mongodb/BasicDBList.html

Upvotes: 1

Related Questions