Mongodb 使用Mongoose中间件查找值在任一字段中匹配的文档

Mongodb 使用Mongoose中间件查找值在任一字段中匹配的文档,mongodb,express,mongoose,mongoose-middleware,Mongodb,Express,Mongoose,Mongoose Middleware,我有一个源帐户和目标帐户之间的帐户连接列表,因此我的模式如下 var ConnectionRequestSchema = new Schema({ sourceAccountId: { type: Schema.ObjectId, ref: 'Account' }, targetAccountId: { type: Schema.ObjectId, ref: 'Account' }, status: { type: String,

我有一个源帐户和目标帐户之间的帐户连接列表,因此我的模式如下

var ConnectionRequestSchema = new Schema({
  sourceAccountId: {
    type: Schema.ObjectId,
    ref: 'Account'
  },

  targetAccountId: {
    type: Schema.ObjectId,
    ref: 'Account'
  },

  status: {
    type: String,
    enum: ['pending', 'accept', 'decline'],
    trim: true
  }
});
我想查询sourceAccountId或targetAccountId等于查询的accountId的所有文档

我看到了这个链接,它与使用Mongo中的stand find方法查找文档相关

User.findOne({
  $or: [
      {first_name: name},
      {last_name: name},
  ],
}, function(err, user) {
  })

但是我想使用Mongoose中间件来实现这一点,我不确定如何构造这个条件。

您已经找到了解决方案,但是您必须在查询中进行一些更改

ConnectionRequest.find({
  $or: [
      {sourceAccountId: "5736eac90a39c2547cb9d911"},
      {targetAccountId: "5736eac90a39c2547cb9d911"},
  ],
}, function(err, connection) {
console.log(connection)
  })

最后您将得到一个文档数组

谢谢,我一直在使用所有中间件过滤器,但没有意识到它们只是标准mongoose find选项顶部的语法糖果