Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 如何过滤mongoose changeStream_Node.js_Mongodb_Mongoose - Fatal编程技术网

Node.js 如何过滤mongoose changeStream

Node.js 如何过滤mongoose changeStream,node.js,mongodb,mongoose,Node.js,Mongodb,Mongoose,我正在开发一个聊天系统,我尝试使用mongoDB/mongoose的changeStream 我只想在当前用户是收件人时获取文档,但它不起作用。到目前为止,我遇到了两个案例。一个从不触发,另一个随所有文档一起返回,即使当前用户不是收件人 区别在于管道是否在数组中 你知道什么是正确的语法吗 在过去的两天里,我在谷歌的前10页上阅读了所有的文章,但没有一篇包含如何过滤的内容。据我所知,聚合管道仅用于操纵结果,但不可能排除未通过条件的文档 以下是我所做的: const pipeline = [{

我正在开发一个聊天系统,我尝试使用mongoDB/mongoose的changeStream

我只想在当前用户是收件人时获取文档,但它不起作用。到目前为止,我遇到了两个案例。一个从不触发,另一个随所有文档一起返回,即使当前用户不是收件人

区别在于管道是否在数组中

你知道什么是正确的语法吗


在过去的两天里,我在谷歌的前10页上阅读了所有的文章,但没有一篇包含如何过滤的内容。据我所知,聚合管道仅用于操纵结果,但不可能排除未通过条件的文档


以下是我所做的:

const pipeline = [{
     $match: { 
         "userId": this.recipient.id,
         "recipientId": this.user.id
    }
}]

const stream = MessageModel.watch(pipeline )

stream.on('change', (data: any) => {
    console.log(`messages changed`);
    this.socketIo.sockets.in(this.socket.id).emit(`protected/message/subscribe/${this.msg.msgId}`, data.fullDocument);
});
这可能有用

const { ReplSet } = require('mongodb-topology-manager');
const mongoose = require('mongoose');

run().catch(error => console.error(error));

async function run() {
  console.log(new Date(), 'start');
  const bind_ip = 'localhost';
  // Starts a 3-node replica set on ports 31000, 31001, 31002, replica set
  // name is "rs0".
  const replSet = new ReplSet('mongod', [
    { options: { port: 31000, dbpath: `${__dirname}/data/db/31000`, bind_ip } },
    { options: { port: 31001, dbpath: `${__dirname}/data/db/31001`, bind_ip } },
    { options: { port: 31002, dbpath: `${__dirname}/data/db/31002`, bind_ip } }
  ], { replSet: 'rs0' });

  // Initialize the replica set
  await replSet.purge();
  await replSet.start();
  console.log(new Date(), 'Replica set started...');

  // Connect to the replica set
  const uri = 'mongodb://localhost:31000,localhost:31001,localhost:31002/' +
    'test?replicaSet=rs0';
  await mongoose.connect(uri);

  // To work around "MongoError: cannot open $changeStream for non-existent
  // database: test" for this example
  await mongoose.connection.createCollection('people');

  const Person = mongoose.model('Person', new mongoose.Schema({ name: String }));

  // Create a change stream. The 'change' event gets emitted when there's a
  // change in the database
  Person.watch().
    on('change', data => console.log(new Date(), data));

  // Insert a doc, will trigger the change stream handler above
  console.log(new Date(), 'Inserting doc');
  await Person.create({ name: 'Axl Rose' });
  console.log(new Date(), 'Inserted doc');
}

我想这篇文章会对你很有帮助。我读了过去两天在谷歌前10页上找到的所有文章(包括这篇文章),但没有一篇包含如何过滤。据我所知,聚合管道仅用于操纵结果,但不可能排除不符合条件的文档。请在此处共享答案。如果您找到了答案,我认为我将在未来几天使用副本集。在这个代码段中,watch方法是空的,但是我认为它的参数是键,如果这里有键的话。