Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongo database Invalid BSON field name exception

Tags:

java

mongodb

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?

like image 579
masiboo Avatar asked Jan 04 '16 09:01

masiboo


2 Answers

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.

like image 89
Alex Avatar answered Oct 25 '22 05:10

Alex


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.

like image 2
folkol Avatar answered Oct 25 '22 04:10

folkol