Creating test data in MongoDB

I want to generate test data for MongoDB. 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 can I generate data with this particular size?

+8
java mongodb
source share
5 answers

I donโ€™t understand what you are trying to achieve by setting the size to 200 MB.

You can add logical checks.

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

  • Object.bsonsize(..) - You can also check the size of the document before pasting to make it exactly 200 MB.

And also you can create a limited collection where you can notify the number of documents or the size of the collection.

Hope this helps.

+4
source share

What I could do is create a limited collection of 200 MB in size (209715200 bytes):

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

Then insert notes as you do. Then, at intervals within the for loop, check to see if the collection is full (or nearly full).

So, in your code, maybe (completely pseudocode):

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

Why do you need to copy the same data 100 times to make 200 bit test data? Instead of this

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

OR

  1. Using a 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 double-byte char strings and 10 eight-byte numbers => 240B

240B * 873813 = 200 MB

+2
source share

A quick dirty solution based on the suggestions of Robert Oud and Somnath Muluk:

+1
source share

create a field of type nvarchar , then generate a .txt file on your desktop with 200 MB of data. then put this line in this field. what he.

+1
source share

All Articles