1. for CompoundIndex, if name is not set, it will raise an exception: com.mongodb.MongoException: no index name specified. Because in org.springframework.data.mongodb.core.index.MongoP ersistentEntityIndexCreator.checkForIndexes(MongoP ersistentEntity<?> entity)
persistentProperty.getFieldName() cannot correctly get a field name for a compound index, name will be null
and then in DBCollection.ensureIndex
here also does not check the name value, and then finally name was set to null.
2. in org.springframework.data.mongodb.core.convert.Quer yMapper.Keyword.isKeyword(Object value),
here simply check whether a String value is a key world, just with an "$" symbol.
and in org.springframework.data.mongodb.core.convert.Quer yMapper.getMappedObject(DBObject query, MongoPersistentEntity<?> entity),
here it does not check the rawvalue is an string or DBObject before check whether it's an keyword. So, for such an update operation will raise an Exception java.lang.ClassCastException: java.lang.String cannot be cast to com.mongodb.DBObject:
Sorry for my poor english, I help you can understand what I mean.
Code:
String name = index.name();
if (!StringUtils.hasText(name)) {
name = persistentProperty.getFieldName();
}
and then in DBCollection.ensureIndex
Code:
DBObject opts = new BasicDBObject();
opts.put("name", name);
opts.put("dropDups", dropDups);
opts.put("sparse", sparse);
opts.put("unique", unique);
2. in org.springframework.data.mongodb.core.convert.Quer yMapper.Keyword.isKeyword(Object value),
Code:
if (value instanceof String) {
return ((String) value).startsWith("$");
}
and in org.springframework.data.mongodb.core.convert.Quer yMapper.getMappedObject(DBObject query, MongoPersistentEntity<?> entity),
Code:
for (String key : query.keySet()) {
if (Keyword.isKeyword(key)) {
result.putAll(getMappedKeyword(new Keyword(query, key), entity));
continue;
}
Field field = entity == null ? new Field(key) : new MetadataBackedField(key, entity, mappingContext);
Object rawValue = query.get(key);
String newKey = field.getMappedKey();
if (Keyword.isKeyword(rawValue) && !field.isIdField()) {
Keyword keyword = new Keyword((DBObject) rawValue);
result.put(newKey, getMappedKeyword(field, keyword));
} else {
result.put(newKey, getMappedValue(field, query.get(key)));
}
}
Code:
mongoTemplate.updateFirst(query, new Update().set("password", "$2a$10$SK4o6VteIjH0Jw1a4eLV6Oe1tSHQ18Fl6WnZ70ob4TSqAwnVHn5AO")