Node.js 仅当operationType为';更新';在mongodb

Node.js 仅当operationType为';更新';在mongodb,node.js,mongodb,mongoose,mongodb-query,aggregation-framework,Node.js,Mongodb,Mongoose,Mongodb Query,Aggregation Framework,我正在侦听mongoDB replicaSet中的更改,并使用{fullDocument:'updateLookup'}获取完整文档以及操作类型的更改字段更新 如果operationType是update 这就是我所做的 const filter = [{"$match":{"operationType":"update"}}, {"$project":{ "fullDocument._id":

我正在侦听mongoDB replicaSet中的更改,并使用
{fullDocument:'updateLookup'}
获取完整文档以及
操作类型的更改字段
更新

如果
operationType
update

这就是我所做的

const filter = [{"$match":{"operationType":"update"}}, {"$project":{ "fullDocument._id": 0, "fullDocument.channel": 0, "fullDocument.user_id": 0, "fullDocument.first_name": 0, "fullDocument.last_name": 0, "fullDocument.profile_pic": 0, "fullDocument.email": 0, "fullDocument.number": 0, "fullDocument.updatedAt": 0, "fullDocument.createdAt": 0, "fullDocument.__v": 0}}];

const userDBChange = userChatModel.watch(filter, { fullDocument: 'updateLookup' });
这里的问题是,我没有收到像-insert这样的任何其他更改

我发现问题出在使用
“$match”:{“operationType”:“update”}
如果我删除它,我会收到insert change更新,但
$project
会应用于它


有没有办法只在
操作类型:'update'
上应用
$project
,同时仍让其他更改保持不变(不应用过滤器)?

因此,经过几天的学习和尝试,我终于解决了这个问题

我做了以下工作:

const filter = 
[{ "$match": 
{ "operationType": {"$in" :["update", "insert"]} } }, 
{ "$project": {
    "fullDocument": {
        "$cond":{
            "if":{
                "$eq":["$operationType", "update"]
            },
            "then":"$fullDocument.client",
            "else":"$fullDocument"
        }
    }
}
}];

const userDBChange = userChatModel.watch(filter, { fullDocument: 'updateLookup' });

快乐编码,谢谢

经过几天的学习和尝试,我终于解决了这个问题

我做了以下工作:

const filter = 
[{ "$match": 
{ "operationType": {"$in" :["update", "insert"]} } }, 
{ "$project": {
    "fullDocument": {
        "$cond":{
            "if":{
                "$eq":["$operationType", "update"]
            },
            "then":"$fullDocument.client",
            "else":"$fullDocument"
        }
    }
}
}];

const userDBChange = userChatModel.watch(filter, { fullDocument: 'updateLookup' });
快乐编码,谢谢