Hi @John_Parsons and welcome to MongoDB community forums!!
Based on my understanding, you wish to view the updates in the change stream for only the documents that satisfy the match condition fields called “available” set to “true” and “language” set to “english”.
I tried the following code the:
const mongoose = require('mongoose');
// Connect to MongoDB
mongoose.connect('mongodb+srv://findThief:findThief@cluster0.sqm88.mongodb.net/?retryWrites=true&w=majority/test', {
useNewUrlParser: true,
useUnifiedTopology: true,
});
const db = mongoose.connection;
db.on('error', console.error.bind(console, 'MongoDB connection error:'));
// Define Trainer schema
const trainerSchema = new mongoose.Schema({
available: Boolean,
language: String,
});
const Trainer = mongoose.model('Trainer', trainerSchema);
const pipeline = [
{
$match: {
available: true,
language: "English",
},
},
];
const changeStream = Trainer.watch(pipeline, { fullDocument: 'updateLookup' });
console.log(changeStream);
console.log('Change stream started.');
changeStream.on('change', (change) => {
console.log(change);
});
The output for the above when an update operation is performed is:
{'_id': {'_data': '8264B625FF000000172B022C0100296E5A1004B72A49ECB85648E49A08B734CBEA93AF46645F6964006464B5240E413B98DBF731CF880004'}, 'operationType': 'update', 'clusterTime': Timestamp(1689658879, 23), 'wallTime': datetime.datetime(2023, 7, 18, 5, 41, 19, 410000), 'fullDocument': {'_id': ObjectId('64b5240e413b98dbf731cf88'), 'name': 'XYZ', 'available': True, 'language': 'English', 'location': 'Changed Location'}, 'ns': {'db': 'test', 'coll': 'trainers'}, 'documentKey': {'_id': ObjectId('64b5240e413b98dbf731cf88')}, 'updateDescription': {'updatedFields': {'location': 'updated Location'}, 'removedFields': [], 'truncatedArrays': []}}
{'_id': {'_data': '8264B625FF000000182B022C0100296E5A1004B72A49ECB85648E49A08B734CBEA93AF46645F6964006464B5244F413B98DBF731CF890004'}, 'operationType': 'update', 'clusterTime': Timestamp(1689658879, 24), 'wallTime': datetime.datetime(2023, 7, 18, 5, 41, 19, 410000), 'fullDocument': {'_id': ObjectId('64b5244f413b98dbf731cf89'), 'name': 'ABC', 'available': True, 'language': 'English', 'location': 'Changed Location'}, 'ns': {'db': 'test', 'coll': 'trainers'}, 'documentKey': {'_id': ObjectId('64b5244f413b98dbf731cf89')}, 'updateDescription': {'updatedFields': {'location': 'updated Location'}, 'removedFields': [], 'truncatedArrays': []}}
The pipeline in the above code would match the requirements in the change stream and would should the results as fullDocument in the change stream output.
Could you please try the above code and let us know if it works for you.
Regards
Aasawari