MongoDB 在对象数组中进行全文搜索
MongoDB Full Text Search in an Array of objects
我有一个这样的架构:
const AyoSchema = new Schema({
images: Array,
Images 是一个数组,其中存储的对象格式如下:
{
id: a uuid here,
name: a string here,
url: a url here,
topic: a string here
}
我想做的是,
我想搜索图像数组中所有对象的名称 属性,而不需要做太多索引工作,
我该怎么办?
有几种方法可以处理它。如果你只想 return 匹配的文档,那就有点复杂了。
我假设您只想 return 匹配的项目。为此,您需要使用聚合管道,特别是 $unwind 和 $match 运算符。
考虑以下因素:
数据库
[
{
_id: ObjectId("111111111111111111111111"),
images: [
{
id: ObjectId("123123224454323123121314"),
name: "foo",
url: "cdn.domain.com/images/foo",
topic: "lorem ipsum"
},
{
id: ObjectId("222123224454323123121314"),
name: "bar",
url: "cdn.domain.com/images/bar",
topic: "lorem ipsum"
},
{
id: ObjectId("333323224454323123121314"),
name: "baz",
url: "cdn.domain.com/images/baz",
topic: "lorem ipsum"
}
]
},
{
_id: ObjectId("222222222222222222222222"),
images: [
{
id: ObjectId("888823224454323123121314"),
name: "text",
url: "cdn.domain.com/images/text",
topic: "lorem ipsum"
},
{
id: ObjectId("999993224454323123121314"),
name: "foo",
url: "cdn.domain.com/images/pic",
topic: "lorem ipsum"
}
]
}
]
查询
db.collection.aggregate([
{
$unwind: "$images"
},
{
$match: {
"images.name": "foo" // <-- replace "foo" with your query
}
}
])
结果
[
{
"_id": ObjectId("111111111111111111111111"),
"images": {
"id": ObjectId("123123224454323123121314"),
"name": "foo",
"topic": "lorem ipsum",
"url": "cdn.domain.com/images/foo"
}
},
{
"_id": ObjectId("222222222222222222222222"),
"images": {
"id": ObjectId("999993224454323123121314"),
"name": "foo",
"topic": "lorem ipsum",
"url": "cdn.domain.com/images/pic"
}
}
]
更新
包括正则表达式。
查询
db.collection.aggregate([
{
$unwind: "$images"
},
{
$match: {
"images.name": {
"$regex": "fo*"
}
}
}
])
我有一个这样的架构:
const AyoSchema = new Schema({
images: Array,
Images 是一个数组,其中存储的对象格式如下:
{
id: a uuid here,
name: a string here,
url: a url here,
topic: a string here
}
我想做的是, 我想搜索图像数组中所有对象的名称 属性,而不需要做太多索引工作,
我该怎么办?
有几种方法可以处理它。如果你只想 return 匹配的文档,那就有点复杂了。
我假设您只想 return 匹配的项目。为此,您需要使用聚合管道,特别是 $unwind 和 $match 运算符。
考虑以下因素:
数据库
[
{
_id: ObjectId("111111111111111111111111"),
images: [
{
id: ObjectId("123123224454323123121314"),
name: "foo",
url: "cdn.domain.com/images/foo",
topic: "lorem ipsum"
},
{
id: ObjectId("222123224454323123121314"),
name: "bar",
url: "cdn.domain.com/images/bar",
topic: "lorem ipsum"
},
{
id: ObjectId("333323224454323123121314"),
name: "baz",
url: "cdn.domain.com/images/baz",
topic: "lorem ipsum"
}
]
},
{
_id: ObjectId("222222222222222222222222"),
images: [
{
id: ObjectId("888823224454323123121314"),
name: "text",
url: "cdn.domain.com/images/text",
topic: "lorem ipsum"
},
{
id: ObjectId("999993224454323123121314"),
name: "foo",
url: "cdn.domain.com/images/pic",
topic: "lorem ipsum"
}
]
}
]
查询
db.collection.aggregate([
{
$unwind: "$images"
},
{
$match: {
"images.name": "foo" // <-- replace "foo" with your query
}
}
])
结果
[
{
"_id": ObjectId("111111111111111111111111"),
"images": {
"id": ObjectId("123123224454323123121314"),
"name": "foo",
"topic": "lorem ipsum",
"url": "cdn.domain.com/images/foo"
}
},
{
"_id": ObjectId("222222222222222222222222"),
"images": {
"id": ObjectId("999993224454323123121314"),
"name": "foo",
"topic": "lorem ipsum",
"url": "cdn.domain.com/images/pic"
}
}
]
更新
包括正则表达式。
查询
db.collection.aggregate([
{
$unwind: "$images"
},
{
$match: {
"images.name": {
"$regex": "fo*"
}
}
}
])