简体   繁体   中英

Objectify Query Opposite of IN

Suppose I have an entity kind called EntityA and I have a list of EntityA ids. How can I query all EntityA entities from my database which the id IS NOT in the list of EntityA ids that I have?

I'm trying to do something like:

ofy().load().type(EntityA.class).filter("!IN", entityAKeys);

How could I get it working? Is it possible?

No, this is not supported as Cloud Datastore only allows queries supported by an index to ensure they don't break as they your dataset scales.

You will have to query for all entities in kind EntityA and filter out those in entityAKeys on the client side yourself.

So I ended up doing like:

List<Key<MXChallenge>> keyList = new ArrayList<>();
List<Key<MXChallenge>> searchInKeys = ofy().load().type(MXChallenge.class).keys().list();

for(Long id : alreadyRetrieved){
    keyList.add(Key.create(MXChallenge.class, id));
}

searchInKeys.removeAll(keyList);

QueryResultIterator<MXChallenge> iteratorChallenges = ofy().load()
        .type(MXChallenge.class).filter("colour", "blue").filterKey("IN", searchInKeys).iterator();

I'm nor sure in terms of performance though but as it is a "keys-only" operation I'd say it should be fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM