Start a new topic

Compound query "or().contains" returns first result

Having a problem with compound queries in Backbone.

var query = new Kinvey.Query();

query.equalTo('owner_id',me.id);

query.or().contains('_id',[array of ids])



The .equalTo argument returns ALL of the matches but the .or().contains argument only returns the first match.



If I use a single query, the .contains returns all of the matches:

var query = new Kinvey.Query();

query.contains('_id',[array of ids])



It doesn't matter if I put the contains ahead of the equalTo:

var query = new Kinvey.Query();

query.contains('_id',[array of ids]);

query.or().equalTo('owner_id',me.id);



The contains will only return the first item that matched.


Do the two queries separately return the right results? If so, I’d suggest joining the result set on the client. I think this has to do with Mongo ObjectIDs not being converted correctly when part of an `$or`. If this is the case, we will support this in the future, but fur now, joining the results would be a good workaround.



If this doesn’t solve your issue, let me know.
Yes, they work individually. So this specific to the way you guys are parsing the $or and not detecting that one of them is an '_id' and needs to be converted?



Because I need caching, joining the results won't work because the last fetch() operation will stomp the previous one. I'm thinking the solution then is to run both queries, then gather the _ids and run the final, third query that will get cached.



Does that sound right?
What do you mean by "stomp the previous one"? The resulting documents for each fetch will be stored locally, and locally the `$or` query should return the correct results.



Another possible solution is to convert `_id`s inside `$or` to Mongo ObjectIDs using business logic.
By stomp, I mean when I run a collection.fetch({query:{field:value}), doesn't the caching mechanism just cache the last fetch that was run on that collection?
It stores all entities retrieved by that fetch into the cache (in addition to any entities that might already be in the cache).
So if I run a query for {name:'dog'} and then run a query for {name:'cat'}, the results of both have accumulated in the query such that when I'm offline, I can run a general fetch {} and get every item that is in the cache, regardless of how it was queried in the first place?
Yes.
Login or Signup to post a comment