ctc-watanabe
ctc-watanabe

Reputation: 33

Is there any way to align the order of data written to Mongo with the index order created by Cygnus?

I am investigating Cygnus for FIWARE Orion historical data persistence.

Since Cygnus 3.0.0, indexes are created according to the data model when writing to MongoDB, but the order of the indexes created and the data written to MongoDB are different.

I tried it and found that the following indexes were created.

> db['sth_/_Car1_Car'].getIndexes()
[
        {
                "v" : 2,
                "key" : {
                        "_id" : 1
                },
                "name" : "_id_"
        },
        {
                "v" : 2,
                "key" : {
                        "recvTime" : 1,
                        "attrName" : 1,
                        "attrType" : 1,
                        "attrValue" : 1
                },
                "name" : "cyg_raw_opt"
        }
]

However, the data written from Cygnus to MongoDB is in the following order, so it seems that the index is not valid.

> db['sth_/_Car1_Car'].find()
{ "_id" : ObjectId("6475a09a91bf560e1ef4cb0d"), "attrName" : "speed", "attrType" : "Float", "attrValue" : 80, "recvTime" : ISODate("2023-05-30T07:07:05.588Z") }
{ "_id" : ObjectId("6475a09f91bf560e1ef4cb0e"), "attrName" : "speed", "attrType" : "Float", "attrValue" : 70, "recvTime" : ISODate("2023-05-30T07:07:11.584Z") }

I checked some past Issues and found that the data was lined up in the same order as the index.(ex. https://github.com/telefonicaid/fiware-cygnus/issues/2204)

What settings are needed to make this order?

My environment is as follows:

Thank you in advance.

Upvotes: 1

Views: 106

Answers (1)

fgalan
fgalan

Reputation: 12322

MongoDB normally stores the documents (in BSON format in the storage backend) with the fields in the same order they appear in the insert() operation (done by the MongoDB Java driver, in the case of Cygnus). However, order doesn't matter for the most of MongoDB functionality and index usage is not an exception.

An index is basically a data structure that "points" to documents in the database. So, no matter if recvTime comes before attrName in the document stored in the storage backend, as the "document address" for that document, pointed by the index, doesn't change.

Upvotes: 1

Related Questions