Mongodb 6 timeseries w/ 2dsphere index not working

Hi, I am trying to add a 2dsphere index to a timeseries collection as documented here
Running the find/$near query as below with timeseries enabled generates “MongoServerError: $geoNear, $near, and $nearSphere are not allowed in this context”
If I comment out the timeseries option, the find/$near works fine.

db.sensorDataTS.drop();

db.createCollection("sensorDataTS", {
    // 2 cases: 1) timeseries is enabled, 2) timeseries is commented out
    timeseries: {
        timeField: "timestamp",
        metaField: "metadata",
        granularity: "hours"
    }
});

db.sensorDataTS.insertOne({
    "metadata": {
        "sensorId": 5578,
        "location": {
            type: "Point",
            coordinates: [-77.40711, 39.03335]
        }
    },
    "timestamp": ISODate("2022-01-15T00:00:00.000Z"),
    "currentConditions": {
        "windDirecton": 127.0,
        "tempF": 71.0,
        "windSpeed": 2.0,
        "cloudCover": null,
        "precip": 0.1,
        "humidity": 94.0,
    }
})
// 2dsphere index is created correctly in both ts and non-ts collections
db.sensorDataTS.createIndex({ "metadata.location": "2dsphere" })

// this query works when timeseries commented out above
// else I get "MongoServerError: $geoNear, $near, and $nearSphere are not allowed in this context"
db.sensorDataTS.find(
    {
        "metadata.location": {
            "$near": {
                "$geometry": {
                    "type": "Point", "coordinates": [
                        -77.4,
                        39.03
                    ]
                }, "$minDistance": 0, "$maxDistance": 10000
            }
        }
    })

This works:

db.sensorDataTS.aggregate([
    {
        $geoNear: {
            near: { type: "Point", coordinates: [-77.40, 39.03] },
            key: "metadata.location",
            distanceField: "dist.calculated",
            maxDistance: 10000,
            includeLocs: "dist.location",
            spherical: true
        }
    }
]);
1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.