MongoDb background indexing and unique index

后端 未结 3 1914
南方客
南方客 2020-12-21 07:42

When you create an index in MongoDb. There are 2 options:

  • Do foreground indexing and lock all write operations while doing so
  • Do background indexing a
相关标签:
3条回答
  • 2020-12-21 07:55

    Referring MongoDB docs-

    If a background index build is in progress when the mongod process terminates, when the instance restarts the index build will restart as foreground index build. If the index build encounters any errors, such as a duplicate key error, the mongod will exit with an error.

    So there are two possibilities-

    1. If index creation is completed then the document which you are trying to insert will give you instant error.
    2. Or if index creation is in progress in background then you will be able to insert the document (because at the time of insertion the index is not there 100%). But later when index creation process tries to put index on your duplicate document then it will exit with error. This is same behavior as if you have duplicate documents and you try to create foreground index.
    0 讨论(0)
  • 2020-12-21 08:10

    @mltsy

    If, from another client, you're doing something like adding a duplicate document while the index is being built, it will insert the document without an error.

    I am not sure this is correct,as Mongodb Doc described as below:

    When building an index on a collection, the database that holds the collection is unavailable for read or write operations until the index build completes.

    I used the mongoose to test this :

    var uniqueUsernameSchema = new Schema({
        username: {
            index: { unique: true, background: false },
            type: String,
            required: true
        }
    })
    var U = mongoose.model('U1', uniqueUsernameSchema)
    var dup = [{ username: 'Val' }, { username: 'Val' }]
    U.create(dup, function (e, d) {
        console.log(e, d)
    })
    

    The unique index failed to build. This result showed the foreground option didnot block the write operation in MongoDB.

    0 讨论(0)
  • 2020-12-21 08:21

    I believe this is the most relevant excerpt from the MongoDB docs:

    Background indexing operations run in the background so that other database operations can run while creating the index. However, the mongo shell session or connection where you are creating the index will block until the index build is complete. To continue issuing commands to the database, open another connection or mongo instance.

    Queries will not use partially-built indexes: the index will only be usable once the index build is complete.

    So this means the client where you issued the command to create the index will remain blocked until the index is fully created. If, from another client, you're doing something like adding a duplicate document while the index is being built, it will insert the document without an error, but eventually your initial client will encounter an error that it was unable to complete the index because there is a duplicate key for the unique index.

    Now, I actually ended up here while trying to understand what MongoID's index(..., {background: true}) option does, because it seems to imply that every write may perform the indexing portion of the write in the background, but my understanding now is that this option only applies to the initial creation of the index. This is explained in the introduction to the docs for the background option for MongoDB's createIndex method (which is not technically the same thing as MongoID's background option, but it clarifies the concept of the feature related to that option):

    MongoDB provides several options that only affect the creation of the index [...] This section describes the uses of these creation options and their behavior.

    Related: Some options that you can specify to createIndex() options control the properties of the index, which are not index creation options. For example, the unique option affects the behavior of the index after creation.

    0 讨论(0)
提交回复
热议问题