【发布时间】:2026-01-27 06:15:02
【问题描述】:
我正在尝试查看 MongoDB 更改流中特定字段 status 的更新。
从Change Event documentation 页面,更改流将输出此文档:
{
_id : { <BSON Object> },
"operationType" : "<operation>",
"fullDocument" : { <document> },
"ns" : {
"db" : "<database>",
"coll" : "<collection"
},
"documentKey" : { "_id" : <ObjectId> },
"updateDescription" : {
"updatedFields" : { <document> },
"removedFields" : [ "<field>", ... ]
}
}
我总是获取所有字段,但将 updateDescription.updatedFields 设置为 null。
这是我正在使用的 Java 代码
MongoClient mongoClient = new MongoClient( "localhost" , 27017 );
MongoDatabase database = mongoClient.getDatabase("db");
MongoCollection<org.bson.Document> collection = database.getCollection("collection");
List<Bson> pipeline = Collections.singletonList(Aggregates.match(Filters.and(
Document.parse("{'fullDocument.status': { $in: [ 'DONE','ERROR'] }}"),
Filters.exists("updateDescription.updatedFields.status",true),
Filters.in("operationType", ("replace")))));
MongoCursor<ChangeStreamDocument<Document>> cursor = collection.watch(pipeline).iterator();
我在项目中使用这些依赖项
<mongo.java.driver>3.6.3</mongo.java.driver>
<spring.data.mongo.version>1.10.10.RELEASE</spring.data.mongo.version>
【问题讨论】: