使用Mongoose Middleware

时间:2016-05-28 13:16:42

标签: mongodb express mongoose mongoose-middleware

我有源帐户和目标帐户之间的帐户关联列表,因此我的架构看起来像

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的所有文档。

我看到此链接how-to-find-a-document-where-either-one-or-another-field-matches-a-value与使用Mongo中的stand find方法查找文档相关。

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

但我想使用Mongoose Middleware来做这件事,我不确定如何构建这个条件。

1 个答案:

答案 0 :(得分:4)

你已经找到了解决方案,就像这样写

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

最后你得到了一系列文件