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

Using watch only sends insertion events, not delete or update #14524

Closed
1 task done
AlexanderEpolite opened this issue Apr 14, 2024 · 5 comments
Closed
1 task done

Using watch only sends insertion events, not delete or update #14524

AlexanderEpolite opened this issue Apr 14, 2024 · 5 comments
Labels
help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary

Comments

@AlexanderEpolite
Copy link

Prerequisites

  • I have written a descriptive issue title

Mongoose version

8.3.1

Node.js version

20

MongoDB version

7.0.8

Operating system

Linux

Operating system version (i.e. 20.04, 11.3, 10)

Ubuntu 22.04

Issue

The following code is only logging when an insertion event happens; however, delete and update events are not being sent in the "change" listener.

Let me know if I'm doing something wrong.

const s = Member.watch([
    {
        $match: {"fullDocument.user_id": user_id, "operationType": {$in: ["insert", "update", "delete"]}},
    }
]);

s.on("change", (change) => {
    console.log(change);
});
@AlexanderEpolite AlexanderEpolite added help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary help wanted labels Apr 14, 2024
@FaizBShah
Copy link
Contributor

If you remove the options object in the watch() function, is it working?

@AlexanderEpolite
Copy link
Author

If you remove the options object in the watch() function, is it working?

@FaizBShah it works when I have this:

const s = Member.watch();

But not with this:

const s = Member.watch([
    {
        $match: {"fullDocument.user_id": user_id},
    }
]);

Any clue what might be causing it? I'm probably going to just switch to once watch stream anyways to save resources.

@FaizBShah
Copy link
Contributor

FaizBShah commented Apr 21, 2024

@AlexanderEpolite Im not too familiar with the type of the argument passed in the .watch() function, but according to this doc, the operationType field should be in the second object, not in the first. Basically, the issue is with the incorrect object structure of the argument.

@IslandRhythms IslandRhythms added confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. and removed help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary help wanted labels Apr 26, 2024
@IslandRhythms
Copy link
Collaborator

const mongoose = require('mongoose');

const testSchema = new mongoose.Schema({
  user_id: mongoose.Schema.Types.ObjectId,
  name: String
});


const Test = mongoose.model('Test', testSchema);

async function run() {
  await mongoose.connect(process.env.MONGOOSECONNECTIONSTRING);
  await mongoose.connection.dropDatabase();
  const userId = new mongoose.Types.ObjectId();
  const s = Test.watch([
    {
      $match: {"fullDocument.user_id": userId },
    }
  ]);

  s.on('change', (change) => console.log('change', change))

  const doc = await Test.create({
    user_id: userId,
    name: 'Test Testerson'
  });

  const res = await Test.updateOne({ _id: doc._id }, { name: 'Test' });
  console.log('what is res', res);
  console.log('done');
}

run();

@vkarpov15
Copy link
Collaborator

This is expected behavior, you need to set the fullDocument option to watch() in order for the fullDocument property to show up on updates and deletes. Use the following syntax:

const s = Member.watch([
    {
        $match: {"fullDocument.user_id": user_id, "operationType": {$in: ["insert", "update", "delete"]}},
    }
], { fullDocument: 'updateLookup' });

@vkarpov15 vkarpov15 added help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary and removed confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. labels May 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary
Projects
None yet
Development

No branches or pull requests

4 participants