Description
John W opened DATAMONGO-1387 and commented
I am attempting to use BasicQuery
with a defined list of fields. It appears that the fields list is being ignored.
String queryString = ... // json-formatted string
Query query = new BasicQuery(queryString);
query.fields().include("myField");
final List<MyObject> results = mongoOps.find(query, MyObject.class);
// MyObject includes all fields, not just myField
I was digging through the source and it looks like org.springframework.data.mongodb.core.query.Query
and org.springframework.data.mongodb.core.query.BasicQuery
do things a little differently. Query
defines fields()
and BasicQuery
does not, but getFieldsObject
is different in the two implementations.
public DBObject getFieldsObject() {
return this.fieldSpec == null ? null : fieldSpec.getFieldsObject();
}
public DBObject getFieldsObject() {
return fieldsObject;
}
It looks like when I use BasicQuery.fields()
it does successfully create a fieldspec in the object. But later, when I pass that BasicQuery
object into MongoOps
, it calls query.getFieldsObject()
and it's null
, because I never set an explicit fieldsObject
.
Am I using BasicQuery
incorrectly? I am not clear why BasicQuery
needs to override getFieldsObject
as the behavior in the parent class seems to be the one that you'd want.
Thanks!
Affects: 1.8.2 (Gosling SR2)
Referenced from: commits 7b29a87, ece655f, 6e45386, 2354b66, 06bc4fd, 119692c, f36baf2, aa5bdcd
Backported to: 1.8.4 (Gosling SR4), 1.7.3 (Fowler SR3)