K
K
kambulovd2019-11-24 21:13:45
Java
kambulovd, 2019-11-24 21:13:45

Can't find error in JAVA code?

There is a newclicklogs.json file. I'm trying to get it into my MOngoDB. I can not understand what is wrong. Help PLEASE!

public static void main(String[] args)throws IOException{
        	mongo mongoDemo = new mongo();	
        	
            MongoClient mongoClient = new MongoClient( "localhost" , 27017 );
            MongoDatabase database = mongoClient.getDatabase("KAMBUL");// initialize to the path of the file to read from
            MongoCollection<Document> collection = database.getCollection("asda");
            String inputFilename="C:\\data/newclicklogs.json";                  // initialize to the path of the file to read from
              // initialize to the collection to which you want to write

            BufferedReader reader = new BufferedReader(new FileReader(inputFilename));
            List<InsertOneModel<Document>> docs = new ArrayList<>();
            try {
                String json;
                
              
                while ((json = reader.readLine()) != null) {
                	Document document = Document.parse(String.format("{\"a\": %s}", json));
                	for(Document doc:document.get("a")) //     РУГАЕТСЯ ЗДЕСЬ
                	{docs.add(new InsertOneModel<>(doc));}
                    
                } 
            } finally {
                reader.close();
            }

        	
        }
        
  }

ERROR:Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Can only iterate over an array or an instance of java.lang.Iterable
at mognoDB.mongo.main(mongo.java:64)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Moseychuk, 2019-11-24
@fshp

It is written in black and white that only arrays and Iterable can be iterated. What does get return? That's right, Object.
https://api.mongodb.com/java/3.2/org/bson/Document...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question