Proper index creation in mongodb timeseries collection

66 Views Asked by At

I am trying to create indexes for a nested mongo db timeseries collection. My mongo db version, obtained by running mongod --version, is v3.6.8.

The timeseries schema follows the suggested one.

My collection has a schema like:

validator = {
    "$jsonSchema": {
        "bsonType": "object",
        "required": ["timestamp", "metadata", "measurements"],
        "properties": {
            "timestamp": {
                "bsonType": "long",
            },
            "metadata": {
                "bsonType": "object",
                "required": ["type", "sensor_id"],
                "properties": {
                    "type": {
                        "bsonType": "string",
                        "description": "Measurement type"
                    },
                    "sensor_id": {
                        "bsonType": "string",
                        "description": "sensor id"
                        }
                }
            },
            "measurement": {
                "bsonType": "array",
                "description": "must be an array and is required",
                "items": {
                    "bsonType": "double",
                    "description": "must be array of float and is required"
                },
                "minItems": 3,
                "maxItems": 3,
            },
        }
    }
}

When using Mongo db compass to access the db, going to the Index page shows in red the message: Unrecognized expression '$toDouble()': Error on MongoDb Compass

I thought this happens because I have not defined any Index yet. So in Pymongo, I try to create Indexes of the nested fields type and sensor_id with the line:

mydb.mycollection.create_index(
    [
        ("attrs.nested.sensor_id", pymongo.ASCENDING), 
        ("attrs.nested.type", pymongo.ASCENDING)
    ])

But the message error in Mongo Db compass keeps showing the error:

  1. how to solve this Mongodb compass error

Furthermore, I am not sure the indexes are correctly defined, because if I create a fake index like:

mydb.mycollection.create_index(
    [
        ("attrs.nested.unexisting_field", pymongo.ASCENDING),
    ])

no error is generated although the specified field does not exist: 2) is there a way to check the index is correctly

0

There are 0 best solutions below