Home:ALL Converter>Node MongoDB records not available after callback with writeconcern

Node MongoDB records not available after callback with writeconcern

Ask Time:2018-04-11T23:50:30         Author:Redsandro

Json Formatter

We're seeing this puzzling problem on our production server where MongoDB occasionally isn't able to query records for which a writeConcern just told us that the records were written to the database.

The following versions are installed on the server:

  • Node.js 4.x LTS
  • MongoDB 3.2
  • node-mongodb-native 2.2

After initiating a bulk action with writeConcern 1, the records are immediately queried in the callback, for reasons.

On our development servers, this works as expected 100% of the time. However, on our production server with about 10,000 queries per day, we observe this second query returning zero documents in about 0.01% of cases.

Interestingly, there are in fact documents that should be returned, and the documents do get returned when the exact same query is fired 500 milliseconds later. Obviously though, we do not want to add a latency of 500 milliseconds to all our responses just

Here is a simplified illustration of what I explained above:

let bulk = collection.initializeOrderedBulkOp()

// bulk gathering data
// ...

bulk.execute({w: 1}, function(error, result) {
    console.log(`Inserted ${result.nInserted} records`)

    return mongo.collection(colName).aggregate([{
        // Some query
    }])
    .toArray()
    .then(function(rows) {
        // 99.9% of the time, rows are here.
        // 0.1% of time, rows is erroneously an empty array.
    })
}

Considering that writeConcern is meant specifically for cases like this, what causes this to fail in a small amount of cases?

Note that there is a single mongodb instance running. No clusters or shards.

Author:Redsandro,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/49779453/node-mongodb-records-not-available-after-callback-with-writeconcern
yy