When I am inserting/updating a document in a collection, is the lock applied on the database or the collection. Suppose I have two collections and they are independant of each other in the same database and wants to do write operations on them concurrently. Is this possible?
Here is the code I am using to test this:
var assert = require('assert'), MongoClient = require('mongodb').MongoClient, async = require('async'); var station_list = require('./station_list.json'), trains_list = require('./trains_list.json'); var stationList = [], trainsList = []; var MONGO_URL = 'mongodb://localhost:27017/test'; for(var i=0; i<station_list.stations.length; i++) stationList.push(station_list.stations[i].station_code); for(var i=0; i<trains_list.trains.length; i++) trainsList.push(trains_list.trains[i].code); console.log('trains : ' + trainsList.length + ' stations : ' + stationList.length); populateTrains(); populateStations(); function populateTrains() { async.eachSeries(trainsList, populateTrainDb, function (err) { assert.equal(null, err); }); } function populateTrainDb(code, callback) { MongoClient.connect(MONGO_URL, function (err, db) { assert.equal(null, err); var jsonData = {}; jsonData.code = code; db.collection('trainsCon').replaceOne( {'code' : code}, jsonData, {upsert: true, w:1}, function (err, res) { assert.equal(null, err); db.close(); callback(); }); }); } function populateStations() { async.eachSeries(stationList, populateStationDb, function (err) { assert.equal(null, err); }); } function populateStationDb(code, callback) { MongoClient.connect(MONGO_URL, function (err, db) { assert.equal(null, err); var jsonData = {}; jsonData.code = code; db.collection('stationsCon').replaceOne( {'code' : code}, jsonData, {upsert:true, w:1}, function (err, res) { assert.equal(null, err); db.close(); callback(); }); }); } The two json files : station_list.json and trains_list.json have around 5000 entries. So after running the given program I get this error after a while :
C:\Users\Adnaan\Desktop\hopSmart\node_modules\mongodb\lib\server.js:242 process.nextTick(function() { throw err; }) ^ AssertionError: null == { [MongoError: connect EADDRINUSE 127.0.0.1:27017] name: 'MongoError', message: 'connect EADDRINUSE 127.0.0.1:27017' } at C:\Users\Adnaan\Desktop\hopSmart\testing.js:52:10 at C:\Users\Adnaan\Desktop\hopSmart\node_modules\mongodb\lib\mongo_client.js:276:20 at C:\Users\Adnaan\Desktop\hopSmart\node_modules\mongodb\lib\db.js:224:14 at null.<anonymous> (C:\Users\Adnaan\Desktop\hopSmart\node_modules\mongodb\lib\server.js:240:9) at g (events.js:273:16) at emitTwo (events.js:100:13) at emit (events.js:185:7) at null.<anonymous> (C:\Users\Adnaan\Desktop\hopSmart\node_modules\mongodb-core\lib\topologies\server.js:301:68) at emitTwo (events.js:100:13) at emit (events.js:185:7) When I check the number of entries entered the database, around 4000 entries had already been entered in both the collections. So what I get from the above experiment was that an error might have occured when one write was being attempted while inside other collection a document must have been getting written.
So how should I proceed to have this concurrency without conflicting locks.