[英]MongoDB Using Java , How do I filter on 2 fields of the same document
Using Mongo DB (Azure cosmos DB) and i need to filter out objects based on 2 fields inside the documents 使用Mongo DB(Azure cosmos DB),我需要根据文档内的2个字段过滤出对象
Here is an example class: 这是一个示例类:
class Event{
public long startTime;
public long endTime;
}
I need to find all events that their startTime==endTime 我需要找到其startTime == endTime的所有事件
In sql i would do 在SQL中我会做
Select * from Events where startTime=endTime
or
Select * from Events where startTime!=endTime
How do i do it in mongo? 我该如何在mongo中做到这一点?
collection.find(???).first();
With native mongo filter operator,you could use find
and aggregate
to filter startTime==endTime
: 使用本机mongo过滤器运算符,您可以使用
find
和aggregate
来过滤startTime==endTime
:
find : 找到 :
db.test.find({
"$where": "this.fields1 == this.fields2"
});
aggregate : 总计 :
db.test.aggregate([
{
$project:{
fields1: 1,
fields2: 1,
difference: { $eq: ["$fields1", "$fields2"]}
},
},
{
$match: {
difference: true
},
}
]);
However,based on the statements in official document : 但是,根据官方文件中的声明:
The $where and the $eval operators are not supported by Azure Cosmos DB.
Azure Cosmos DB不支持$ where和$ eval运算符。
You could refer to the aggregation pipeline which is preview version. 您可以参考聚合管道 ,它是预览版本。
Or you could try to use stored procedure to select the documents and loop it for comparing the filter columns,then return the desired data.(for reference packages : https://github.com/lmaccherone/documentdb-lumenize ) 或者,您可以尝试使用存储过程选择文档并循环进行比较以比较过滤器列,然后返回所需的数据。(对于参考包: https : //github.com/lmaccherone/documentdb-lumenize )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.