无法在mongodb中插入多个值

时间:2013-10-07 03:11:50

标签: java mongodb hadoop

我只能将第一个值插入mongodb.how我可以编辑代码以将所有值从循环插入到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。我的代码中有什么问题吗?

1 个答案:

答案 0 :(得分:0)

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

BasicDBObject out = new BasicDBObject();
                 out.put("final",jsonobj );
                 collection.insert(out);