如何通过mongodb Java驱动程序3.4+;使用capped collection添加多个文档;?
我想添加几个文档,使用和编写如下内容:如何通过mongodb Java驱动程序3.4+;使用capped collection添加多个文档;?,java,mongodb,Java,Mongodb,我想添加几个文档,使用和编写如下内容: // this code insert only first document using capped collection MongoDatabase database = mongoClient.getDatabase("db"); database.createCollection("capped_collection", new CreateCollectionOptions().capped(t
// this code insert only first document using capped collection
MongoDatabase database = mongoClient.getDatabase("db");
database.createCollection("capped_collection",
new CreateCollectionOptions().capped(true).sizeInBytes(536870912).maxDocuments(5000));
MongoCollection<Document> collection = database.getCollection("capped_collection");
Document found = database.getCollection("capped_collection").find(new Document("title", title)
.append("url", url)
.append("img", img)
.append("price", price)).first();
if (found == null) {
collection.insertOne(new Document("title", title)
.append("url", url)
.append("img", img)
.append("price", price));
mongoClient.close();
}
//此代码仅插入使用capped集合的第一个文档
MongoDatabase=mongoClient.getDatabase(“db”);
database.createCollection(“capped_collection”,
新建CreateCollectionOptions().capped(true).sizeInBytes(536870912).maxDocuments(5000));
MongoCollection collection=database.getCollection(“capped_collection”);
找到的文档=database.getCollection(“capped_collection”).find(新文档(“title”,title)
.append(“url”,url)
.附加(“img”,img)
.append(“price”,price)).first();
if(found==null){
collection.insertOne(新文档(“标题”,标题)
.append(“url”,url)
.附加(“img”,img)
.附加(“价格”,价格));
mongoClient.close();
}
在这种情况下,capped collection只允许我插入第一个文档,仅此而已(我想要几个文档,而不是一个)。如果我创建普通集合,我可以插入多个文档。为什么会这样
MongoDatabase database = mongoClient.getDatabase("db");
MongoCollection<Document> collection = database.getCollection("normal_collection");
Document found = database.getCollection("normal_collection").find(new Document("title", title)
.append("url", url)
.append("img", img)
.append("price", price)).first();
if (found == null) {
collection.insertOne(new Document("title", title)
.append("url", url)
.append("img", img)
.append("price", price));
mongoClient.close();
}
MongoDatabase=mongoClient.getDatabase(“db”);
MongoCollection collection=database.getCollection(“normal_collection”);
找到的文档=database.getCollection(“普通集合”).find(新文档(“标题”),title)
.append(“url”,url)
.附加(“img”,img)
.append(“price”,price)).first();
if(found==null){
collection.insertOne(新文档(“标题”,标题)
.append(“url”,url)
.附加(“img”,img)
.附加(“价格”,价格));
mongoClient.close();
}
通过这种方式,我可以使用普通集合插入多个文档
正如我正确理解的那样,将文档插入到一个有上限的集合和一个正常的集合之间的代码没有区别,但事实上我有不同的结果
更新:
刚刚尝试通过powershell在capped collection中手动添加文档
在powershell中手动执行以下操作:
文档看起来像(作为测试):
如果我错了,请纠正我。谢谢你的帮助。终于解决了。我需要:
boolean collectionExists=mongoClient.getDatabase(“db_提要”).listCollectionNames()
.into(new ArrayList()).contains(“capped_集合”);
如果(!collectionExists){
database.createCollection(“capped_collection”,
新建CreateCollectionOptions().capped(true).sizeInBytes(327).maxDocuments(1).autoIndex(true));
}
@Veeram,你能在这里帮我或其他人吗?我看不到你插入多个文档的代码。您使用的是insertOne
,它只会插入一个文档。@Veeram,是的,因为拥有此文档集合的方法会通过insertOne
方法分别解析每个商品和外接程序。好的,我将为您改进代码。@Veeram,如果我使用普通集合,则添加为“完全代码”,一切都很好,每个新文档都完美地添加到数据库中,但当我创建capped集合而不是普通的数据库.createCollection(“capped_集合”,new CreateCollectionOptions().capped(true).sizeInBytes(536870912)时.maxDocuments(5000)代码>,在集合中将只插入一个文档。我不明白为什么?他们之间有什么区别?
boolean collectionExists = mongoClient.getDatabase("db_feed").listCollectionNames()
.into(new ArrayList<>()).contains("capped_collection");
if(!collectionExists) {
database.createCollection("capped_collection",
new CreateCollectionOptions().capped(true).sizeInBytes(327).maxDocuments(1).autoIndex(true));
}