Using the $ in operator through Morphia - are you doing it wrong?

I have the following Play Framework object (using Morphia for persistence) as part of a generic blogging application:

@Entity
public class Comment extends Model {

    ...

    @Reference
    @Indexed
    public SiteUser commenter;

    public static List<Comment> getLastCommentsByUsers(final List<SiteUser> users) {
        final Query<Comment> query ds().createQuery(Comment.class);
        query.field(commenter).hasAnyOf(users);
        return query.asList();
    }

}

SiteUser:

@Entity(noClassnameStored=true)
public class SiteUser extends AbstractUser {

    public String realName;

}

AbstractUser:

public class AbstractUser extends Model {

    @Indexed(value= IndexDirection.DESC, unique = true)
    public String emailAddress;

    @Required
    public String password;
}

The method is supposed to getLastCommentsByUsers()return all user comments in the parameter users, but I always get empty Listback. The reason that there Commmentis a separate collection is the ability to get the last X by Commentcertain users through their associated Posts, which is not possible if it Commentis built into the collection Post.

Something is wrong with my request (if I should use something other than hasAnyOf), or is it a problem with displaying relationships - should I use it instead ObjectId?

+5
2

List<Key<SiteUser>> :

public static List<Comment> getLastCommentsByUsers(final List<SiteUser> users) {
    final Query<Comment> query ds().createQuery(Comment.class);
    query.field(commenter).hasAnyOf(toKeys(users)); // convert to keys
    return query.asList();
}

public static List<Key<SiteUser>> toKeys(List<SiteUser> users) {
    List<Key<SiteUser>> keys = new ArrayList<Key<SiteUser>>();
    for(SiteUser user: users) {
        keys.add(ds().getMapper().getKey(user));
    }
    return keys;
}

:

List<Key<SiteUser>> keys = ds().createQuery(SiteUser.class).query().filter(...).asKeyList();
+3

in() . :

List<String> keywordList;
List<Product> products = Product.find().field("keywords").in(keywordList).asList();

.

+6

All Articles