Raise MongoDB event after entering data into mongoDB collection

I am very new to MongoDB. I did a “rough” operation with MongoDB.

I want to work with event functions in MongoDB when data is inserted into the MongoDB collection event and the data must be immediately emitted in the user interface by MongoDB itself.

Is this possible in MongoDB?

If so, how? and if not, why?

Thanks in advance.

+4
source share
1 answer

You can use tailable cursorwith option Bytes.QUERYOPTION_AWAITDATA. Mongodb tail pointer documentation: https://docs.mongodb.com/manual/core/tailable-cursors/

... , , .

. , cur.hasNext(), , ( ):

mongoTemplates.createCollection("model", new CollectionOptions(null, 10, true));
DBObject query = new BasicDBObject("value", "val");
DBCursor cur = mongoTemplates.getCollection("model")
            .find(query)
            .addOption(Bytes.QUERYOPTION_TAILABLE)
            .addOption(Bytes.QUERYOPTION_AWAITDATA);

new Thread() {
    public void run() {
        //cur.hasNext will wait for data
        while (cur.hasNext()) {
            DBObject obj = cur.next();
            System.out.println(obj);
        }
    };
}.start();
  • cursor.hasNext() , mongodb: db.model.insertOne({value: "val"})

, "capped":

  • java: mongoTemplates.createCollection("model", new CollectionOptions(MAX_SIZE_BYTES, MAX_NB_DOCUMENTS, IS_CAPPED));

  • mongo: db.createCollection( "model", { capped: true, size: 10 } )

Capped Collection :

MongoDB , . , Tailable Cursor, , .

+1

Source: https://habr.com/ru/post/1653734/


All Articles