RussellHarrower
RussellHarrower

Reputation: 6820

Mongoose upsert data

I am getting the following issue when I try to run a findOneAndUpdate

    MongoError: E11000 duplicate key error collection: AdStitchr.listeners index: uuid dup key: { UUID: "4502191d-1975-463d-8fc1-8ab3537cc9c8" }
    at Connection.<anonymous> (C:\Users\russe\node_modules\mongodb\lib\core\connection\pool.js:466:61)         at Connection.emit (events.js:210:5)
    at processMessage (C:\Users\russe\node_modules\mongodb\lib\core\connection\connection.js:384:10)           at TLSSocket.<anonymous> (C:\Users\russe\node_modules\mongodb\lib\core\connection\connection.js:553:15)    at TLSSocket.emit (events.js:210:5)
    at addChunk (_stream_readable.js:326:12)
    at readableAddChunk (_stream_readable.js:301:11)
    at TLSSocket.Readable.push (_stream_readable.js:235:10)
    at TLSWrap.onStreamRead (internal/stream_base_commons.js:182:23) {
  operationTime: Timestamp { _bsontype: 'Timestamp', low_: 1, high_: 1580080312 },
  ok: 0,
  errmsg: 'E11000 duplicate key error collection: AdStitchr.listeners index: uuid dup key: { UUID: "4502191d-1975-463d-8fc1-8ab3537cc9c8" }',
  code: 11000,
  codeName: 'DuplicateKey',
  keyPattern: { UUID: 1 },
  keyValue: { UUID: '4502191d-1975-463d-8fc1-8ab3537cc9c8' },
  '$clusterTime': {
    clusterTime: Timestamp { _bsontype: 'Timestamp', low_: 1, high_: 1580080312 },
    signature: { hash: [Binary], keyId: [Long] }
  },
  name: 'MongoError',
  [Symbol(mongoErrorContextSymbol)]: {}
}

Now I understand that the error is saying it can not do it because

duplicate key error collection: AdStitchr.listeners index: uuid dup key: { UUID: "4502191d-1975-463d-8fc1-8ab3537cc9c8"

However, it should not be trying to create a new insert. What the script should be doing is updating that document as it already exists with the data from the update.

 var query = userData,
  update = {$set:{
    "endtime":Date.now()
  }},
  options = { upsert: true, setDefaultsOnInsert: true};
  Listeners.findOneAndUpdate(query, update, options, function(error, result) {
    if (!error) {
        // If the document doesn't exist
        if (!result) {
            // Create it
           // result = new Listeners(userData);
        }
        // Save the document
       /* result.save(function(error) {
            if (!error) {
                // Do something with the document
            } else {
                throw error;
            }
        });*/
    }
    else{
     console.log(error);
    }

Query

const userData = {"UUID": query.uuid, "ipaddress":ip, "station":query.station,"starttime":Date.now()};

var update = {'$set':{ "endtime":Date.now() }},

No Endtime is stored indexes

Upvotes: 1

Views: 173

Answers (1)

mickl
mickl

Reputation: 49985

userdata variable is being passed as a query which means that all four below conditions should be met:

const userData = {"UUID": query.uuid, "ipaddress":ip, "station":query.station,"starttime":Date.now()};

The problem is that "starttime":Date.now() generates new value every time you run this code which leads to no matching database document thus there is an insert attempt since you're running upsert on a database. Try to remove that dynamically evaluated condition:

const userData = {"UUID": query.uuid, "ipaddress":ip, "station":query.station };

Upvotes: 1

Related Questions