Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
304 views
in Technique[技术] by (71.8m points)

changestream - MongoDB change stream processor - Unable to use $project

I am using the watch method to listen to the change-steam events in the MongoDB database. The "fullDocument" that I'm getting after the change event at the database is quite heavy, so I'm trying to filter out the unnecessary field. Below is the syntax:

const pipelines = [
      
      {
        $match: {
          operationType: { $in: ["insert", "update", "replace"] },
        },
      },
      {
        $project: {
          fullDocument: {
            form: 0,
          },
        },
      },
    ];

    const options = { fullDocument: "updateLookup" };

    const changeStream = collection.watch(pipelines, options);

This code is not working and throwing the below error:

MongoError: Change stream must be followed by a match and then a project stage
    at MessageStream.messageHandler (node_modules/mongodb/lib/cmap/connection.js:268:20)
    at MessageStream.emit (events.js:198:13)
    at processIncomingData (node_modules/mongodb/lib/cmap/message_stream.js:144:12)
    at MessageStream._write (node_modules/mongodb/lib/cmap/message_stream.js:42:5)
    at doWrite (_stream_writable.js:415:12)
    at writeOrBuffer (_stream_writable.js:399:5)
    at MessageStream.Writable.write (_stream_writable.js:299:11)
    at TLSSocket.ondata (_stream_readable.js:710:20)
    at TLSSocket.emit (events.js:198:13)
    at addChunk (_stream_readable.js:288:12)

Additional details: MongoDB version: 3.2.0 Mongo client version: 3.6.3

Is there anything missing from my side? If I try {fullDocument: 1} then it works fine.

question from:https://stackoverflow.com/questions/66066945/mongodb-change-stream-processor-unable-to-use-project

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...