15

In the Java Mongo DB driver version 3 the API has changed as compared to the version 2. So a code like this does not compile anymore:

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

A Collection insert works only with a Mongo Document.

Dealing with the old code I need to ask the question:

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

1
  • 1
    You may want to see this Commented Aug 12, 2015 at 14:46

4 Answers 4

14

We Can Convert BasicDBObject to Document by the following way

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

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

and BasicDBObject can be too be catch in DBObject as BasicDBObject is an implementation of DBObject.

@Black_Rider for you too

Sign up to request clarification or add additional context in comments.

Comments

10

I think the easiest thing to do is just change your code to use a Document as opposed to BasicDBObject.

So change

BasicDBObject doc = new BasicDBObject("name", "john")
    .append("age", 35)
    .append("kids", kids)
    .append("info", new BasicDBObject("email", "[email protected]")
    .append("phone", "876-134-667"));

To

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

and then insert into the collection

coll.insertOne(doc);

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

MongoDatabase vs. DB MongoCollection vs DBCollection

Comments

3

The Document is very similar to the BasicDBObject. I am not quite sure what you are referring to as a way to convert BasicDBObjects to Documents, but the Document object provides some very useful methods.

Document.parse(string) will return a Document if you feed it in a JSON string.

Document.append("key", Value) will add to the fields of a Document.

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

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

See

Java Driver 3.0 Guide

and

MongoDB Java Driver 3.0 Documentation

2 Comments

What is I receive a BasicDBObject from an old method and then I want to insert it?
You could do Document doc = Document.parse(YourBasicDBObject.toString());
1
@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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.