将JSON解析为MONGODB文档 [英] Parse JSON into a MONGODB docment

查看:1374
本文介绍了将JSON解析为MONGODB文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 JAVA MONGODB 的新手,并且一直在学习如何尝试并了解这些技术是否会满足我对产品的要求。

I am new to JAVA and MONGODB and have been learning both to try and understand if these technologies would meet my requirements for a product.

我目前陷入困境,无法将 JAVA 中的文件(记录)插入我的 MONGODB 集合。

I am currently stuck in a point where I am not able to insert documents(records) from JAVA into my MONGODB collection.

我使用新的 MONGODB 版本3.0

到目前为止的代码

MongoCollection<Document> coll = db.getCollection("Collection");
String json = "{'class':'Class10', 'student':{'name':'Alpha', 'sex':'Female'}, {'name':'Bravo', 'sex':'Male'}}";

我找到了将其转换为 DBObject的代码 type。

I have found code to convert this to a DBObject type.

DBObject dbObject = (DBObject)JSON.parse(json);

但我猜新版 MONGODB 没有insert方法,而是具有 insertOne方法

But I guess the new version of MONGODB does not have the insert method but instead has the insertOne method.

coll.insertOne()要求输入采用Document格式,不接受DBObject格式。

coll.insertOne() requires that the input be in the Document format and does not accept the DBObject format.

coll.insertOne((Document) dbObject);

给出错误

com.mongodb.BasicDBObject cannot be cast to org.bson.Document

可以有人帮我正确的类型铸造,并给我一个链接,我可以找到并学习相同的?

Can someone help me with the right type casting and give me a link where I could find and learn the same?

问候。

推荐答案

使用静态 parse(String json)方法定义于文档类。

Use the static parse(String json) method that's defined in the Document class.

这篇关于将JSON解析为MONGODB文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆