【发布时间】:2017-11-30 21:39:30
【问题描述】:
我的基本结构是我有一个用户对象和一个会话对象,其中包含一个 subjectId 和一个小时价格。
User{
defaultHourly: Number,
subjects{
[
id: String,
hourly: Number
]
}
}
我是这样使用 elemMatch 的:
query.elemMatch("subjects", {
"id": { $in: subjects },
"$or": [
{ "hourly": { $eq: null } }, // this is my issue
{
"$and": [
{ "hourly": { $ne: null } },
{ "hourly": { $gte: price.low, $lte: price.high } }
]
}
]
});
elemMatch 的第一部分确保我想要的 subjectId 在主题数组中。然后我想按价格过滤。如果用户在每小时字段中为空,我想将 defaultHourly 与 price.low 和 price.high 进行比较。
问题是 defaultHourly 不是主题中的字段,而是主题父项中的字段,因此我不确定如何访问它。
我想写:
"$or": [
{
"$and": [
{ "hourly": { $eq: null } },
{ "defaultHourly": { $gte: price.low, $lte: price.high } } //this doesnt work because default hourly is not a field.
]
},
{
"$and": [
{ "hourly": { $ne: null } },
{ "hourly": { $gte: price.low, $lte: price.high } }
]
}
]
});
如何进行这种多级比较?
【问题讨论】:
标签: javascript node.js mongodb mongoose mongodb-query