Skip to content

Commit

Permalink
Merge branch '6.x'
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 committed Aug 25, 2023
2 parents d27d646 + eb34bd3 commit 1d4fcad
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 4 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
6.12.0 / 2023-08-24
===================
* feat: use mongodb driver v4.17.1
* fix(model): make Model.bulkWrite() with empty array and ordered false not throw an error #13664
* fix(document): correctly handle inclusive/exclusive projections when applying subdocument defaults #13763 #13720

7.4.4 / 2023-08-22
==================
* fix(connection): reset document state in between transaction retries #13726 #13698
Expand Down
4 changes: 4 additions & 0 deletions lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -3496,6 +3496,10 @@ Model.bulkWrite = async function bulkWrite(ops, options) {
const validOpIndexes = validOps;
validOps = validOps.sort().map(index => ops[index]);

if (validOps.length === 0) {
return cb(null, getDefaultBulkwriteResult());
}

this.$__collection.bulkWrite(validOps, options, (error, res) => {
if (error) {
if (validationErrors.length > 0) {
Expand Down
4 changes: 3 additions & 1 deletion lib/schema/SubdocumentPath.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const geospatial = require('./operators/geospatial');
const getConstructor = require('../helpers/discriminator/getConstructor');
const handleIdOption = require('../helpers/schema/handleIdOption');
const internalToObjectOptions = require('../options').internalToObjectOptions;
const isExclusive = require('../helpers/projection/isExclusive');
const utils = require('../utils');
const InvalidSchemaOptionError = require('../error/invalidSchemaOption');

Expand Down Expand Up @@ -178,7 +179,8 @@ SubdocumentPath.prototype.cast = function(val, doc, init, priorVal, options) {
subdoc = new Constructor(void 0, selected, doc, false, { defaults: false });
delete subdoc.$__.defaults;
subdoc.$init(val);
applyDefaults(subdoc, selected);
const exclude = isExclusive(selected);
applyDefaults(subdoc, selected, exclude);
} else {
options = Object.assign({}, options, { priorDoc: priorVal });
if (Object.keys(val).length === 0) {
Expand Down
16 changes: 16 additions & 0 deletions test/document.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12342,6 +12342,22 @@ describe('document', function() {
assert.strictEqual(test2.constructor.polluted, undefined);
assert.strictEqual(Object.polluted, undefined);
});

it('sets defaults on subdocs with subdoc projection (gh-13720)', async function() {
const subSchema = new mongoose.Schema({
propertyA: { type: String, default: 'A' },
propertyB: { type: String, default: 'B' }
});
const userSchema = new mongoose.Schema({
name: String,
sub: { type: subSchema, default: () => ({}) }
});
const User = db.model('User', userSchema);
await User.insertMany([{ name: 'user' }]);
await User.updateMany({}, { $unset: { 'sub.propertyA': '' } });
const nestedProjectionDoc = await User.findOne({}, { name: 1, 'sub.propertyA': 1, 'sub.propertyB': 1 });
assert.strictEqual(nestedProjectionDoc.sub.propertyA, 'A');
});
});

describe('Check if instance function that is supplied in schema option is availabe', function() {
Expand Down
29 changes: 26 additions & 3 deletions test/model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5941,9 +5941,32 @@ describe('Model', function() {
const userSchema = new Schema({ name: String });
const User = db.model('User', userSchema);

const err = await User.bulkWrite([], { ordered: false }).then(() => null, err => err);
assert.ok(err);
assert.equal(err.name, 'MongoInvalidArgumentError');
const res = await User.bulkWrite([], { ordered: false });
assert.deepEqual(
res,
{
result: {
ok: 1,
writeErrors: [],
writeConcernErrors: [],
insertedIds: [],
nInserted: 0,
nUpserted: 0,
nMatched: 0,
nModified: 0,
nRemoved: 0,
upserted: []
},
insertedCount: 0,
matchedCount: 0,
modifiedCount: 0,
deletedCount: 0,
upsertedCount: 0,
upsertedIds: {},
insertedIds: {},
n: 0
}
);
});

it('allows calling `create()` after `bulkWrite()` (gh-9350)', async function() {
Expand Down

0 comments on commit 1d4fcad

Please sign in to comment.