1
votes

I am using Objectify to create an entity:

@Entity
public class Collection {

    @Id
    private String name;
    @Index
    private List<Long> viewersIds;

//other fields
}

Now I am trying to retrieve the list of Collections which have a particular viewerId, lets say 1. I have tried:

List<Collection> usersCollections = ofy().load().type(Collection.class).filter("viewersIds",1).list();

and

ofy().load().type(Collection.class).filter("viewersIds =",1).list();

and

ofy().load().type(Collection.class).filter("viewersIds ==",1).list();

Getting all Collections works using:

ofy().load().type(Collection.class).list();

What am I doing wrong? Thank you!

EDIT:

Changing the Colllection object to contain a list of strings viewerIds instead of Long

@Index
private List<String> viewersIds;

And then query it with:

ofy().load().type(Collection.class).filter("viewerIds", value).list();

works. So this could be a solution if the list can be of Strings.

2
do you know any way if it's a list of objects? - Ulises CT
Something like .filter("users.id IN", values), supposing you have a List of User objects called "users" and you want to filter by a property called id belonging to the User object. - Roxana Roman

2 Answers

1
votes

One thing to check is that viewerIds is indeed indexed in the entities that are supposed to be returned: https://console.cloud.google.com/datastore/entities/query

0
votes

You're looking for 'in'

.filter("<collection> in", value)