如何在mongodb查询中使用文档字段
我想选择文档,其中from如何在mongodb查询中使用文档字段,mongodb,Mongodb,我想选择文档,其中fromnow+delay,其中from、to和delay是文档字段。现在是我的变量。 诸如此类 { from: {$gte: now}, to: {$lte: now + $delay} } 以下查询可以获得预期的输出: db.collection.find({ "from":{ $gte: now }, $expr:{ $lte:["$to", { $sum: ["$delay", now] }] }
{
from: {$gte: now},
to: {$lte: now + $delay}
}
以下查询可以获得预期的输出:
db.collection.find({
"from":{
$gte: now
},
$expr:{
$lte:["$to", { $sum: ["$delay", now] }]
}
}).pretty()
数据集:
输出:
如果你不能使用$ExPR,你可能会考虑。
注:$where有一个 例如:db.collection.find({
$where: function () {
return this.to > (now + this.delay);
}
});
这是指当前文档。您也可以使用obj来引用它。很酷,但似乎我得到了mongodb的较低版本,其中不支持$expr
{
"_id" : ObjectId("5d7645ab14e02904fe680e12"),
"from" : 2,
"to" : 4,
"delay" : 2
}
{
"_id" : ObjectId("5d7645ab14e02904fe680e13"),
"from" : 2,
"to" : 10,
"delay" : 2
}
{
"_id" : ObjectId("5d7645ab14e02904fe680e14"),
"from" : 6,
"to" : 10,
"delay" : 2
}
{
"_id" : ObjectId("5d7645ab14e02904fe680e12"),
"from" : 2,
"to" : 4,
"delay" : 2
}
db.collection.find({
$where: function () {
return this.to > (now + this.delay);
}
});