masiboo
masiboo

Reputation: 4719

Mongo database Invalid BSON field name exception

I tried to follow this How to use dot in field name?. But it result as the picture. There is a additional space:-

enter image description here

  protected Document setNestedField(Document doc, FieldValue parentField, String nestedFieldName, Object value, boolean concatenate) {
            if (concatenate) {
                doc.put(parentField.getSystemName() + "." + nestedFieldName, value);
            }
            else {
                doc.put(nestedFieldName, value);
            }
            return doc;
        }

Exception:-Invalid BSON field name photographs.inner_fields; nested exception is java.lang.IllegalArgumentException: Invalid BSON field name photographs.inner_fields.

How can I use dot "." in field name. I have to use . as I'm using some 3rd party api and I have no option to replace to something else like [dot]. Please suggest me?

Upvotes: 5

Views: 16075

Answers (2)

Alex
Alex

Reputation: 21766

In MongoDB field names cannot contain the dot (.) character as it is part of dot-notation syntax, see the documentation.

What third party API are you using ? Are you sure you need a dot ? Dots are commonly used when parsing JSON and your third party API should not need it.

Upvotes: 7

folkol
folkol

Reputation: 4883

So, a third party api is both constructing the keys (with periods in them), AND saving that to MongoDB?

I suggest that you open a bug ticker in said API:s tracker.

If this is not the case, encode the periods somewhere in the persistence code - and decode it on the way up.

Upvotes: 2

Related Questions