通过mongodb中的语态对类中的对象列表进行字段访问 [英] field access for lists of objects in a class via morphia in mongodb

查看:278
本文介绍了通过mongodb中的语态对类中的对象列表进行字段访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用morphia POJO映射器在mongodb中实现过滤器时遇到一些问题。

I have some problems while implementing filters in mongodb using morphia POJO mapper.

在我的课程中(例如 SampleClass ),当我尝试访问<$ c $的字段时c> @Entity 类(在我们的例子中是 Person ),我发现字段访问正常,对普通字段(如int)使用点表示法,字符串,地图或直接嵌入的对象。

In my class (for example SampleClass), when i try to access the fields of an @Entity class (in our case it's Person), I find the field access works fine, using dot notation for general fields like int, string, maps or direct embedded objects.

问题是我不明白在<$ c $中引用的对象列表的情况下它是如何工作的。 c> Person 类。 (假设这里一个人可以有很多地址,所以这个 Person 类有一个 addresses 字段,其中包含地址对象)

The issue is I could not understand how it works for the case of a "List of Objects" referenced in the Person class. (Assume here, a person can have many addresses, so this Person class has a field addresses which holds a list of Address objects)

@Entity
Class Person
{
    String name;
    int age;
    String type;
    private Map<String, String> personalInfo= new HashMap<String, String>();
    @Reference
    List<Address> addresses = new ArrayList<Address>;
}

@Entity
Class Address
{
    String streetName;
    int doorNo;
}

例如,我想对应用过滤器地址对象的街道名称,位于地址列表

For example, I want to apply a filter on streetName of the Address object which is in the addresses List

public class SampleClass
{  
    private Datastore ds;
    Query<Node>              query;
    CriteriaContainer        container;

    // connections params etc....
    public List<Person> sampleMethod()
    {
        query = ds.find( Person.class ).field( "type" ).equal( "GOOD");                              

        container.add( query.criteria( "name" ).containsIgnoreCase("jo" ));   
        // general String field in the Person Class ---- OKAY, Work's Fine

        container.add( query.criteria( "personalInfo.telephone" ).containsIgnoreCase( "458" ) ); 
        // Map field in the Person Class, accessing telephone key value in the map --- OKAY, Work's Fine

        container.add( query.criteria( "addresses.streetname").containsIgnoreCase( "mainstreet" ) ); 
        // List of address object in the Person Class, name of the field is 'addresses'
        // ----NOT OKAY   ????????? -- Here is the problem it returns nothing, even though some value exists

        return readTopography( query.asList() );
    }
}

我在访问对象时做错了什么列表?

Am I doing something wrong while accessing the objects in a list?

推荐答案

地址字段是@Reference,我们不能在条件中使用 addresses.name作为字段。应该是地址字段位于列表<键<地址>>中的标准:

"addresses" field is a @Reference we cannot use "addresses.name" as a field in a criteria. It should be a criteria where addresses field is in a "List< Key< Address > >" :

    Query<Person> personQuery = ds.createQuery(Person.class);
    Query<Address> addressQuery = ds.createQuery(Address.class);
    addressQuery.criteria("streetName").containsIgnoreCase("mainstreet");
    container.add(personQuery.criteria("addresses").in(addressQuery.asKeyList()));

    System.out.println(personQuery.asList());

致谢,
悲伤

这篇关于通过mongodb中的语态对类中的对象列表进行字段访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆