Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-37919

Unique index with partialFilterExpression option causes WriteConflict

    • Type: Icon: Bug Bug
    • Resolution: Gone away
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 4.0.3, 4.1.4
    • Labels:
      None
    • ALL
    • Hide
      'use strict';
      
      const runTest = async () => {
       const MongoClient = require('mongodb').MongoClient;
       let client;
       let db;
       let s1, s2;
       try {
       console.log('Creating database');
       client = await MongoClient.connect('mongodb://localhost:27017');
       db = client.db('testdb');
       await db.dropDatabase();
      
       let collection = await db.createCollection('testcoll');
       await db.createIndex('testcoll', \{id: 1}, \{unique: true});
       await db.createIndex('testcoll', \{prop1: 1}, \{unique: true, partialFilterExpression: {prop1: {$exists: true}}});
      
       console.log('Inserting documents');
       s1 = client.startSession();
       s1.startTransaction();
       s2 = client.startSession();
       s2.startTransaction();
      
       await collection.insertOne(\{id:1}, \{session: s1});
       await collection.insertOne(\{id:2}, \{session: s2});
      
       await s1.commitTransaction();
       await s2.commitTransaction();
       await s1.endSession();
       await s2.endSession();
      
       console.log('Deleting documents');
       s1 = client.startSession();
       s1.startTransaction();
       s2 = client.startSession();
       s2.startTransaction();
      
       await collection.deleteOne(\{id:1}, \{session: s1});
       await collection.deleteOne(\{id:2}, \{session: s2});
      
       await s1.commitTransaction();
       await s2.commitTransaction();
       await s1.endSession();
       await s2.endSession();
      
       } catch (error) {
       if (s1 && s1.inTransaction()) {
       await s1.abortTransaction();
       s1.endSession();
       }
       if (s2 && s2.inTransaction()) {
       await s2.abortTransaction();
       s2.endSession();
       }
       throw error;
       }
      };
      
      runTest()
       .then (() => {
       process.exit(0);
       })
       .catch(err => {
       console.error(err);
       process.exit(1);
       });
      
      Show
      'use strict' ; const runTest = async () => { const MongoClient = require( 'mongodb' ).MongoClient; let client; let db; let s1, s2; try { console.log( 'Creating database' ); client = await MongoClient.connect( 'mongodb: //localhost:27017' ); db = client.db( 'testdb' ); await db.dropDatabase(); let collection = await db.createCollection( 'testcoll' ); await db.createIndex( 'testcoll' , \{id: 1}, \{unique: true }); await db.createIndex( 'testcoll' , \{prop1: 1}, \{unique: true , partialFilterExpression: {prop1: {$exists: true }}}); console.log( 'Inserting documents' ); s1 = client.startSession(); s1.startTransaction(); s2 = client.startSession(); s2.startTransaction(); await collection.insertOne(\{id:1}, \{session: s1}); await collection.insertOne(\{id:2}, \{session: s2}); await s1.commitTransaction(); await s2.commitTransaction(); await s1.endSession(); await s2.endSession(); console.log( 'Deleting documents' ); s1 = client.startSession(); s1.startTransaction(); s2 = client.startSession(); s2.startTransaction(); await collection.deleteOne(\{id:1}, \{session: s1}); await collection.deleteOne(\{id:2}, \{session: s2}); await s1.commitTransaction(); await s2.commitTransaction(); await s1.endSession(); await s2.endSession(); } catch (error) { if (s1 && s1.inTransaction()) { await s1.abortTransaction(); s1.endSession(); } if (s2 && s2.inTransaction()) { await s2.abortTransaction(); s2.endSession(); } throw error; } }; runTest() .then (() => { process.exit(0); }) . catch (err => { console.error(err); process.exit(1); });
    • Storage NYC 2018-12-03

      An attempt to delete two documents from the same collection from two different transactions results in a WriteConflict exception if the collection has a unique index with the partialFilterExpression option and the field for that index is left unset in both documents.

            Assignee:
            xiangyu.yao@mongodb.com Xiangyu Yao (Inactive)
            Reporter:
            dbolar Dan Bolar
            Votes:
            0 Vote for this issue
            Watchers:
            11 Start watching this issue

              Created:
              Updated:
              Resolved: