简体   繁体   English

MongoDB使用Java,如何过滤同一文档的2个字段

[英]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过滤器运算符,您可以使用findaggregate来过滤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.

相关问题 MongoDB同一个文档中的两个字段如何比较过滤? - How can I compare and filter two fields in the same document in MongoDB? 如何使用Mongodb和JAVA检查文档是否存在? - How do i check whether the document exists using Mongodb and JAVA? 如何执行MongoDB文档的字段的原子更新? - How do I perform atomic updates of fields of a MongoDB document? 如何在 MongoDB 的嵌套文档中索引未知数量的字段? - How do I index an unknown number of fields in a nested document in MongoDB? 如何更新MongoDB文档字段? - How do I update MongoDB document fields only if they exist? MongoDB,使用java同时更新三个文件字段,有时只更新两个字段 - MongoDB,using java to update three fields of document at the same time,sometimes only two fields is updated 如何过滤 mongodb 中另一个文档中的文档字段? - how to filter the fields of a document within another document in mongodb? 如何使用 MongoDB/pyMongo 使字段路径在同一文档中包含一个值? - How do I make a field path include a value in same document using MongoDB/pyMongo? 当修改的字段并不总是相同时,如何更新 MongoDB 文档的字段? - how to update the fields of a MongoDB document when the modified fields are not always the same? 如何使用 Java 向现有 MongoDB 文档中的数组添加另一个值? - How do i add another value to an array in an existing MongoDB document using Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM