9

I want to generate test data for MongoDB. The size should be 200 Mb. I tried this code:

@Test
    public void testMongoDBTestDataGenerate()
    {
        MongoClient mongoClient = new MongoClient("localhost", 27017);

        DB db = mongoClient.getDB("development");
        DBCollection collection = db.getCollection("ssv");

        for (int i = 0; i < 100; i++)
        {
            BasicDBObject document = new BasicDBObject();
            document.put("database", "test");
            document.put("table", "hosting");

            BasicDBObject documentDetail = new BasicDBObject();
            documentDetail.put("records", 99);
            documentDetail.put("index", "vps_index1");
            documentDetail.put("active", "true");

            document.put("detail", documentDetail);

            collection.insert(document);
        }

        mongoClient.close();

    }

How I can generate data exactly with this size?

5 Answers 5

4

I am not getting what are you trying to achieve by setting size 200 Mb.

You can add logical checks.

  1. db.testCollection.stats() - You can check size of the collection before every insert.

  2. Object.bsonsize(..) - Also you can check document size before inserting to make it exactly 200 MB.

And also you can make capped collection where you can notify number of documents or size of collection.

Hope this helps.

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

1 Comment

db.testCollection.stats() Object.bsonsize() Given some pointers. Help yourself. @PeterPenzov
2

What I would probably do is create a capped collection with size 200MB (209715200 bytes):

db.createCollection( "ssv", { capped: true, size: 209715200 } )

Then insert records as you are doing. Then at intervals inside the for loop, check if the collection if full (or almost full).

So in your code, maybe (totally pseudo code):

if(i % 10 == 0) {
   if(db.ssv.stats().size >= 209715100){ //Or an arbitrary value closer to 200MB
     break;
   }
}

Comments

2
+50

Why would you copy the same data 100 times to make 200mb worth test data? Instead

1.append a counter to the value so that you can generate data sequentially

OR

  1. Use random function to generate random data
@Test
    public void testMongoDBTestDataGenerate()
    {
        MongoClient mongoClient = new MongoClient("localhost", 27017);

        DB db = mongoClient.getDB("development");
        DBCollection collection = db.getCollection("ssv");
        int counter=0;
        for (int i = 0; i < 873813; i++)
        {
            BasicDBObject document = new BasicDBObject();
            document.put("database", "test");
            document.put("table", "hosting");

            BasicDBObject documentDetail = new BasicDBObject();
            documentDetail.put("counter0", counter++);
            documentDetail.put("counter1", counter++);
            documentDetail.put("counter2", counter++);
            documentDetail.put("counter3", counter++);
            documentDetail.put("counter4", counter++);
            documentDetail.put("counter5", counter++);
            documentDetail.put("counter6", counter++);
            documentDetail.put("counter7", counter++);
            documentDetail.put("counter8", counter++);
            documentDetail.put("counter9", counter++);

            document.put("detail", documentDetail);

            collection.insert(document);
        }

        mongoClient.close();

    }
}

10 eight two-bytes char strings and 10 eight-bytes numbers => 240B

240B * 873813 = 200MB

1 Comment

Yes sure. Assuming that you are using Java and hence can utilize Random class object. Values we are setting to the counter variable can rather be replaced by the object of Random class. E.g. Random counter = new Random(); documentDetail.put("counter0",counter.nextInt(100)); Here - nextInt(100) generates random number from 0-100. Can be changed as per the need/requirement
1

A quick dirty solution based on Robert Udah and Somnath Muluk suggestions:

2 Comments

Ok, what is the best solution?
It's the same suggestion :)
1

create a field with type nvarchar then generate a .txt file in your for example desktop with 200MB data. then put this string into that field. that's it.

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.