Predicate on a Map<String, String> field using spring-data-mongodb -
as subject says, have problems query involving predicate
on map<string, string>
field.
i have following test case reproduces problem:
@test public void testreferencespredicate() throws exception { //cleans event collection final dbcollection event = mongotemplate.getcollection("event"); event.drop(); final event anevent = new event(); //event has more fields final map<string, string> somerefs = new hashmap<>(); somerefs.put("a", "b"); anevent.setreferences(somerefs); final event save = this.eventrepository.save(anevent); assertnotnull(save); final booleanbuilder refpredicate = new booleanbuilder(); final map<string, string> references = new hashmap<>(); references.put("a", "b"); (final map.entry<string, string> stringobjectentry : references.entryset()) { final string key = stringobjectentry.getkey(); final string value = stringobjectentry.getvalue(); // doesn't work: refpredicate.and(qevent.event.references.contains(key, value)); refpredicate.and(qevent.event.references.get(key).eq(value)); } final pageable firstpage = new pagerequest(1, 1); final page<event> = this.eventrepository.findall(refpredicate, firstpage); assertnotnull(all); assertequals(1, all.getcontent().size()); //the eventrepository this: public interface eventrepository extends mongorepository<event, objectid>, querydslpredicateexecutor<event>, }
when mongo logging stdout queries client performing, see following:
2015-07-30t10:49:24.399+0200 query [conn3] query some-db.event query: { references.a: "b" } plansummary: collscan ntoreturn:0 ntoskip:0 nscanned:0 nscannedobjects:1 keyupdates:0 writeconflicts:0 numyields:0 nreturned:1 reslen:207 locks:{ global: { acquirecount: { r: 2 } }, mmapv1journal: { acquirecount: { r: 1 } }, database: { acquirecount: { r: 1 } }, collection: { acquirecount: { r: 1 } } } 0ms
it seems query performed different query works mongo client:
e = db.getcollection("event") e.find({ "references.a": "b" })
please note quotes around references.a
, think used access nested json document.
has experienced already?
i on spring-data-mongodb-1.7.0.release
on mongodb v3.0.4
Comments
Post a Comment