Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(pkFactory): use pkFactory for operations with insert as side effect #2193

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions lib/bulk/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,15 @@ class FindOperators {
document.hint = updateDocument.hint;
}

if (document.upsert) {
document.u = document.u || {};
document.u.$setOnInsert = document.u.$setOnInsert || {};

if (!document.u.$setOnInsert._id) {
document.u.$setOnInsert._id = this.s.collection.s.pkFactory.createPk();
}
}

// Clear out current Op
this.s.currentOp = null;
return this.s.options.addToOperationsList(this, UPDATE, document);
Expand Down Expand Up @@ -854,7 +863,7 @@ class BulkOperationBase {
*/
insert(document) {
if (this.s.collection.s.db.options.forceServerObjectId !== true && document._id == null)
document._id = new ObjectID();
document._id = this.s.collection.s.pkFactory.createPk();
return this.s.options.addToOperationsList(this, INSERT, document);
}

Expand Down Expand Up @@ -983,18 +992,18 @@ class BulkOperationBase {
// Insert operations
if (op.insertOne && op.insertOne.document == null) {
if (forceServerObjectId !== true && op.insertOne._id == null)
op.insertOne._id = new ObjectID();
op.insertOne._id = this.s.collection.s.pkFactory.createPk();
return this.s.options.addToOperationsList(this, INSERT, op.insertOne);
} else if (op.insertOne && op.insertOne.document) {
if (forceServerObjectId !== true && op.insertOne.document._id == null)
op.insertOne.document._id = new ObjectID();
op.insertOne.document._id = this.s.collection.s.pkFactory.createPk();
return this.s.options.addToOperationsList(this, INSERT, op.insertOne.document);
}

if (op.insertMany) {
for (let i = 0; i < op.insertMany.length; i++) {
if (forceServerObjectId !== true && op.insertMany[i]._id == null)
op.insertMany[i]._id = new ObjectID();
op.insertMany[i]._id = this.s.collection.s.pkFactory.createPk();
this.s.options.addToOperationsList(this, INSERT, op.insertMany[i]);
}

Expand Down
9 changes: 9 additions & 0 deletions lib/operations/find_and_modify.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ class FindAndModifyOperation extends OperationBase {
queryObject.update = doc;
}

if (doc && options.upsert) {
queryObject.update = queryObject.update || {};
queryObject.update.$setOnInsert = queryObject.update.$setOnInsert || {};

if (!queryObject.update.$setOnInsert._id) {
queryObject.update.$setOnInsert._id = this.collection.s.pkFactory.createPk();
}
}

if (options.maxTimeMS) queryObject.maxTimeMS = options.maxTimeMS;

// Either use override on the function, or go back to default on either the collection
Expand Down