Mark
Mark

Reputation: 1135

mongodb integer index precision

TLDR: Given documents such as { x: 15, y: 15 }, can I create an index on 20 / x, 20 / y without adding those values as fields?

I'd like to index the x, y coordinates of a collection of documents. However my use case is such that:

An index of { x: 1, y: 1} would work but seems wasteful. With my use case the index would have an entry for every document in the collection! Since my queries will be aligned on multiples of 20, I really only need to index to that resolution. I'm expecting this would produce a smaller footprint on disk, in memory, and slightly faster lookups.

Is there a way to create an index like this or am I thinking in the wrong direction?

I'm aware that I could add block_x and block_y to my document, but the block concept doesn't exist in my application so it would be junk data.

Upvotes: 0

Views: 500

Answers (1)

kevinadi
kevinadi

Reputation: 13775

Since my queries will be aligned on multiples of 20, I really only need to index to that resolution. I'm expecting this would produce a smaller footprint on disk, in memory, and slightly faster lookups.

Index entries are created per-document, since each index entry points to a single document. Lowering the "resolution" of the index would therefore impart no space savings at all, since the size of the index depends on the index type (single field, compound, etc. see https://docs.mongodb.com/manual/indexes/#index-types) and the number of documents in that collection.

I'm aware that I could add block_x and block_y to my document, but the block concept doesn't exist in my application so it would be junk data.

If the fields block_x and block_y would help you to more effectively find a document, then I wouldn't say it's junk data. It's true that you don't need to display those fields in your application, but they could be useful to speed up your queries nonetheless.

Upvotes: 1

Related Questions