提问者:小点点

观察MongoDB返回更改以及指定的字段值,而不是返回fullDocument


我正在使用mongo的watch()函数来监听对副本集所做的更改,现在我知道可以通过将{fullDocument:'update lookup'}传递给watch方法来获取整个文档(fullDocument),例如:-

someModel.watch({ fullDocument: 'updateLookup' })

但我真正想做的是,只获得一个额外的字段,它不会在每次新的更新时被更改。

假设一个名为'user_id'的字段,当前我只获得UpdatedFieldsFullDocument,其中包含'user_id'以及许多我希望避免的其他数据。

到目前为止,我研究的是聚合管道,但无法找到实现它的方法。

有谁能帮我想个办法吗?


共1个答案

匿名用户

我想这个对你来说很有用。 使用您不希望存在于更新字段中的字段筛选watch方法:

var filter = [{
                $match: {
                    $and: [
                        { "updateDescription.updatedFields.user_id": { $exists: false } },
                        { operationType: "update" }]
                }
            }];
        
            var options = { fullDocument: 'updateLookup' };
            db.collection('somecollection').watch(filter, options).on('change', data => 
            {
                console.log("I'm here!");
            });