java.lang.IllegalArgumentException: fields stored in the db can't have . in them. (Bad Key: 'iPhone 5.0.1')

Spring JIRA | Adam Nisenbaum | 5 years ago
  1. 0

    create an object like: {code} @Document class MyDataObject { private Map<String, Long> impressionsByOSVersion; } {code} add an entry to map like {code} myObject.osVersions.put("iPhone 4.3.2", 1L); {code} save this object using mongo operations i.e. see exception: {noformat} java.lang.IllegalArgumentException: fields stored in the db can't have . in them. (Bad Key: 'iPhone 5.0.1') at com.mongodb.DBCollection.validateKey(DBCollection.java:1203) at com.mongodb.DBCollection._checkKeys(DBCollection.java:1171) at com.mongodb.DBCollection._checkKeys(DBCollection.java:1174) at com.mongodb.DBCollection._checkObject(DBCollection.java:1161) at com.mongodb.DBCollection.save(DBCollection.java:706) at com.mongodb.DBCollection.save(DBCollection.java:691) at org.springframework.data.mongodb.core.MongoTemplate$11.doInCollection(MongoTemplate.java:790) at org.springframework.data.mongodb.core.MongoTemplate.execute(MongoTemplate.java:368) at org.springframework.data.mongodb.core.MongoTemplate.saveDBObject(MongoTemplate.java:784) at org.springframework.data.mongodb.core.MongoTemplate.doSave(MongoTemplate.java:720) at org.springframework.data.mongodb.core.MongoTemplate.save(MongoTemplate.java:707) at org.springframework.data.mongodb.repository.support.SimpleMongoRepository.save(SimpleMongoRepository.java:72) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:601) at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.executeMethodOn(RepositoryFactorySupport.java:322) at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:307) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202) at $Proxy3022.save(Unknown Source) {noformat} if I extend {{MappingMongoConverter}} and use it, the save works. {code} class KeyEscapingMongoConverter extends MappingMongoConverter { public KeyEscapingMongoConverter(MongoDbFactory mongoDbFactory, MappingContext<? extends MongoPersistentEntity<?>, MongoPersistentProperty> mappingContext) { super(mongoDbFactory, mappingContext); } @Override protected Map<Object, Object> readMap(TypeInformation<?> type, DBObject dbObject) { return unescapeKeys(super.readMap(type, dbObject)); } @Override protected DBObject writeMapInternal(Map<Object, Object> map, DBObject dbo, TypeInformation<?> propertyType) { return super.writeMapInternal(escapeKeys(map), dbo, propertyType); } @SuppressWarnings("unchecked") private <K, V> Map<K, V> escapeKeys(Map<K, V> map) { Map<K, V> escaped = newHashMap(); for (Object key : map.keySet()) { Object newKey = key; if (key instanceof String) { newKey = escapeMongoValue((String) key); } escaped.put((K) newKey, map.get(key)); } return escaped; } @SuppressWarnings("unchecked") private <K, V> Map<K, V> unescapeKeys(Map<K, V> map) { Map<K, V> unescaped = newHashMap(); for (Object key : map.keySet()) { Object newKey = key; if (key instanceof String) { newKey = unescapeMongoValue((String) key); } unescaped.put((K) newKey, map.get(key)); } return unescaped; } private String escapeMongoValue(String raw) { return raw.replaceAll("\\.", "ZZ"); } private String unescapeMongoValue(String raw) { return raw.replaceAll("ZZ", "\\."); } } {code}

    Spring JIRA | 5 years ago | Adam Nisenbaum
    java.lang.IllegalArgumentException: fields stored in the db can't have . in them. (Bad Key: 'iPhone 5.0.1')
  2. 0

    create an object like: {code} @Document class MyDataObject { private Map<String, Long> impressionsByOSVersion; } {code} add an entry to map like {code} myObject.osVersions.put("iPhone 4.3.2", 1L); {code} save this object using mongo operations i.e. see exception: {noformat} java.lang.IllegalArgumentException: fields stored in the db can't have . in them. (Bad Key: 'iPhone 5.0.1') at com.mongodb.DBCollection.validateKey(DBCollection.java:1203) at com.mongodb.DBCollection._checkKeys(DBCollection.java:1171) at com.mongodb.DBCollection._checkKeys(DBCollection.java:1174) at com.mongodb.DBCollection._checkObject(DBCollection.java:1161) at com.mongodb.DBCollection.save(DBCollection.java:706) at com.mongodb.DBCollection.save(DBCollection.java:691) at org.springframework.data.mongodb.core.MongoTemplate$11.doInCollection(MongoTemplate.java:790) at org.springframework.data.mongodb.core.MongoTemplate.execute(MongoTemplate.java:368) at org.springframework.data.mongodb.core.MongoTemplate.saveDBObject(MongoTemplate.java:784) at org.springframework.data.mongodb.core.MongoTemplate.doSave(MongoTemplate.java:720) at org.springframework.data.mongodb.core.MongoTemplate.save(MongoTemplate.java:707) at org.springframework.data.mongodb.repository.support.SimpleMongoRepository.save(SimpleMongoRepository.java:72) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:601) at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.executeMethodOn(RepositoryFactorySupport.java:322) at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:307) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202) at $Proxy3022.save(Unknown Source) {noformat} if I extend {{MappingMongoConverter}} and use it, the save works. {code} class KeyEscapingMongoConverter extends MappingMongoConverter { public KeyEscapingMongoConverter(MongoDbFactory mongoDbFactory, MappingContext<? extends MongoPersistentEntity<?>, MongoPersistentProperty> mappingContext) { super(mongoDbFactory, mappingContext); } @Override protected Map<Object, Object> readMap(TypeInformation<?> type, DBObject dbObject) { return unescapeKeys(super.readMap(type, dbObject)); } @Override protected DBObject writeMapInternal(Map<Object, Object> map, DBObject dbo, TypeInformation<?> propertyType) { return super.writeMapInternal(escapeKeys(map), dbo, propertyType); } @SuppressWarnings("unchecked") private <K, V> Map<K, V> escapeKeys(Map<K, V> map) { Map<K, V> escaped = newHashMap(); for (Object key : map.keySet()) { Object newKey = key; if (key instanceof String) { newKey = escapeMongoValue((String) key); } escaped.put((K) newKey, map.get(key)); } return escaped; } @SuppressWarnings("unchecked") private <K, V> Map<K, V> unescapeKeys(Map<K, V> map) { Map<K, V> unescaped = newHashMap(); for (Object key : map.keySet()) { Object newKey = key; if (key instanceof String) { newKey = unescapeMongoValue((String) key); } unescaped.put((K) newKey, map.get(key)); } return unescaped; } private String escapeMongoValue(String raw) { return raw.replaceAll("\\.", "ZZ"); } private String unescapeMongoValue(String raw) { return raw.replaceAll("ZZ", "\\."); } } {code}

    Spring JIRA | 5 years ago | Adam Nisenbaum
    java.lang.IllegalArgumentException: fields stored in the db can't have . in them. (Bad Key: 'iPhone 5.0.1')
  3. 0

    I have to objects: {code} class Parent { @Id private String id; private Child child; Parent (String id) { this.id=id; } void setChild(Child c) { this.child = c; } } class Child{ @Id private String id; private String someValue; Child (String id) { this.id = id; } } {code} With the following code: {code} Child c = new Child ("abc.def"); Parent p = new Parent("parent"); p.setChild(c); mongoTemplate.save(p); {code} I have the following exception: {code} java.lang.IllegalArgumentException: fields stored in the db can't have . in them. (Bad Key: 'abc.def') at com.mongodb.DBCollection.validateKey(DBCollection.java:1203) at com.mongodb.DBCollection._checkKeys(DBCollection.java:1171) at com.mongodb.DBCollection._checkKeys(DBCollection.java:1174) at com.mongodb.DBCollection._checkObject(DBCollection.java:1161) at com.mongodb.DBCollection.save(DBCollection.java:706) at com.mongodb.DBCollection.save(DBCollection.java:691) at org.springframework.data.mongodb.core.MongoTemplate$11.doInCollection(MongoTemplate.java:790) at org.springframework.data.mongodb.core.MongoTemplate.execute(MongoTemplate.java:368) at org.springframework.data.mongodb.core.MongoTemplate.saveDBObject(MongoTemplate.java:784) at org.springframework.data.mongodb.core.MongoTemplate.doSave(MongoTemplate.java:720) at org.springframework.data.mongodb.core.MongoTemplate.save(MongoTemplate.java:707) at org.springframework.data.mongodb.core.MongoTemplate.save(MongoTemplate.java:703) {code} What's happening is that it is consider the id field in my {{Child}} object as key and tries to validate it. But clearly this is not the case when I save it as part of my {{Parent}} object. I am using {{@Id}} in {{Child}} because I want to be have to save this object by itself as well.

    Spring JIRA | 5 years ago | Ryan Liu
    java.lang.IllegalArgumentException: fields stored in the db can't have . in them. (Bad Key: 'abc.def')
  4. Speed up your debug routine!

    Automated exception search integrated into your IDE

  5. 0

    I have to objects: {code} class Parent { @Id private String id; private Child child; Parent (String id) { this.id=id; } void setChild(Child c) { this.child = c; } } class Child{ @Id private String id; private String someValue; Child (String id) { this.id = id; } } {code} With the following code: {code} Child c = new Child ("abc.def"); Parent p = new Parent("parent"); p.setChild(c); mongoTemplate.save(p); {code} I have the following exception: {code} java.lang.IllegalArgumentException: fields stored in the db can't have . in them. (Bad Key: 'abc.def') at com.mongodb.DBCollection.validateKey(DBCollection.java:1203) at com.mongodb.DBCollection._checkKeys(DBCollection.java:1171) at com.mongodb.DBCollection._checkKeys(DBCollection.java:1174) at com.mongodb.DBCollection._checkObject(DBCollection.java:1161) at com.mongodb.DBCollection.save(DBCollection.java:706) at com.mongodb.DBCollection.save(DBCollection.java:691) at org.springframework.data.mongodb.core.MongoTemplate$11.doInCollection(MongoTemplate.java:790) at org.springframework.data.mongodb.core.MongoTemplate.execute(MongoTemplate.java:368) at org.springframework.data.mongodb.core.MongoTemplate.saveDBObject(MongoTemplate.java:784) at org.springframework.data.mongodb.core.MongoTemplate.doSave(MongoTemplate.java:720) at org.springframework.data.mongodb.core.MongoTemplate.save(MongoTemplate.java:707) at org.springframework.data.mongodb.core.MongoTemplate.save(MongoTemplate.java:703) {code} What's happening is that it is consider the id field in my {{Child}} object as key and tries to validate it. But clearly this is not the case when I save it as part of my {{Parent}} object. I am using {{@Id}} in {{Child}} because I want to be have to save this object by itself as well.

    Spring JIRA | 5 years ago | Ryan Liu
    java.lang.IllegalArgumentException: fields stored in the db can't have . in them. (Bad Key: 'abc.def')
  6. 0

    MongoDb: fields stored in the db can't start with '$'

    GitHub | 4 years ago | tiagoboldt
    java.lang.IllegalArgumentException: fields stored in the db can't start with '$' (Bad Key: '$outer')

    Not finding the right solution?
    Take a tour to get the most out of Samebug.

    Tired of useless tips?

    Automated exception search integrated into your IDE

    Root Cause Analysis

    1. java.lang.IllegalArgumentException

      fields stored in the db can't have . in them. (Bad Key: 'iPhone 5.0.1')

      at com.mongodb.DBCollection.validateKey()
    2. MongoDB Java Driver
      DBCollection.save
      1. com.mongodb.DBCollection.validateKey(DBCollection.java:1203)
      2. com.mongodb.DBCollection._checkKeys(DBCollection.java:1171)
      3. com.mongodb.DBCollection._checkKeys(DBCollection.java:1174)
      4. com.mongodb.DBCollection._checkObject(DBCollection.java:1161)
      5. com.mongodb.DBCollection.save(DBCollection.java:706)
      6. com.mongodb.DBCollection.save(DBCollection.java:691)
      6 frames
    3. Spring Data MongoDB - Core
      SimpleMongoRepository.save
      1. org.springframework.data.mongodb.core.MongoTemplate$11.doInCollection(MongoTemplate.java:790)
      2. org.springframework.data.mongodb.core.MongoTemplate.execute(MongoTemplate.java:368)
      3. org.springframework.data.mongodb.core.MongoTemplate.saveDBObject(MongoTemplate.java:784)
      4. org.springframework.data.mongodb.core.MongoTemplate.doSave(MongoTemplate.java:720)
      5. org.springframework.data.mongodb.core.MongoTemplate.save(MongoTemplate.java:707)
      6. org.springframework.data.mongodb.repository.support.SimpleMongoRepository.save(SimpleMongoRepository.java:72)
      6 frames
    4. Java RT
      Method.invoke
      1. sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      2. sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
      3. sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      4. java.lang.reflect.Method.invoke(Method.java:601)
      4 frames
    5. Spring Data Core
      RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke
      1. org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.executeMethodOn(RepositoryFactorySupport.java:322)
      2. org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:307)
      2 frames
    6. Spring AOP
      JdkDynamicAopProxy.invoke
      1. org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
      2. org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
      2 frames
    7. Unknown
      $Proxy3022.save
      1. $Proxy3022.save(Unknown Source)
      1 frame