Roxana Roman
Roxana Roman

Reputation: 1012

Objectify Query by list's element

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.

Upvotes: 1

Views: 984

Answers (2)

isk
isk

Reputation: 21

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

Upvotes: 1

Nick
Nick

Reputation: 1822

You're looking for 'in'

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

Upvotes: 0

Related Questions