0

Mongodb and JSP :

my code :


DBCursor curseur = table.find();

while (curseur.hasNext()) { 
    DBObject ligne = curseur.next();

    out.println(ligne.get("_id")); 
    out.println(ligne.get("joueur")); 
    out.println(ligne.get("equipes"));
    out.println(ligne.get("matchs"));
}

the code runs normally but the problem is in the display :


_id 1  
Joueur  { "nom" : "De Oliveira" , "prenom" : "Denilson"}  
Equipes [ "Sao Paulo FC" , "Bétis Seville" , "Bordeaux"]  
Matchs  [{ "date_m" : "1996" , "stade" : "Morumbi"}, 
         { "date_m" : "1999" , "stade" : "Benito Villamarn"}, 
         { "date_m" : "2005" , "stade" : "Chaban-Delmas"}]  

It's correct but :

  1. How can I know the number of columns?

  2. How can I access the sub documents?

I want to display a list of data with this structure (without the braces and brackets):


id : 1

Joueur : nom : De Oliveira
        prenom : Denilson

Equipes : Sao Paulo FC, Betis Seville, Bordeaux

matchs : date_m : 1996
         stade : Morumbi

         date_m : 1999
         stade : Benito Villamarn

         date_m : 2005
         stade : Chaban-Delmas

Thank you in advance for your cooperation !

Yurii
  • 4,811
  • 7
  • 32
  • 41

2 Answers2

0

The number of "columns" can fetched using dbObject.size(). Subdocuments are just fields backed by other DBObjects so you fetch them the same way as any other field.

evanchooly
  • 6,102
  • 1
  • 16
  • 23
  • For the number of columns it's ok ! thanx ! but for subdocuments display I don't understand ! I changed code : DBCollection coll = db.getCollection("table_auteur"); DBCursor cur = coll.find(); for (DBObject doc : cur) { String desc = (String) doc.get("description_generale"); out.println(desc); DBObject auteur = (BasicDBObject) doc.get("auteurs"); String nom_auteur = (String) auteur.get("nom"); out.println(reportName); } => Error : java.lang.ClassCastException: com.mongodb.BasicDBList cannot be cast to com.mongodb.BasicDBObject ???? – user3595568 May 02 '14 at 13:20
  • You're casting to BasicDBObject and not DBObject. BasicDBList is not a BasicDBObject but *is* a DBObject. – evanchooly May 02 '14 at 14:01
  • I'm sorry ! but I don't understand ! BasicDBList auteurs = ((DBObject)) doc.get("matchs"); String stade = (String) auteurs.get("stade"); String date_m = (String) auteurs.get("date_m"); – user3595568 May 02 '14 at 14:46
  • DBObject auteur = (DBObject) doc.get("auteurs"); – evanchooly May 02 '14 at 15:31
  • I put the new code : DBObject matchs = (DBObject) doc.get("matchs"); String date_m = (String) matchs.get("date_m"); String stade = (String) matchs.get("stade"); ! Error => BasicBSONList can only work with numeric keys, not: [stade] – user3595568 May 02 '14 at 15:56
0

If you want fetch the data inside an inner array of your document you need store it in a ArrayList or List of java, like this:

cursor = document.find(query);
     if(cursor.hasNext()) {
     ArrayList<?> Matchs = (BasicDBList) cursor.next().get("Matchs");
             /* here you can iterate over Matchs arrayList and get his data */
             for(int i=0; i< Matchs.size(); i++){
                   JsonParser jsonParser = new JsonParser();
                   JsonElement jsonElement = jsonParser.parse(String.valueOf(Matchs.get(i)));

                if (jsonElement.isJsonObject()) {
                   JsonObject jsonObject = jsonElement.getAsJsonObject();
                   String date_m = jsonObject.get("date_m").getAsString();
                   String stade = jsonObject.get("stade").getAsString();
                }
             }
     }

NOTE: You need add to you project a jar of google.gson, to deserialize more easily your bson object

https://code.google.com/p/google-gson/downloads/detail?name=google-gson-2.2.4-release.zip

I hope it helps.

hcarrasko
  • 2,320
  • 6
  • 33
  • 45
  • Thanks ! but : ArrayList> Matchs = (BasicDBList) cursor.next().get("Matchs"); => Error : BasicDBList cannot be resolved to a type – user3595568 May 02 '14 at 14:18
  • are you doing the import of : `import com.mongodb.BasicDBList;` ?? – hcarrasko May 02 '14 at 14:29
  • I imported the package! Now there is no error ! thanks ! but when I run my application => Error : java.lang.NullPointerException !! DBCollection coll = db.getCollection("table_auteur"); DBCursor cur = coll.find(); if(cur.hasNext()) { ArrayList> Matchs = (BasicDBList) cur.next().get("matchs"); ...... – user3595568 May 02 '14 at 14:36