Skip to content

Commit

Permalink
Merge branch '8.5' into vkarpov15/gh-8302
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 committed Jun 24, 2024
2 parents 5167992 + da15836 commit 278b310
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 2 deletions.
6 changes: 6 additions & 0 deletions lib/drivers/node-mongodb-native/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,12 @@ function _setClient(conn, client, options, dbName) {
});
}

if (options.monitorCommands) {
client.on('commandStarted', (data) => conn.emit('commandStarted', data));
client.on('commandFailed', (data) => conn.emit('commandFailed', data));
client.on('commandSucceeded', (data) => conn.emit('commandSucceeded', data));
}

conn.onOpen();

for (const i in conn.collections) {
Expand Down
6 changes: 6 additions & 0 deletions lib/helpers/query/castUpdate.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,12 @@ function walkUpdatePath(schema, obj, op, options, context, filter, pref) {
// an update.
if (op === '$pull') {
schematype = schema._getSchema(prefix + key);
if (schematype == null) {
const _res = getEmbeddedDiscriminatorPath(schema, obj, filter, prefix + key, options);
if (_res.schematype != null) {
schematype = _res.schematype;
}
}
if (schematype != null && schematype.schema != null) {
obj[key] = cast(schematype.schema, obj[key], options, context);
hasKeys = true;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"dependencies": {
"bson": "^6.7.0",
"kareem": "2.6.3",
"mongodb": "6.6.2",
"mongodb": "6.7.0",
"mpath": "0.9.0",
"mquery": "5.0.0",
"ms": "2.1.3",
Expand Down
24 changes: 23 additions & 1 deletion test/connection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ describe('connections:', function() {
let conn;

before(async function() {
conn = mongoose.createConnection(start.uri2);
conn = mongoose.createConnection(start.uri2, { monitorCommands: true });
await conn.asPromise();
await conn.collection('test').deleteMany({});
return conn;
Expand Down Expand Up @@ -254,6 +254,28 @@ describe('connections:', function() {
assert.equal(events[1].method, 'findOne');
assert.deepStrictEqual(events[1].result, { _id: 17, answer: 42 });
});

it('commandStarted, commandFailed, commandSucceeded (gh-14611)', async function() {
let events = [];
conn.on('commandStarted', event => events.push(event));
conn.on('commandFailed', event => events.push(event));
conn.on('commandSucceeded', event => events.push(event));

await conn.collection('test').insertOne({ _id: 14611, answer: 42 });
assert.equal(events.length, 2);
assert.equal(events[0].constructor.name, 'CommandStartedEvent');
assert.equal(events[0].commandName, 'insert');
assert.equal(events[1].constructor.name, 'CommandSucceededEvent');
assert.equal(events[1].requestId, events[0].requestId);

events = [];
await conn.createCollection('tests', { capped: 1024 }).catch(() => {});
assert.equal(events.length, 2);
assert.equal(events[0].constructor.name, 'CommandStartedEvent');
assert.equal(events[0].commandName, 'create');
assert.equal(events[1].constructor.name, 'CommandFailedEvent');
assert.equal(events[1].requestId, events[0].requestId);
});
});

it('should allow closing a closed connection', async function() {
Expand Down
31 changes: 31 additions & 0 deletions test/model.updateOne.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3017,6 +3017,37 @@ describe('model: updateOne: ', function() {
const doc = await Test.findById(_id);
assert.equal(doc.subdoc['1'], 'foobar');
});
it('handles embedded discriminators with $pull when discriminator key set in filter (gh-14675)', async function() {
const LoginSchema = new Schema({}, { discriminatorKey: 'type', _id: false });
const UserSchema = new Schema({
name: String,
login: LoginSchema
});
UserSchema.path('login').discriminator('ssh-key', new Schema({
keys: {
type: [{
id: { type: String, required: true },
publicKey: { type: String, required: true }
}],
default: []
}
}, { _id: false }));
const User = db.model('Test', UserSchema);

const { _id } = await User.create({
login: {
type: 'ssh-key',
keys: [{ id: 'my-key', publicKey: 'test' }, { id: 'test2', publicKey: 'foo' }]
}
});
const doc = await User.findOneAndUpdate(
{ _id, 'login.type': 'ssh-key' },
{ $pull: { 'login.keys': { id: 'my-key' } } },
{ new: true }
).exec();
assert.equal(doc.login.keys.length, 1);
assert.equal(doc.login.keys[0].id, 'test2');
});
});

async function delay(ms) {
Expand Down

0 comments on commit 278b310

Please sign in to comment.