Mongo convert document to DBObject

Hi, I need to convert Mongo Document to DBObject (BasicDBObject).

I upload a file to mongo using GridFS and I want to set the metadata that I get in the document. I know that a Document is almost the same as a DBObject. I know I can do something like this:

Document doc = new Document();
BasicDBObject.parse(doc.toJson());

But is it useless very hard?

The gridFS method only setMetaData()accepts DBObject, so I need to convert it.

Is there a better way to do this rather than converting it to a string and vice versa?

+4
source share
1 answer

You are here micro-optimization.

However, since both classes are implementations of Map, you can simply do:

Document document = new Document();
BasicDBObject basicDBObject = new BasicDBObject(document);

Map#putAll, BasicDbObject.

+5

Source: https://habr.com/ru/post/1685581/


All Articles