简体   繁体   中英

How to get the updated record count when running the Mongodb aggregation queries With Java driver

I need number of updated MongoDB records in database for the below syntax. Could you anyone help me to get the number of updated records for the below syntax

MongoDatabase database = client.getDatabase("sample); MongoCollection collection = database.getCollection("collection");

// Created with Studio 3T, the IDE for MongoDB - https://studio3t.com/

Consumer<Document> processBlock = new Consumer<Document>() {
    @Override
    public void accept(Document document) {
        System.out.println(document);
    }
};
Consumer<Document> processBlock = new Consumer<Document>() {
    @Override
    public void accept(Document document) {
        System.out.println(document);
    }
};
List<? extends Bson> pipeline = Arrays.asList(
        new Document()
                .append("$match", new Document()
                        .append("key", "123")
                        .append("value", "ss")
                ),
        new Document()
                .append("$addFields", new Document()
                        .append("sample", "$_id")
                        .append("period", period)
                        )
                        .append("amount", new Document()
                                .append("$multiply", Arrays.asList(
                                        "$amountVal",
                                         Decimal128.parse("-1.0")
                                        )
                                )
                        )
                       
      
        new Document()
                .append("$out", "TMP")
);

collection.aggregate(pipeline)
        .allowDiskUse(false)
        .forEach(processBlock);

I wrote an article that explains why you should avoid using the Document object as long as you can & transfer the call to MongoDB as a mongo shell script .

Take a look at it. I think that even though you'll have to invest more of your time in learning the mongo shell script in the short run, it will save you huge amount of debugging & performance time in the long run.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM