How to convert a BasicDBObject file to a Mongo Document using the Mongo DB version 3 Java driver?

In the Java driver, Mongo DB version 3, the API has changed compared to version 2. So this code no longer compiles:

BasicDBObject personObj = new BasicDBObject(); collection.insert(personObj) 

The collection insert only works with a Mongolian document.

Working with old code I need to ask a question:

What is the best way to convert a BasicDBObject to a document?

+7
java mongodb
source share
4 answers

I think it’s easiest to just change your code to use a document, not BasicDBObject.

So change

 BasicDBObject doc = new BasicDBObject("name", "john") .append("age", 35) .append("kids", kids) .append("info", new BasicDBObject("email", " john@mail.com ") .append("phone", "876-134-667")); 

For

 import org.bson.Document; ... Document doc = new Document("name", "john") .append("age", 35) .append("kids", kids) .append("info", new BasicDBObject("email", " john@mail.com ") .append("phone", "876-134-667")); 

and then paste into the collection

 coll.insertOne(doc); 

You will need to change other bits of code to work with MongoDB 3+

MongoDatabase and DB MongoCollection vs DBCollection

+5
source share

We can convert BasicDBObject to Document as follows

 public static Document getDocument(DBObject doc) { if(doc == null) return null; return new Document(doc.toMap()); } 

as Document is an implementation of Map<String,Object> .

and BasicDBObject may be too much of a trick in DBObject , since BasicDBObject is an implementation of DBObject .

@ Black_Rider is for you.

+1
source share

The document is very similar to a BasicDBObject. I'm not quite sure what you call the way to convert BasicDBObjects files to documents, but the Document object provides some very useful methods.

Document.parse (string) will return the document if you pass it in a JSON string.

Document.append ("key", value) will add to the document fields.

As for the code in your post, it should compile with version 3:

 Document personObj = new Document(); collection.insertOne(personObj) 

Cm

Guide for Java Driver 3.0

and

MongoDB Java Driver Documentation

0
source share
 @SuppressWarnings("unchecked") public static Document getDocument(BasicDBObject doc) { if(doc == null) return null; Map<String, Object> originalMap = doc.toMap(); Map<String, Object> resultMap = new HashMap<>(doc.size()); for(Entry<String, Object> entry : originalMap.entrySet()) { String key = entry.getKey(); Object value = entry.getValue(); if(value == null) { continue; } if(value instanceof BasicDBObject) { value = getDocument((BasicDBObject)value); } if(value instanceof List<?>) { List<?> list = (List<?>) value; if(list.size() > 0) { // check instance of first element Object firstElement = list.get(0); if(firstElement instanceof BasicDBObject) { List<Document> resultList = new ArrayList<>(list.size()); for(Object listElement : list) { resultList.add(getDocument((BasicDBObject)listElement)); } value = resultList; } else { value = list; } } } resultMap.put(key, value); } Document result = new Document(resultMap); return result; } 
0
source share

All Articles