我對Java很新。我對mongoDB非常陌生。使用morphia在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,我甚至不確定這是否是最好的方式去解決這個問題。
你試過'ds.createQuery(Clients.class).field(「products.fooType」)。hasThisElement(「bar」)'? –
同樣的錯誤:'無效參數:預計一個對象($ elemMatch)' –