我在 MongoDB 中有以下数据库结构(见下文),我想对“名称”字段进行搜索查询
数据库结构:
{
"_id" : ObjectId("505ad9a7ed5022cbe1f3dc2e"),
"id" : "10",
"description" : "",
"members" : [{
"id" : "1",
"name" : "Jack"
}, {
"id" : "2",
"name" : "Mike"
}, {
"id" : "3",
"name" : "Laura"
}, {
"id" : "4",
"name" : "Sara"
}, {
"id" : "240",
"name" : "Ronald"
}],
"status" : "active"
},
{
"_id" : ObjectId("5059c214707747cbc5819f6f"),
"id" : "12",
"description" : "",
"members" : [{
"id" : "19",
"name" : "Geoff"
}, {
"id" : "21",
"name" : "Andrew"
}, {
"id" : "23",
"name" : "Rachel"
}, {
"id" : "25",
"name" : "Susan"
}],
"status" : "active"
},
我有以下代码将在“描述”或“状态”字段上进行搜索。DBCollection 集合 = database.getCollection("members");
BasicDBObject or1 = new BasicDBObject();
or1.put("description", Pattern.compile(keyword, Pattern.CASE_INSENSITIVE));
BasicDBObject or2 = new BasicDBObject();
or2.put("status", Pattern.compile(keyword, Pattern.CASE_INSENSITIVE));
BasicDBList or = new BasicDBList();
or.add(or1);
or.add(or2);
BasicDBObject query = new BasicDBObject();
query.put("$or", or);
BasicDBObject select = new BasicDBObject();
select.put("description", 1);
select.put("status", 1);
DBCollection collection = collection.find(query, select);
问题是,我将如何对字段 'members.name' 进行 OR 搜索?因此,如果我要搜索 ID 为 10 的文档/记录将在结果集中的名称或文本“劳拉”。
我是否必须遍历所有结果或类似的东西?
感谢您的任何帮助或建议!