简体   繁体   English

如何通过mongodb Java driver 3.4+使用封顶集合添加多个文档?

[英]How to add several documents using capped collection via mongodb Java driver 3.4+?

I want to add several document using capped collection and writing like: 我想使用封顶集合并像这样写一些文档:

// 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();
    }

In this case capped collection gives me to insert only first document and that's all (I want several not one). 在这种情况下,有上限的集合使我只能插入第一个文档,仅此而已(我要几个而不是一个)。 If I create normal collection I can insert several docs. 如果我创建普通收藏,我可以插入几个文档。 Why it happens? 为什么会发生?

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();
        }

In this way I can insert several documents using normal collection. 这样,我可以使用普通集合插入多个文档。

As I understand properly, there is no difference in the code between inserting documents into a capped collection and a normal collection, but in fact I have different results. 据我正确理解,将文档插入有上限的集合和普通集合之间的代码没有什么区别,但是实际上我得到的结果是不同的。

Updated: Just tried manually to add docs in capped collection via powershell 更新: 刚刚尝试通过Powershell在封闭的集合中添加文档

In powershell manually could be like: 在powershell中,手动可能像这样: 在此处输入图片说明

Documents looks like (as test): 文档看起来像(作为测试): 在此处输入图片说明

Correct me, please, if I'm wrong. 请纠正我,如果我错了。 Thanks for help. 感谢帮助。

Finally solve it. 终于解决了。 I needed to check for an existing collection : 我需要检查现有集合

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));
        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 MongoDB Java 驱动程序 3.4+ 光标到数组 - MongoDB Java Driver 3.4+ Cursor to Array 如何使用Java驱动程序3.4+在mongodb中计算带有ObjectId字段的唯一_id? - How to count unique _id with ObjectId field in mongodb using Java driver 3.4+? 如何避免异常使用 mongoDB Java 驱动程序 3.4+ 或 3.6+ 过早到达 stream 的结尾? (插入时) - How to avoid an exception Prematurely reached end of stream using mongoDB Java driver 3.4+ or 3.6+? (during insertion) Matlab通过Java驱动程序从MongoDB集合中删除所有文档 - Matlab delete all documents from MongoDB collection via Java driver 如何使用 java 驱动程序复制 mongodb 集合 (v3.4) - How to copy a mongodb collection with the java driver (v3.4) Spring Data MongoDB 与 Mongo Atlas 的连接抛出“未发送 SNI 名称,请确保使用 MongoDB 3.4+ 驱动程序/shell”错误 - Spring Data MongoDB connection to Mongo Atlas throws 'no SNI name sent, make sure using a MongoDB 3.4+ driver/shell' error 无法使用mongodb java驱动程序将文档插入集合 - not able to insert documents into collection using mongodb java driver 如何在Java mongodb驱动程序中使用“ _id”字段查询文档而不使用集合名称? - How to query documents using “_id” field in Java mongodb driver without using the collection name? MongoCommandException:命令失败,错误为 8000 (AtlasError):“未发送 SNI 名称,请确保使用 MongoDB 3.4+ 驱动程序/shell。” - MongoCommandException: Command failed with error 8000 (AtlasError): 'no SNI name sent, make sure using a MongoDB 3.4+ driver/shell.' Java MongoDB 驱动程序:如何更新集合中的所有文档? - Java MongoDB driver: How to update all Documents in Collection?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM