简体   繁体   English

寻找适用于 mongodb 4.2.x 的替代结果投影

[英]Looking for alternative result projection that works on mongodb 4.2.x

As per my last question ( Query document count by multiple ranges returning range start/end with matching element count ), I built a query to check for count of documents in multiple, potentially overlapping date ranges.根据我的最后一个问题( Query document count by multiple ranges returning range start/end with matching element count ),我构建了一个查询来检查多个可能重叠的日期范围内的文档数。

The query works on MongoDB 4.4 but I need to run it on 4.2 as well.该查询适用于 MongoDB 4.4,但我也需要在 4.2 上运行它。 On MongoDB 4.2, I get the following error:在 MongoDB 4.2 上,出现以下错误:

Mongo Server error (MongoCommandException): Command failed with error 168 (InvalidPipelineOperator): 'Unrecognized expression '$first'' on server localhost:27017. 

The full response is:
{ 
    "ok" : 0.0, 
    "errmsg" : "Unrecognized expression '$first'", 
    "code" : 168.0, 
    "codeName" : "InvalidPipelineOperator"
}

How would you write the aggregation projection to achieve the same result structure.您将如何编写聚合投影来实现相同的结果结构。 Here is the complete code with data setup这是带有数据设置的完整代码

db.createCollection("object_location_tracking");
db.getCollection("object_location_tracking").insertMany([
    {
        _id: "1",
        locationId: "locationA",
        objectId: "objectA",
        timestamp: ISODate("2020-01-01T00:00:00Z")
    },
    {
        _id: "2",
        locationId: "locationB",
        objectId: "objectA",
        timestamp: ISODate("2020-01-01T00:00:00Z")
    },
    {
        _id: "3",
        locationId: "locationA",
        objectId: "objectB",
        timestamp: ISODate("2019-01-01T00:00:00Z")
    },
    {
        _id: "4",
        locationId: "locationB",
        objectId: "objectB",
        timestamp: ISODate("2020-01-01T00:00:00Z")
    }
]);



db.getCollection("object_location_tracking").aggregate([
    {$facet: {
        "first_bucket_id": [
            {$match: {"objectId":"objectA",
                      "locationId":"locationA",
                      "timestamp": {$gte: new ISODate('2020-01-01'),
                                    $lt: new ISODate('2020-12-31')}
                     }},
            {$count: "N"}
        ],

        "second_bucket_id": [
            {$match: {"objectId":"objectA",
                      "locationId":"locationA",
                      "timestamp": {$gte: new ISODate('2020-01-01'),
                                    $lt: new ISODate('2022-12-31')}
                     }},
            {$count: "N"}
        ],

        "third_bucket_id": [
            {$match: {"objectId":"objectA",
                      "locationId":"locationB",
                      "timestamp": {$gte: new ISODate('2022-01-01'),
                                    $lt: new ISODate('2022-12-31')}
                     }},
            {$count: "N"}
        ]
    }},
    {
        $set: {
            first_bucket_id: { $first: "$first_bucket_id.N"},
            second_bucket_id: { $first: "$second_bucket_id.N"},
            third_bucket_id: { $first: "$third_bucket_id.N"}
        }
    }
    , {
        $project: {
            first_bucket_id: 1,
            second_bucket_id: 1,
            third_bucket_id: 1
            
        }
    }
]);

You are using the first array element , which as you can tell is a new operator added for version 4.4您正在使用第一个数组元素,您可以看出这是为 4.4 版添加的新运算符

Luckily for you this is quite easy to overcome, by just using $arrayElemAt , like so:幸运的是,这很容易克服,只需使用$arrayElemAt ,就像这样:

{
    $set: {
        first_bucket_id: {$arrayElemAt: ["$first_bucket_id.N", 0]},
        second_bucket_id: {$arrayElemAt: ["$second_bucket_id.N", 0]},
        third_bucket_id: {$arrayElemAt: ["$third_bucket_id.N", 0]}
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在JBoss 4.2.x上升级到Quartz 1.6 - Upgrade to Quartz 1.6 on JBoss 4.2.x 在CAS 4.2.x中找不到ldaptive模式 - Unable to locate ldaptive schema in CAS 4.2.x spring-ws 2.2.2与spring 4.2.x兼容吗? - Is spring-ws 2.2.2 compatible with spring 4.2.x? CAS 4.2.x deployerConfigContext.xml连接到LDAP - CAS 4.2.x deployerConfigContext.xml to connect to LDAP Hibernate 4.3.x和4.2.x之间是否在对抽象实现的类进行水合的方式上有所变化 - Is there a change between Hibernate 4.3.x and 4.2.x in how they hydrate abstract implemented classes 使用JBoss 4.2.x,如何在不取消部署现有数据源的情况下部署新数据源? - Using JBoss 4.2.x, how can I deploy a new data source without undeploying existing ones? 从 JBoss 4.2.x 升级到 JBoss 5.x、6.x、7.x 和 WildFly 8.x 的好处(和提示)? - Benefits (and tips) of an upgrade from JBoss 4.2.x to JBoss 5.x, 6.x, 7.x and WildFly 8.x? 将项目A(Spring 4.2.x)添加为项目B(Spring Boot 2,Spring 5)的依赖项 - Add Project A (Spring 4.2.x) as dependency of Project B (Spring Boot 2, Spring 5) Webjars-locator 不适用于基于 XML 的 Spring MVC 4.2.x 配置? - Webjars-locator doesn't work with XML based Spring MVC 4.2.x configuration? 是否有适用于MongoDB的DbUnit替代方案? - Is there a DbUnit alternative that works with MongoDB?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM