Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用morphia在mongodb中的对象列表中搜索_Java_Mongodb_Morphia - Fatal编程技术网

Java 使用morphia在mongodb中的对象列表中搜索

Java 使用morphia在mongodb中的对象列表中搜索,java,mongodb,morphia,Java,Mongodb,Morphia,我对Java非常陌生。我对mongoDB很陌生 我有一个类似这样的收藏: { "_id" : "1234", "name" : "bill", "products" : [ { "fooType" : "bar", ..... }, { "fooType" : "oof", ..... }

我对Java非常陌生。我对mongoDB很陌生

我有一个类似这样的收藏:

{
    "_id" : "1234",
    "name" : "bill",
    "products" : [ 
        {
            "fooType" : "bar",
            .....
        },
        {
            "fooType" : "oof",
            .....
        }        
    ],
    "status" : "Truncated"
},
{...}
我正在尝试实现一个搜索功能来按fooType进行搜索。我能够使用标准mongodb语法创建工作查询,但不知道如何使用morphia实现

工作mongodb查询:

db.Clients.find({products: {$elemMatch: {fooType: "bar"}}})
我尝试过的一些(被截断的)代码没有成功:

DatastoreImpl ds;
q = ds.createQuery(Clients.class).field("products").hasThisElement("fooType");

显然,这不起作用,因为它需要一个对象。我似乎不知道如何使用hasThisElement,我甚至不确定这是否是最好的方法

hasThisElement
要求参数中有一个对象,因此不能使用字符串“fooType”或“bar”

假设此集合具有以下类:

class Clients {
String id;
String name;
List<Product> products = new ArrayList<Product>();
String status;
}

class Product {
String fooType;
....
}

hasThisElement
要求参数中有一个对象,因此不能使用字符串“fooType”或“bar”

假设此集合具有以下类:

class Clients {
String id;
String name;
List<Product> products = new ArrayList<Product>();
String status;
}

class Product {
String fooType;
....
}

hasThisElement
要求参数中有一个对象,因此不能使用字符串“fooType”或“bar”

假设此集合具有以下类:

class Clients {
String id;
String name;
List<Product> products = new ArrayList<Product>();
String status;
}

class Product {
String fooType;
....
}

hasThisElement
要求参数中有一个对象,因此不能使用字符串“fooType”或“bar”

假设此集合具有以下类:

class Clients {
String id;
String name;
List<Product> products = new ArrayList<Product>();
String status;
}

class Product {
String fooType;
....
}

您是否尝试过
ds.createQuery(Clients.class).field(“products.fooType”).hasThisElement(“bar”)
?相同错误:
无效参数:应为对象($elemMatch)
您是否尝试过
ds.createQuery(Clients.class).field(“products.fooType”).hasThisElement(“bar”)
?相同错误:
无效参数:应为对象($elemMatch)
您是否尝试过
ds.createQuery(Clients.class).field(“products.fooType”).hasThisElement(“bar”)
相同错误:
无效参数:应为对象($elemMatch)
您是否尝试过
ds.createQuery(Clients.class).field(“products.fooType”).hasThisElement(“bar”)
?相同错误:
无效参数:应为对象($elemMatch)
我最初将其标记为正确,但现在不确定。我希望能够按FootType进行搜索。这似乎需要与我要查找的对象完全匹配,而不是查找所有具有FootType=“bar”的对象,对吗?除非要匹配数组元素中的多个组件,否则不需要使用$elemMatch运算符。此查询ds.createQuery(Clients.class).field(“products”).hasThisElement(filterProduct)将被解释为:{“products”:{“$elemMatch”:{“fooType”:“bar”}}。如果您只想返回字段products中包含“fooType”:“bar”的所有文档,则可以使用查询db.Clients.find({“products.fooType”:“bar”}),morphia中相应的代码是:ds.createQuery(Clients.class).filter(“products.fooType”,“bar”);我最初将其标记为正确,但现在不确定。我希望能够按fooType进行搜索。这似乎需要与我要查找的对象完全匹配,而不是查找所有具有fooType=“bar”的对象,对吗?除非要匹配数组元素中的多个组件,否则不需要使用$elemMatch运算符。此查询ds.createQuery(Clients.class).field(“products”).hasThisElement(filterProduct)将被解释为:{“products”:{“$elemMatch”:{“fooType”:“bar”}}。如果您只想返回字段products中包含“fooType”:“bar”的所有文档,则可以使用查询db.Clients.find({“products.fooType”:“bar”}),morphia中相应的代码是:ds.createQuery(Clients.class).filter(“products.fooType”,“bar”);我最初将其标记为正确,但现在不确定。我希望能够按fooType进行搜索。这似乎需要与我要查找的对象完全匹配,而不是查找所有具有fooType=“bar”的对象,对吗?除非要匹配数组元素中的多个组件,否则不需要使用$elemMatch运算符。此查询ds.createQuery(Clients.class).field(“products”).hasThisElement(filterProduct)将被解释为:{“products”:{“$elemMatch”:{“fooType”:“bar”}}。如果您只想返回字段products中包含“fooType”:“bar”的所有文档,则可以使用查询db.Clients.find({“products.fooType”:“bar”}),morphia中相应的代码是:ds.createQuery(Clients.class).filter(“products.fooType”,“bar”);我最初将其标记为正确,但现在不确定。我希望能够按fooType进行搜索。这似乎需要与我要查找的对象完全匹配,而不是查找所有具有fooType=“bar”的对象,对吗?除非要匹配数组元素中的多个组件,否则不需要使用$elemMatch运算符。此查询ds.createQuery(Clients.class).field(“products”).hasThisElement(filterProduct)将被解释为:{“products”:{“$elemMatch”:{“fooType”:“bar”}}。如果您只想返回字段products中具有“fooType”:“bar”的所有文档,则可以使用查询db.Clients.find({“products.fooType”:“bar”}),morphia中相应的代码是:ds.createQuery(Clients.class).filter(“products.fooType”,“bar”);