Apache beam java MongoDbIO sink/upsert opertation not preserving the given field order

72 views Asked by At

Using the MongoDbIO sink approach, I'm able to upsert the documents but when the document(s) gets inserted it is actually not preserving the given field sequence order in the bson document and the fields arranged in sorted order in the mongodb document instead.

Currently I'm following the latest Apache beam API document. https://beam.apache.org/releases/javadoc/current/index.html?org/apache/beam/sdk/io/mongodb/MongoDbIO.html

Below is how I'm upserting the documents.

pCollectionDocuments.apply(MongoDbIO.write()
    .withUri("mongodb://localhost:27017")
    .withDatabase("my-database")
    .withCollection("my-collection")
    .withUpdateConfiguration(UpdateConfiguration.create()
            .withFindKey("userId").withUpdateKey("userId")
        .withUpdateFields(
            UpdateField.fieldUpdate("$set", "firstName", "firstName"),
            UpdateField.fieldUpdate("$set", "middleName", "middleName"),
            UpdateField.fieldUpdate("$set", "lastName", "lastName"))));

Also with the tradition approach it works good like I tried before it maintains field order as if inserting a bson document using collection.insertOne(document) but with the above approach it's not preserving the field order in the mongodb document.

Assume before insert (Bson document):

{
    "firstName" : "John",
    "middleName" : "M",
    "lastName" : "Frank"
}

After insert (Mongodb document): (field order not preserved!)

{
    "firstName" : "John",
    "lastName" : "Frank",
    "middleName" : "M"
}

Could anyone please help me how to make the given field order preserved into the mongodb document using the MongoDbIO approach?

0

There are 0 answers