Command failed with error 16819 (Location16819)

I have the bellow query to MongoDB:

 public void query11(String product, int limit) {
        printBlock = new Consumer<UserBson>() {
            @Override
            public void accept(final UserBson user) {
                    System.out.println("Hola");
                if (cantResults < limit) {
                    System.out.println("#=[" + (cantResults + 1) + "]\t_id:" + user.getId() );
                    cantResults++;
                }
            }
        };

        connect.getCollection().aggregate(
                Arrays.asList(
                        Aggregates.match(Filters.exists("scores")),
                        Aggregates.unwind("$scores"),
                        Aggregates.project(Projections.include("name", "secondName", "lastName", "secondLastName", "sales.products.name")),
                        Aggregates.sort(Sorts.ascending("_id"))
                )
        ).forEach(printBlock);
        totalTime = (System.currentTimeMillis() - startTimeT);
    }

But Mongo is responding with:

Preformatted textException in thread “main” com.mongodb.MongoCommandException:
Command failed with error 16819 (Location16819): ‘Sort exceeded memory limit of 104857600 bytes, but did not opt in to external sorting. Aborting operation. Pass allowDiskUse:true to opt in.’ on server 127.0.0.1:27017. The full response is {“ok”: 0.0, “errmsg”: “Sort exceeded memory limit of 104857600 bytes, but did not opt in to external sorting. Aborting operation. Pass allowDiskUse:true to opt in.”, “code”: 16819, “codeName”: “Location16819”}

Hello.

This is the error you are getting (and the reason is quite clear in it): … ‘Sort exceeded memory limit of 104857600 bytes, but did not opt in to external sorting. Aborting operation. Pass allowDiskUse:true to opt in.’ on server….

The reason for the error and how to overcome is explained in the documentation: $sort and Memory Restrictions.

In your Java code, use the allowDiskUse option on the collection.aggregate method as follows, and this should solve the problem:

collection.aggregate(pipeline).allowDiskUse(true)

2 Likes

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.