我的要求不是那么独特,但是我只是在寻找正确的术语来搜索,因为我一直在获取有关如何查询列表/数组字段或嵌套元素的结果。
这是我的课程(文档类型)...
public class Item {
private String identity = null;
private String name = null;
private String type = null;
private List<Item> grouping = null;
}
因此,此type
的某些实体实例可能变得复杂,如下所示...
{
"identity":"ITEM-1",
"name":"Pack-1",
"type":"Bundle",
"grouping":[
{
"identity":"ITEM-2",
"name":"Book",
"type":"Atomic Unit"
},
{
"identity":"ITEM-3",
"name":"Stationary",
"type":"Bundle",
"grouping":[
{
"identity":"ITEM-4",
"name":"Pen",
"type":"Atomic Unit"
},
{
"identity":"ITEM-5",
"name":"Paper",
"type":"Atomic Unit"
},
{
"identity":"ITEM-6",
"name":"Paraphernalia",
"type":"Bundle",
"grouping":[
{
"identity":"ITEM-7",
"name":"Eraser",
"type":"Atomic Unit"
},
{
"identity":"ITEM-8",
"name":"Ruler",
"type":"Atomic Unit"
},
{
"identity":"ITEM-9",
"name":"Compass",
"type":"Atomic Unit"
}
]
}
]
}
]
}
现在,我的要求是能够搜索Book
或Pen
或Compass
,并且必须能够获取记录ITEM-1
。如何在Mongo Query中实现这一目标。我在数据抽象服务层上使用Spring Data Mongo Repository方法。
预先感谢所有帮助。
答案 0 :(得分:0)
我的要求是能够搜索Book或Pen或Compass, 必须能够获取记录ITEM-1。
您可以从mongo
shell尝试此查询,并获取文档:
var SEARCH_ITEMS = [ "Book", "Pen", "Compass" ]
var ATOMIC = "Atomic Unit"
db.collection.find( {
$or: [
{ "type": ATOMIC, name: { $in: SEARCH_ITEMS } },
{ "grouping.type": ATOMIC, "grouping.name": { $in: SEARCH_ITEMS } },
{ "grouping.grouping.type": ATOMIC, "grouping.grouping.name": { $in: SEARCH_ITEMS } },
{ "grouping.grouping.grouping.type": ATOMIC, "grouping.grouping.grouping.name": { $in: SEARCH_ITEMS } }
]
} )