barrypicker
barrypicker

Reputation: 10108

Is allowDiskUse valid for find() method in mongodb?

I am trying to test some theories on slow running queries - hence I want my query to run slow. For this reason I have ommited indexes on my collection. When I run db.mycollection.find({myfield: /.abc./}).sort({myfield2: 1}, {allowDiskUse: true}) method I receive the following error...

Error: error: {
        "ok" : 0,
        "errmsg" : "Executor error during find command: OperationFailed: Sort operation used more than the maximum 33554432 bytes of RAM. Add an index, or specify a smaller limit.",
        "code" : 96,
        "codeName" : "OperationFailed"
}

OK fine. I want to continue to sort on disk, but it seems the aggregate framework command 'allowDiskUse:' does nothing on the find() method. I receive the same error.

Is the 'allowDiskUse' valid when used with find() or is it only valid when used with the Aggregation Framework?

I can't seem to find any documentation that states it is framework specific but then again, it doesn't seem to work with find(). Maybe that's a loose answer. I guess I am seeking a more concrete answer.

Upvotes: 4

Views: 12377

Answers (2)

Wernfried Domscheit
Wernfried Domscheit

Reputation: 59642

In MongoDB version 4.4 this feature has been added: cursor.allowDiskUse()

allowDiskUse() has the following form:

db.collection.find(<match>).sort(<sort>).allowDiskUse()

Upvotes: 7

Stennie
Stennie

Reputation: 65443

Is the 'allowDiskUse' valid when used with find() or is it only valid when used with the Aggregation Framework?

The allowDiskUse option is specific to the aggregation framework.

Sort operation used more than the maximum 33554432 bytes of RAM.

You can remove sort() from your find() query to avoid hitting the in-memory sort limit.

I want my query to run slow.

While this isn't a typical goal, a few options to make this query slower are:

  • Use hint() to force a collection scan or non-optimal index choice (but the sort limit still applies)

  • Make your regex query case-insensitive: {myfield: /abc/i}

  • Limit the resources available to mongod using cgroups or a virtual machine. Limited RAM (where your working set is significantly bigger than RAM) and slow disks can contribute to slow queries.

Upvotes: 5

Related Questions