0

我只能将第一个值插入 mongodb。如何编辑代码以将循环中的所有值插入 mongodb 下面是我的代码。

public class FormatDriver extends Configured implements Tool{
    //my code
    public static void main(String[] args) throws Exception {

        try{
            /*connection Mongodb*/

            while((clusterdata = cluster.readLine())!= null){
             String[] str_array = clusterdata.split("\\[");
             String star_tstr = str_array[0];
             String end_str = str_array[1]; 

             String end_array[] = end_str.split(",");

            BasicDBObject book = new BasicDBObject();   

             for(int k=0 ; k < doc_array.length ; k++){
            /*Formating the output*/
                 if(k%2 == 0){
                     if(end_array[0].equals(doc_array[k])){
                         end_array[0] = doc_array[k+1]; 
                book.put("docname",end_array[0]);  //value1
                         book.put("clusterno",star_tstr );  //value 2
                         books.insert(book);
                             /*Retrieve*/

                         DBCursor cursor = books.find(book);   
                         while(cursor.hasNext())    
                               {    
                                  System.out.println("in mongo while");
                                  System.out.println(cursor.next());   

                               }
                    }
                    }
                    }
                    }
                    }

                    }

谁能告诉我如何将所有值输入到 mongodb。我的代码有什么问题吗?

4

1 回答 1

0

我找到了答案只需将它们包装在 JSON 对象中并

BasicDBObject out = new BasicDBObject();
                 out.put("final",jsonobj );
                 collection.insert(out);
于 2013-10-08T06:45:54.950 回答