在 mongodb 中的 object 中搜索 object 中嵌入的公共字段
Search a common field in objects embedded in an object in mongodb
我有一个文件如下:
books: [
{_id: 1,chapters:{0:{title:'ch1'},1:{title:'ch2'},2:{title:'ch3'}},description:'book one'},
{_id: 2,chapters:{0:{title:'ch4'},1:{title:'ch2'},2:{title:'ch5'}},description:'book two'},
{_id: 3,chapters:{0:{title:'ch6'},1:{title:'ch7'},2:{title:'ch8'}},description:'book three'},
{_id: 4,chapters:{0:{title:'ch9'},1:{title:'ch10'},2:{title:'ch11'}},description:'book four'}
]
所以我的问题是:
我如何在这个 collection 中找到标题为 'ch2' 的章节的 object?
而且我无法更改它的数据和结构!
也许这是一个帮助:
我使用来自 jenssegers/laravel-mongodb 的嵌入式文档。
如果有更好的库可以在 laravel 中使用 mongodb,请告诉我!
谢谢
由于您的 collection 有 100m,请尝试索引字段,并在 collection
上进行 $text
搜索
正在创建文本索引
https://docs.mongodb.com/manual/core/index-text/
$text
搜索
https://docs.mongodb.com/manual/reference/operator/query/text/index.html
> db.books.ensureIndex({"$**" : "text"})
指数
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 2,
"numIndexesAfter" : 2,
"note" : "all indexes already exist",
"ok" : 1
}
结果
>
> db.books.find({$text : {$search : "ch2"}})
{ "_id" : 2, "chapters" : { "0" : { "title" : "ch4" }, "1" : { "title" : "ch2" }, "2" : { "title" : "ch5" } }, "description" : "book two" }
{ "_id" : 1, "chapters" : { "0" : { "title" : "ch1" }, "1" : { "title" : "ch2" }, "2" : { "title" : "ch3" } }, "description" : "book one" }
>
这在没有文本索引的情况下是可能的,方法是将章节 $objectToArray
转换为搜索并返回 $arrayToObject
,但这将对 100m 的文档执行此操作,不适合您的情况
db.books.aggregate(
[
{$project : { description : 1, arr : {$objectToArray : "$$ROOT.chapters"}}},
{$match : {"arr.v.title" : "ch2"}},
{$project : { description : 1 , chapters : { $arrayToObject : "$$ROOT.arr"}}}
]
).pretty()
结果
> db.books.aggregate([{$project : { description : 1, arr : {$objectToArray : "$$ROOT.chapters"}}}, {$match : {"arr.v.title" : "ch2"}}, {$project : { description : 1 , chapters : { $arrayToObject : "$$ROOT.arr"}}}])
{ "_id" : 1, "description" : "book one", "chapters" : { "0" : { "title" : "ch1" }, "1" : { "title" : "ch2" }, "2" : { "title" : "ch3" } } }
{ "_id" : 2, "description" : "book two", "chapters" : { "0" : { "title" : "ch4" }, "1" : { "title" : "ch2" }, "2" : { "title" : "ch5" } } }
>
我有一个文件如下:
books: [
{_id: 1,chapters:{0:{title:'ch1'},1:{title:'ch2'},2:{title:'ch3'}},description:'book one'},
{_id: 2,chapters:{0:{title:'ch4'},1:{title:'ch2'},2:{title:'ch5'}},description:'book two'},
{_id: 3,chapters:{0:{title:'ch6'},1:{title:'ch7'},2:{title:'ch8'}},description:'book three'},
{_id: 4,chapters:{0:{title:'ch9'},1:{title:'ch10'},2:{title:'ch11'}},description:'book four'}
]
所以我的问题是: 我如何在这个 collection 中找到标题为 'ch2' 的章节的 object? 而且我无法更改它的数据和结构!
也许这是一个帮助: 我使用来自 jenssegers/laravel-mongodb 的嵌入式文档。 如果有更好的库可以在 laravel 中使用 mongodb,请告诉我! 谢谢
由于您的 collection 有 100m,请尝试索引字段,并在 collection
上进行$text
搜索
正在创建文本索引
https://docs.mongodb.com/manual/core/index-text/
$text
搜索
https://docs.mongodb.com/manual/reference/operator/query/text/index.html
> db.books.ensureIndex({"$**" : "text"})
指数
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 2,
"numIndexesAfter" : 2,
"note" : "all indexes already exist",
"ok" : 1
}
结果
>
> db.books.find({$text : {$search : "ch2"}})
{ "_id" : 2, "chapters" : { "0" : { "title" : "ch4" }, "1" : { "title" : "ch2" }, "2" : { "title" : "ch5" } }, "description" : "book two" }
{ "_id" : 1, "chapters" : { "0" : { "title" : "ch1" }, "1" : { "title" : "ch2" }, "2" : { "title" : "ch3" } }, "description" : "book one" }
>
这在没有文本索引的情况下是可能的,方法是将章节 $objectToArray
转换为搜索并返回 $arrayToObject
,但这将对 100m 的文档执行此操作,不适合您的情况
db.books.aggregate(
[
{$project : { description : 1, arr : {$objectToArray : "$$ROOT.chapters"}}},
{$match : {"arr.v.title" : "ch2"}},
{$project : { description : 1 , chapters : { $arrayToObject : "$$ROOT.arr"}}}
]
).pretty()
结果
> db.books.aggregate([{$project : { description : 1, arr : {$objectToArray : "$$ROOT.chapters"}}}, {$match : {"arr.v.title" : "ch2"}}, {$project : { description : 1 , chapters : { $arrayToObject : "$$ROOT.arr"}}}])
{ "_id" : 1, "description" : "book one", "chapters" : { "0" : { "title" : "ch1" }, "1" : { "title" : "ch2" }, "2" : { "title" : "ch3" } } }
{ "_id" : 2, "description" : "book two", "chapters" : { "0" : { "title" : "ch4" }, "1" : { "title" : "ch2" }, "2" : { "title" : "ch5" } } }
>