Skip to content

Yet another programming solutions log

Sample bits from programming for the future generations.

Technologies Technologies
  • Algorithms and Data Structures
  • Java Tutorials
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Spock Framework Tutorial
  • Spring Framework
  • Bash Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Developer’s Tools
  • Productivity
  • About
Expand Search Form

MongoDB JSON to Document/DBObject

farenda 2017-04-13 0

MongoDB integrates with JSON very well, but how to convert JSON to Document/DBObject using MongoDB API? In this post we show two solutions.

Conversion between JSON and Document/DBObject is especially useful during testing, when we export some documents from MongoDB as JSON and want to load in tests to perform computations.

MongoDB 3 – JSON to Document

Since MongoDB 3 the API is different and the basic class is org.bson.Document. To convert JSON into Document we can use Document.parse(String) like so:

String json = "{$match: {\"country\": {$in: [\"PL\"]}}}";
Document doc = Document.parse(json);

Document match = doc.get("$match", Document.class);
System.out.printf("$match: '%s'%n", match);

When run, the above code produces the following output:

$match: 'Document{{country=Document{{$in=[PL]}}}}'

As you can see, inner documents are also represented as Document objects.

MongoDB – JSON to DBObject

If you are still using the old MongoDB API and want to convert JSON to DBObject, then you can use JSON.parse(String) from com.mongodb.util package. The old API is less convenient, because it returns Object everywhere and you have to cast result to DBObject, but it gets the job done:

String json = "{$match: {\"country\": {$in: [\"PL\"]}}}";
DBObject dbObject = (DBObject) JSON.parse(json);

DBObject match = (DBObject) dbObject.get("$match");
System.out.printf("$match: '%s'%n", match);

The printed representation of DBObject is quite different than of Document, but that’s how it is in life. ;-)

$match: '{ "country" : { "$in" : [ "PL"]}}'

References:

  • How to export JSON data from MongoDB
Share with the World!
Categories MongoDB Tags json, mongodb
Previous: How to generate random test data in MongoDB
Next: MongoDB join collections

Recent Posts

  • Java 8 Date Time concepts
  • Maven dependency to local JAR
  • Caesar cipher in Java
  • Java casting trick
  • Java 8 flatMap practical example
  • Linked List – remove element
  • Linked List – insert element at position
  • Linked List add element at the end
  • Create Java Streams
  • Floyd Cycle detection in Java

Pages

  • About Farenda
  • Algorithms and Data Structures
  • Bash Tutorial
  • Bean Validation Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Java 8 Streams and Lambda Expressions Tutorial
  • Java Basics Tutorial
  • Java Collections Tutorial
  • Java Concurrency Tutorial
  • Java IO Tutorial
  • Java Tutorials
  • Java Util Tutorial
  • Java XML Tutorial
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Software Developer’s Tools
  • Spock Framework Tutorial
  • Spring Framework

Tags

algorithms bash bean-validation books clojure design-patterns embedmongo exercises git gof gradle groovy hateoas hsqldb i18n java java-basics java-collections java-concurrency java-io java-lang java-time java-util java-xml java8 java8-files junit linux lists log4j logging maven mongodb performance quartz refactoring regex rest slf4j solid spring spring-boot spring-core sql unit-tests

Yet another programming solutions log © 2022

sponsored