Java 将JSON数组插入mongodb
我正在尝试将一个表示JSON数组的字符串插入到mongodb集合中Java 将JSON数组插入mongodb,java,json,mongodb,Java,Json,Mongodb,我正在尝试将一个表示JSON数组的字符串插入到mongodb集合中 String str = "[{\"id\":1,\"data\":\"data1\"},{\"id\":2,\"data\":\"data2\"},{\"id\":3,\"data\":\"data3\"}]"; DBObject dbObject = (DBObject) JSON.parse(str); collection.insert(dbObject); 但我有个例外 Exception in thread "ma
String str = "[{\"id\":1,\"data\":\"data1\"},{\"id\":2,\"data\":\"data2\"},{\"id\":3,\"data\":\"data3\"}]";
DBObject dbObject = (DBObject) JSON.parse(str);
collection.insert(dbObject);
但我有个例外
Exception in thread "main" java.lang.IllegalArgumentException: BasicBSONList can only work with numeric keys, not: [_id]
有人能告诉我正确的方法吗?根据insert()
可以接受单个DBObject
或数组或列表
因此,为了保存,您需要将JSON数组转换为一个数组/DBObjects列表,或者保存每个数组的项String JSON=“[{“id\”:1,“data\”:“data1\”,{“id\”:2,“data\”:“data2\”,{“id\”:3,“data\”:“data3\”;
String json = "[{\"id\":1,\"data\":\"data1\"},{\"id\":2,\"data\":\"data2\"},{\"id\":3,\"data\":\"data3\"}]";
MongoCredential credential = MongoCredential.createCredential("root", "sample", "root".toCharArray());
MongoClient mongoClient = new MongoClient(new ServerAddress("localhost"), Arrays.asList(credential));
MongoDatabase db = mongoClient.getDatabase("sample");
MongoCollection<Document> collection = db.getCollection("loginTracking");
List<Document> jsonList = new ArrayList<Document>();
net.sf.json.JSONArray array = net.sf.json.JSONArray.fromObject(json);
for (Object object : array) {
net.sf.json.JSONObject jsonStr = (net.sf.json.JSONObject) JSONSerializer.toJSON(object);
Document jsnObject = Document.parse(jsonStr.toString());
jsonList.add(jsnObject);
}
collection.insertMany(jsonList);
mongoClient.close();
MongoCredential credential=MongoCredential.createCredential(“根”、“样本”、“根”.tocharray());
MongoClient MongoClient=newmongoclient(新服务器地址(“localhost”)、Arrays.asList(凭证));
MongoDatabase db=mongoClient.getDatabase(“示例”);
MongoCollection collection=db.getCollection(“loginTracking”);
List jsonList=new ArrayList();
net.sf.json.JSONArray数组=net.sf.json.JSONArray.fromObject(json);
用于(对象:数组){
net.sf.json.JSONObject jsonStr=(net.sf.json.JSONObject)JSONSerializer.toJSON(object);
Document jsnObject=Document.parse(jsonStr.toString());
添加(jsnObject);
}
collection.insertMany(jsonList);
mongoClient.close();
我找到了实现这一目标的好方法:
(ArrayList<Document>) JSON.parse("[String json array]");
但问题是Document.parse()不适用于数组,所以我可以使用上面的行来解决它。最后的代码是:
Document objAddendumVersion = new Document();
objAddendumVersion.append("_id", new ObjectId());
objAddendumVersion.append("Array", (ArrayList<Document>) JSON.parse("[String json array]"));
documentobjaddendumversion=newdocument();
追加(“_id”,new ObjectId());
objAddendumVersion.append(“Array”,(ArrayList)JSON.parse(“[String JSON Array]”);
它工作得很好。是的,我知道有更多更好的方法可以做到这一点,但目前我正在使用这个
我等着这对有同样麻烦的人有用。你到处都有相同的id是正常的吗?我不认为这是真正的原因,但这是weird@vincent我编辑了代码。即使ID不同,也会出现相同的错误。您需要用数组保存单个文档还是多个文档?从MongoDB控制台添加字符串是可行的。因此,这可能是MongoDB Java驱动程序中的一个错误。@injecteer我想保存多个文档。虽然这段代码可能会回答这个问题,但提供有关这段代码为什么和/或如何回答这个问题的附加上下文可以提高其长期价值。
Document objAddendumVersion = new Document();
objAddendumVersion.append("_id", new ObjectId());
objAddendumVersion.append("Array", (ArrayList<Document>) JSON.parse("[String json array]"));