0

Trying to create a file (json) for use by mongoimport utility to populate collections in mongodb to test performance of our application.

I have an existing spring boot application interacting with mongodb.

This is what I am trying to do:

  1. Create a Object in Java (say a class annotated with @Document that serves as the entity to persist in a collection)
  2. Convert that java object to BSON ===> not sure how to do this
  3. Append that to a json file

Thereafter use mongoimport to load the generated file into mongodb.

I have seen that we can use:

Map<String, Object> map =
        mapper.convertValue(SOME_JAVA_OBJECT, new TypeReference<Map<String, Object>>() {
        });
org.bson.Document d = new Document();
d.putAll(map);
String json = d.toJson();
System.out.println(json);

However, this JSON has LocaleDate as (this is not what I want):

"serviceDate": {"dayOfWeek": "THURSDAY", "dayOfYear": 231, "month": "AUGUST", "nano": 818930000, "year": 2021, "monthValue": 8, "dayOfMonth": 19, "hour": 11, "minute": 57, "second": 5, "chronology": {"id": "ISO", "calendarType": "iso8601"}}, 

What I need is to insert this into Mongo with the date value as:

serviceDate: ISODate("2021-08-14T06:37:15.722Z"

Essentially using Java to create a json file from existing POJO (@Document instances) for bulk upload using mongoimport utility.

3
  • MongoImport expects JSON, not BSON. Commented Aug 19, 2021 at 18:54
  • MongoRestore expects BSON Commented Aug 19, 2021 at 18:55
  • @barrypicker: corrected it. Thanks. Commented Aug 19, 2021 at 19:08

1 Answer 1

1

This is not a spring boot program, but just a raw driver access program, but this seems to provide eJSON for dates rather than LocalDate...

File pom.xml

<project
    xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>test.barry</groupId>
    <artifactId>test</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>test</name>
    <url>http://maven.apache.org</url>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.2.1</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${basedir}</outputDirectory>
                            <finalName>Test</finalName>
                            <transformers>
                                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>test.barry.Main</mainClass>
                                </transformer>
                            </transformers>
                            <createDependencyReducedPom>false</createDependencyReducedPom>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>org.mongodb</groupId>
            <artifactId>mongodb-driver-sync</artifactId>
            <version>4.2.0</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.30</version>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.2.3</version>
        </dependency>
    </dependencies>
</project>

File Main.java

package test.barry;

public class Main {

    public static void main(String[] args) {

        com.mongodb.MongoClientSettings mongoClientSettings = com.mongodb.MongoClientSettings.builder()
                .applyConnectionString(new com.mongodb.ConnectionString("mongodb://barry:barry@localhost:42011,localhost:42012,localhost:42013/?replicaSet=replSet&retryWrites=true&w=majority"))
                .build();

        com.mongodb.client.MongoClient client = com.mongodb.client.MongoClients.create(mongoClientSettings);
        
        com.mongodb.client.MongoDatabase db = client.getDatabase("javatest");
        com.mongodb.client.MongoCollection<org.bson.Document> collection = db.getCollection("test");


        // START WITH A CLEARED OUT EMPTY COLLECTION
        collection.deleteMany(new org.bson.BsonDocument());

        // INSERT A DOCUMENT
        org.bson.Document insertedDocument = new org.bson.Document();
        insertedDocument.append("createdDate", new java.util.Date());
        insertedDocument.append("firstName", "John");
        insertedDocument.append("lastName", "Doe");
        insertedDocument.append("age", 38);
        insertedDocument.append("happinessScore", 95.6);

        java.util.List<org.bson.Document> children = new java.util.ArrayList<org.bson.Document>();
        org.bson.Document child1 = new org.bson.Document();
        child1.append("firstName", "Timmy");
        child1.append("dateOfBirth", new java.util.Date("January 1, 2011"));
        children.add(child1);

        org.bson.Document child2 = new org.bson.Document();
        child2.append("firstName", "Tommy");
        child2.append("dateOfBirth", new java.util.Date("July 1, 2012"));
        children.add(child2);

        org.bson.Document child3 = new org.bson.Document();
        child3.append("firstName", "Tammy");
        child3.append("dateOfBirth", new java.util.Date("November 1, 2013"));
        children.add(child3);

        insertedDocument.append("children", children);

        org.bson.Document spouse = new org.bson.Document();
        spouse.append("firstName", "Jane");
        spouse.append("lastName", "Doe");
        spouse.append("age", 34);

        insertedDocument.append("spouse", spouse);
    
        System.out.println(String.format("Before insert ObjectId: %s", insertedDocument.get("_id")));
        collection.insertOne(insertedDocument);
        System.out.println(String.format("After insert ObjectId: %s", insertedDocument.get("_id")));
        System.out.println("");
        
        System.out.println("JSON value is:");
        System.out.println(insertedDocument.toJson());


        // --------------------------------------------------------------------------
        // QUERY DOCUMENT
        org.bson.conversions.Bson filter = com.mongodb.client.model.Filters.eq("_id", insertedDocument.get("_id"));
        org.bson.conversions.Bson sort = com.mongodb.client.model.Sorts.ascending("lastName");
        org.bson.conversions.Bson projection = com.mongodb.client.model.Projections.exclude("lastName");

        com.mongodb.client.FindIterable<org.bson.Document> iterable1 = collection.find(filter).sort(sort).skip(0).limit(1).projection(projection);

        // AUTO-CLOSABLE TRY
        try(com.mongodb.client.MongoCursor<org.bson.Document> cursor1 = iterable1.iterator())
        {
            while (cursor1.hasNext())
            {
                org.bson.Document queriedDocument1 = cursor1.next();
                System.out.println(String.format("queriedDocument1: %s", queriedDocument1));
                
                System.out.println("");
                
                System.out.println("JSON value is:");
                System.out.println(insertedDocument.toJson());
            }
        }
        
        System.out.println("");
    }
}

Results when Run

Before insert ObjectId: null
15:19:13.672 [main] DEBUG org.mongodb.driver.protocol.command - Sending command '{"insert": "test", "ordered": true, "writeConcern": {"w": "majority"}, "txnNumber": 1, "$db": "javatest", "$clusterTime": {"clusterTime": {"$timestamp": {"t": 1629411546, "i": 1}}, "signature": {"hash": {"$binary": {"base64": "P0qNdQj1rWGa9efu4STXEW4ObCU=", "subType": "00"}}, "keyId": 6966701957889130499}}, "lsid": {"id": {"$binary": {"base64": "kiLR686WT/qOe2yThNehWg==", "subType": "04"}}}, "documents": [{"_id": {"$oid": "611ed8e1a563bc5abdd7957c"}, "createdDate": {"$date": "2021-08-19T22:19:13.628Z"}, "firstName": "John", "lastName": "Doe", "age": 38, "happinessScore": 95.6, "children": [{"firstName": "Timmy", "dateOfBirth": {"$date": "2011-01-01T08:00:00Z"}}, {"firstName": "Tommy", "dateOfBirth": {"$date": "2012-07-01T07:00:00Z"}}, {"firstName": "Tammy", "dateOfBirth": {"$date": "2013-11-01T07:00:00Z"}}], "spouse": {"firstName": "Jane", "lastName": "Doe", "age": 34}}]}' with request id 12 to database javatest on connection [connectionId{localValue:7, serverValue:1784}] to server localhost:42012
15:19:13.984 [main] DEBUG org.mongodb.driver.protocol.command - Execution of command with request id 12 completed successfully in 346.59 ms on connection [connectionId{localValue:7, serverValue:1784}] to server localhost:42012
After insert ObjectId: 611ed8e1a563bc5abdd7957c

JSON value is:
{"createdDate": {"$date": "2021-08-19T22:19:13.628Z"}, "firstName": "John", "lastName": "Doe", "age": 38, "happinessScore": 95.6, "children": [{"firstName": "Timmy", "dateOfBirth": {"$date": "2011-01-01T08:00:00Z"}}, {"firstName": "Tommy", "dateOfBirth": {"$date": "2012-07-01T07:00:00Z"}}, {"firstName": "Tammy", "dateOfBirth": {"$date": "2013-11-01T07:00:00Z"}}], "spouse": {"firstName": "Jane", "lastName": "Doe", "age": 34}, "_id": {"$oid": "611ed8e1a563bc5abdd7957c"}}
15:19:14.005 [main] DEBUG org.mongodb.driver.protocol.command - Sending command '{"find": "test", "filter": {"_id": {"$oid": "611ed8e1a563bc5abdd7957c"}}, "sort": {"lastName": 1}, "projection": {"lastName": 0}, "limit": 1, "$db": "javatest", "$clusterTime": {"clusterTime": {"$timestamp": {"t": 1629411553, "i": 2}}, "signature": {"hash": {"$binary": {"base64": "if1LAHJtpGkJyNjBGcwbpUWXvho=", "subType": "00"}}, "keyId": 6966701957889130499}}, "lsid": {"id": {"$binary": {"base64": "kiLR686WT/qOe2yThNehWg==", "subType": "04"}}}}' with request id 13 to database javatest on connection [connectionId{localValue:7, serverValue:1784}] to server localhost:42012
15:19:14.006 [main] DEBUG org.mongodb.driver.protocol.command - Execution of command with request id 13 completed successfully in 4.11 ms on connection [connectionId{localValue:7, serverValue:1784}] to server localhost:42012
queriedDocument1: Document{{_id=611ed8e1a563bc5abdd7957c, createdDate=Thu Aug 19 15:19:13 PDT 2021, firstName=John, age=38, happinessScore=95.6, children=[Document{{firstName=Timmy, dateOfBirth=Sat Jan 01 00:00:00 PST 2011}}, Document{{firstName=Tommy, dateOfBirth=Sun Jul 01 00:00:00 PDT 2012}}, Document{{firstName=Tammy, dateOfBirth=Fri Nov 01 00:00:00 PDT 2013}}], spouse=Document{{firstName=Jane, lastName=Doe, age=34}}}}

JSON value is:
{"createdDate": {"$date": "2021-08-19T22:19:13.628Z"}, "firstName": "John", "lastName": "Doe", "age": 38, "happinessScore": 95.6, "children": [{"firstName": "Timmy", "dateOfBirth": {"$date": "2011-01-01T08:00:00Z"}}, {"firstName": "Tommy", "dateOfBirth": {"$date": "2012-07-01T07:00:00Z"}}, {"firstName": "Tammy", "dateOfBirth": {"$date": "2013-11-01T07:00:00Z"}}], "spouse": {"firstName": "Jane", "lastName": "Doe", "age": 34}, "_id": {"$oid": "611ed8e1a563bc5abdd7957c"}}
Sign up to request clarification or add additional context in comments.

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.