java-在Mongo中插入JSON

前端之家收集整理的这篇文章主要介绍了java-在Mongo中插入JSON 前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我有一个使用Gson库创建的JSON对象.我想使用Java Mongo Driver v3.8.1将这个对象插入MongoDB中,并将UUID作为Ids和Integer的int64类型.但是,这似乎分别作为String和Int32类型插入.

JsonObject folderObject = new JsonObject();
folderObject.addProperty("id",UUID.randomUUID().toString());
folderObject.addProperty("cid",document.getCid());

我正在将其转换为BSON

org.bson.Document doc= org.bson.Document.parse(folderObject .toString());

并通过使用将其插入MongoDB

  mongoCollection.insertOne(doc);

我的document.getCid()的类型为Long,但仍作为int32插入,因此我无法将String以外的任何内容传递给folderObject.

最佳答案
我认为您不应该创建JsonObject并随后对其进行解析:

您可以简单地尝试一下:

    Document doc = new Document()
            .append("id",UUID.randomUUID())
            .append("cid",document.getCid());
    mongoCollection.insertOne(doc);
原文链接:https://www.f2er.com/java/532944.html

猜你在找的Java相关文章