简体   繁体   English

如何在 Spring 引导中迭代 MongoDB 更改 Stream?

[英]How do I iterate over a MongoDB Change Stream in Spring Boot?

I have read countless of articles and code examples on MongoDB Change Streams, but I still can't manage to set it up properly.我已经阅读了无数关于 MongoDB Change Streams 的文章和代码示例,但我仍然无法正确设置它。 I'm trying to listen to a specific collection in my MongoDB and whenever a document is inserted, updated or deleted, I want to do something.我正在尝试收听我的 MongoDB 中的特定集合,每当插入、更新或删除文档时,我都想做点什么。

This is what I've tried:这是我试过的:

@Data
@Document(collection = "teams")
public class Teams{
    private @MongoId(FieldType.OBJECT_ID)
    ObjectId id;
    private Integer teamId;
    private String name;
    private String description;
}

import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;

import com.mongodb.client.model.Aggregates;
import com.mongodb.client.model.Filters;
import com.mongodb.client.model.changestream.FullDocument;
import com.mongodb.client.ChangeStreamIterable;

import org.bson.Document;
import org.bson.conversions.Bson;

import java.util.Arrays;
import java.util.List;

public class MongoDBChangeStream {

    // connect to the local database server
    MongoClient mongoClient = MongoClients.create("db uri goes here");

    // Select the MongoDB database
    MongoDatabase database = mongoClient.getDatabase("MyDatabase");

    // Select the collection to query
    MongoCollection<Document> collection = database.getCollection("teams");

    // Create pipeline for operationType filter
    List<Bson> pipeline = Arrays.asList(
            Aggregates.match(
                    Filters.in("operationType",
                            Arrays.asList("insert", "update", "delete"))));

    // Create the Change Stream
    ChangeStreamIterable<Document> changeStream = collection.watch(pipeline)
            .fullDocument(FullDocument.UPDATE_LOOKUP);

    // Iterate over the Change Stream
    for (Document changeEvent : changeStream) {
        // Process the change event here
    }
}

So this is what I have so far and everything is good until the for-loop which gives three errors:所以这就是我到目前为止所拥有的,一切都很好,直到出现三个错误的 for 循环:

  1. There is a red line under 'for (' , which says unexpected token . 'for ('下方有一条红线,表示unexpected token
  2. There is a red line under ':' , which says ';' expected ':'下面有一条红线,上面写着';' expected ';' expected . ';' expected
  3. There is a red line under 'changeStream)' , which says unknown class: 'changeStream' . 'changeStream)'下面有一条红线,上面写着unknown class: 'changeStream'

First of all you should put your code inside class method, not class body.首先,您应该将代码放在 class 方法中,而不是 class 主体中。 Second - ChangeStreamIterable<Document> iterator element is ChangeStreamDocument<Document> and not Document .第二 - ChangeStreamIterable<Document>迭代器元素是ChangeStreamDocument<Document>而不是Document

Summing things up:总结一下:

public class MongoDBChangeStream {

    public void someMethod() {

        // connect to the local database server
        MongoClient mongoClient = MongoClients.create("db uri goes here");

        // Select the MongoDB database
        MongoDatabase database = mongoClient.getDatabase("MyDatabase");

        // Select the collection to query
        MongoCollection<Document> collection = database.getCollection("teams");

        // Create pipeline for operationType filter
        List<Bson> pipeline = Arrays.asList(
                Aggregates.match(
                        Filters.in(
                                "operationType",
                                Arrays.asList("insert", "update", "delete")
                        )));

        // Create the Change Stream
        ChangeStreamIterable<Document> changeStream = collection.watch(pipeline)
                .fullDocument(FullDocument.UPDATE_LOOKUP);

        // Iterate over the Change Stream
        for (ChangeStreamDocument<Document> changeEvent : changeStream) {
            // Process the change event here
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM