spring-data-mongodb icon indicating copy to clipboard operation
spring-data-mongodb copied to clipboard

IndexInfo does not expose background option of the index

Open debugmaster opened this issue 2 years ago • 3 comments

While working with MongoOperations and IndexOperations, and it hard to validate when an index has been created with background or not.

Mongo version: 7.0.12 SpringData MongoDB version: 4.2.4

Simple Java Code:

IndexDefinition definition = new Index()
        .on("field", Sort.Direction.ASC)
        .background();
mongoOperations.indexOps(IndexedClass.class).ensureIndex(indexDefinition);

mongoOperations.indexOps(IndexedClass.class).getIndexInfo();
// No way to validate whether background option exists on the index

Looking at Mongo, it seems to keep track of it:

db.getCollection('indexedClass').getIndexes()
[
  { v: 2, key: { _id: 1 }, name: '_id_' },
  { v: 2, key: { field: 1 }, name: 'field_1', background: true }
]

Please include background property to IndexInfo

debugmaster avatar Aug 05 '24 13:08 debugmaster

Thank you @debugmaster for getting in touch - if you've got time to submit a PR we'd be happy to review the changes.

christophstrobl avatar Aug 05 '24 14:08 christophstrobl

For future reference. This is the section of the mongodb documentation that talks about foreground and background index builds.

marcingrzejszczak avatar Sep 11 '24 10:09 marcingrzejszczak

After reading through this part of the Mongo docs it seems that since version 4.2 the background index property has no effect.

Deprecated: In MongoDB 4.0 and earlier, users could control whether to build indexes in the foreground (blocking) or background (non-blocking, but less efficient) using the background option.

class Person
  include Mongoid::Document
  field :ssn
  index({ ssn: 1 }, { unique: true, background: true })
end

The default value of background is controlled by Mongoid's background_indexing configuration option.

The background option has no effect as of MongoDB 4.2.

Closing this issue as invalid.

marcingrzejszczak avatar Sep 11 '24 11:09 marcingrzejszczak