您好,我需要将 Mongo Document 转换为 DBObject (BasicDBObject)。
我正在使用 GridFS 将文件上传到 mongo,并且我想设置在 document.xml 中获取的元数据。我知道 Document 与 DBObject 几乎相同。我知道我可以这样做:
Document doc = new Document();
BasicDBObject.parse(doc.toJson());
但这不是不必要的性能负担吗?
gridFS 方法 setMetaData()
只接受 DBObject
所以我必须转换它。
有没有更好的方法来做到这一点,而不是将其转换为字符串并返回?
最佳答案
您在这里进行了微优化。
但是,由于这两个类都是 Map 的实现,您可以这样做:
Document document = new Document();
BasicDBObject basicDBObject = new BasicDBObject(document);
这在内部执行 Map#putAll
操作,将 Document 映射的所有条目放入 BasicDbObject 映射。
https://stackoverflow.com/questions/46191940/
相关文章:
javascript - 为已登录的用户手动创建 Passport session
json - 使用 Python 从 MongoDB 文档创建 JSON 文件
java - Spring MongoTemplate - 将聚合结果映射到集合(例如 List 和
mongodb - 使用键值对将 mongo 数组转换为对象
node.js - 我应该使用什么数据类型来使用 MongoDB 存储图像?
node.js - Mongoose 类型错误 : User is not a constructo
c# - ReadBsonType 只能在 State 为 Type 时调用,不能在 State 为