使用 Mongoose 中间件查找在任一字段中值匹配的文档
Find a Document where value matched in either field using 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的所有文档。
我看到这个 link how-to-find-a-document-where-either-one-or-another-field-matches-a-value 与在 Mongo.
中使用标准查找方法查找文档相关
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)
})
最后你会得到文档数组
我有一个源帐户和目标帐户之间的帐户连接列表,因此我的架构如下所示
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的所有文档。
我看到这个 link how-to-find-a-document-where-either-one-or-another-field-matches-a-value 与在 Mongo.
中使用标准查找方法查找文档相关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)
})
最后你会得到文档数组