我想为 MongoDB 生成测试数据。大小应为 200 Mb。我试过这段代码:
@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();
}
我如何才能生成恰好具有此大小的数据?
最佳答案
我没有得到您想要通过设置大小 200 Mb 实现的目标。
您可以添加逻辑检查。
db.testCollection.stats()
- 您可以在每次插入之前检查集合的大小。Object.bsonsize(..)
- 您还可以在插入之前检查文档大小,使其恰好为 200 MB。
您还可以设置上限集合,您可以在其中通知文档数量或集合大小。
希望这对您有所帮助。
关于java - 在 MongoDB 中创建测试数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38152073/