Skip to content

Commit 1d4fcad

Browse files
committed
Merge branch '6.x'
2 parents d27d646 + eb34bd3 commit 1d4fcad

File tree

5 files changed

+55
-4
lines changed

5 files changed

+55
-4
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
6.12.0 / 2023-08-24
2+
===================
3+
* feat: use mongodb driver v4.17.1
4+
* fix(model): make Model.bulkWrite() with empty array and ordered false not throw an error #13664
5+
* fix(document): correctly handle inclusive/exclusive projections when applying subdocument defaults #13763 #13720
6+
17
7.4.4 / 2023-08-22
28
==================
39
* fix(connection): reset document state in between transaction retries #13726 #13698

lib/model.js

+4
Original file line numberDiff line numberDiff line change
@@ -3496,6 +3496,10 @@ Model.bulkWrite = async function bulkWrite(ops, options) {
34963496
const validOpIndexes = validOps;
34973497
validOps = validOps.sort().map(index => ops[index]);
34983498

3499+
if (validOps.length === 0) {
3500+
return cb(null, getDefaultBulkwriteResult());
3501+
}
3502+
34993503
this.$__collection.bulkWrite(validOps, options, (error, res) => {
35003504
if (error) {
35013505
if (validationErrors.length > 0) {

lib/schema/SubdocumentPath.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const geospatial = require('./operators/geospatial');
1717
const getConstructor = require('../helpers/discriminator/getConstructor');
1818
const handleIdOption = require('../helpers/schema/handleIdOption');
1919
const internalToObjectOptions = require('../options').internalToObjectOptions;
20+
const isExclusive = require('../helpers/projection/isExclusive');
2021
const utils = require('../utils');
2122
const InvalidSchemaOptionError = require('../error/invalidSchemaOption');
2223

@@ -178,7 +179,8 @@ SubdocumentPath.prototype.cast = function(val, doc, init, priorVal, options) {
178179
subdoc = new Constructor(void 0, selected, doc, false, { defaults: false });
179180
delete subdoc.$__.defaults;
180181
subdoc.$init(val);
181-
applyDefaults(subdoc, selected);
182+
const exclude = isExclusive(selected);
183+
applyDefaults(subdoc, selected, exclude);
182184
} else {
183185
options = Object.assign({}, options, { priorDoc: priorVal });
184186
if (Object.keys(val).length === 0) {

test/document.test.js

+16
Original file line numberDiff line numberDiff line change
@@ -12342,6 +12342,22 @@ describe('document', function() {
1234212342
assert.strictEqual(test2.constructor.polluted, undefined);
1234312343
assert.strictEqual(Object.polluted, undefined);
1234412344
});
12345+
12346+
it('sets defaults on subdocs with subdoc projection (gh-13720)', async function() {
12347+
const subSchema = new mongoose.Schema({
12348+
propertyA: { type: String, default: 'A' },
12349+
propertyB: { type: String, default: 'B' }
12350+
});
12351+
const userSchema = new mongoose.Schema({
12352+
name: String,
12353+
sub: { type: subSchema, default: () => ({}) }
12354+
});
12355+
const User = db.model('User', userSchema);
12356+
await User.insertMany([{ name: 'user' }]);
12357+
await User.updateMany({}, { $unset: { 'sub.propertyA': '' } });
12358+
const nestedProjectionDoc = await User.findOne({}, { name: 1, 'sub.propertyA': 1, 'sub.propertyB': 1 });
12359+
assert.strictEqual(nestedProjectionDoc.sub.propertyA, 'A');
12360+
});
1234512361
});
1234612362

1234712363
describe('Check if instance function that is supplied in schema option is availabe', function() {

test/model.test.js

+26-3
Original file line numberDiff line numberDiff line change
@@ -5941,9 +5941,32 @@ describe('Model', function() {
59415941
const userSchema = new Schema({ name: String });
59425942
const User = db.model('User', userSchema);
59435943

5944-
const err = await User.bulkWrite([], { ordered: false }).then(() => null, err => err);
5945-
assert.ok(err);
5946-
assert.equal(err.name, 'MongoInvalidArgumentError');
5944+
const res = await User.bulkWrite([], { ordered: false });
5945+
assert.deepEqual(
5946+
res,
5947+
{
5948+
result: {
5949+
ok: 1,
5950+
writeErrors: [],
5951+
writeConcernErrors: [],
5952+
insertedIds: [],
5953+
nInserted: 0,
5954+
nUpserted: 0,
5955+
nMatched: 0,
5956+
nModified: 0,
5957+
nRemoved: 0,
5958+
upserted: []
5959+
},
5960+
insertedCount: 0,
5961+
matchedCount: 0,
5962+
modifiedCount: 0,
5963+
deletedCount: 0,
5964+
upsertedCount: 0,
5965+
upsertedIds: {},
5966+
insertedIds: {},
5967+
n: 0
5968+
}
5969+
);
59475970
});
59485971

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

0 commit comments

Comments
 (0)