mongodb聚合嵌入文档值
我正在使用mongodb中的一些聚合函数 假设我有一些这样的文件mongodb聚合嵌入文档值,mongodb,mongodb-query,Mongodb,Mongodb Query,我正在使用mongodb中的一些聚合函数 假设我有一些这样的文件 [ { _id: "1", periods: [ { _id: "12", tables: [ { _id: "121", rows: [ { _id: "1211", text: "some text"}, { _
[
{
_id: "1",
periods: [
{
_id: "12",
tables: [
{
_id: "121",
rows: [
{ _id: "1211", text: "some text"},
{ _id: "1212", text: "some other text"},
{ _id: "1213", text: "yet another text"},
]
}
]
},
{
_id: "13",
tables: [
{
_id: "131",
rows: [
{ _id: "1311", text: "different text"},
{ _id: "1312", text: "Oh yeah"}
]
}
]
}
]
},
{
_id: "2",
periods: [
{
_id: "21",
tables: [
{
_id: "212",
rows: [
{ _id: "2121", text: "period2 text"},
{ _id: "2122", text: "period2 other text"},
{ _id: "2123", text: "period2 yet another text"},
]
}
]
}
]
}
]
[
{ "$project" : { "text" : "$periods.tables.rows.text" , "_id" : "$_id"}},
{ "$unwind" : "$text"},
{ "$group" : { "_id" : "$_id" , "texts" : { "$addToSet" : "$text"}}},
{ "$project" : { "_id" : 0 , "texts" : 1}}
]
{ "texts" : [
[ [ "Some text" , "Some other text" , "yet another text"] , [ "different text" , "oh yeah" ] ],
[ [ "period2 some text", "period2 some other text", "period2 yet another text"]]
]}
现在我想使用mongodb查询来检索一个特定顶级项的所有唯一文本
例如,放弃顶部id 1的所有文本。这意味着我想得到这两个句点子树中的所有文本
预期产出如下:
聚合文本筛选_id:1
[
"some text",
"some other text",
"yet another text",
"different text",
"Oh yeah"
]
聚合文本筛选_id:2
[
"period2 some text",
"period2 some other text",
"period2 yet another text"
]
到目前为止,我已经成功地聚合了所有文本,但最终以多个数组结束,我还没有成功地使用$match在id上过滤它们
我当前的聚合查询如下所示
[
{
_id: "1",
periods: [
{
_id: "12",
tables: [
{
_id: "121",
rows: [
{ _id: "1211", text: "some text"},
{ _id: "1212", text: "some other text"},
{ _id: "1213", text: "yet another text"},
]
}
]
},
{
_id: "13",
tables: [
{
_id: "131",
rows: [
{ _id: "1311", text: "different text"},
{ _id: "1312", text: "Oh yeah"}
]
}
]
}
]
},
{
_id: "2",
periods: [
{
_id: "21",
tables: [
{
_id: "212",
rows: [
{ _id: "2121", text: "period2 text"},
{ _id: "2122", text: "period2 other text"},
{ _id: "2123", text: "period2 yet another text"},
]
}
]
}
]
}
]
[
{ "$project" : { "text" : "$periods.tables.rows.text" , "_id" : "$_id"}},
{ "$unwind" : "$text"},
{ "$group" : { "_id" : "$_id" , "texts" : { "$addToSet" : "$text"}}},
{ "$project" : { "_id" : 0 , "texts" : 1}}
]
{ "texts" : [
[ [ "Some text" , "Some other text" , "yet another text"] , [ "different text" , "oh yeah" ] ],
[ [ "period2 some text", "period2 some other text", "period2 yet another text"]]
]}
它给了我这样的结果
[
{
_id: "1",
periods: [
{
_id: "12",
tables: [
{
_id: "121",
rows: [
{ _id: "1211", text: "some text"},
{ _id: "1212", text: "some other text"},
{ _id: "1213", text: "yet another text"},
]
}
]
},
{
_id: "13",
tables: [
{
_id: "131",
rows: [
{ _id: "1311", text: "different text"},
{ _id: "1312", text: "Oh yeah"}
]
}
]
}
]
},
{
_id: "2",
periods: [
{
_id: "21",
tables: [
{
_id: "212",
rows: [
{ _id: "2121", text: "period2 text"},
{ _id: "2122", text: "period2 other text"},
{ _id: "2123", text: "period2 yet another text"},
]
}
]
}
]
}
]
[
{ "$project" : { "text" : "$periods.tables.rows.text" , "_id" : "$_id"}},
{ "$unwind" : "$text"},
{ "$group" : { "_id" : "$_id" , "texts" : { "$addToSet" : "$text"}}},
{ "$project" : { "_id" : 0 , "texts" : 1}}
]
{ "texts" : [
[ [ "Some text" , "Some other text" , "yet another text"] , [ "different text" , "oh yeah" ] ],
[ [ "period2 some text", "period2 some other text", "period2 yet another text"]]
]}
如果我添加$match:{u id:1},则不会返回任何结果
有人能帮我解决这个问题吗,或者给我指出一个解决问题的方向。我一直在搜索资源,但似乎没有找到任何关于如何使用这些聚合函数的好文档。mongodb文档只使用简单文档
PS我知道我可以使用mapreduce来实现这一点,但我希望能够使用聚合函数来实现这一点。展开只会降低一个级别,因此如果您愿意,您必须调用所调用级别的倍
[
{ "$project" : { "text" : "$periods.tables.rows.text" , "_id" : "$_id"}},
{ "$unwind" : "$text"},
{ "$unwind" : "$text"},
{ "$unwind" : "$text"},
{ "$group" : { "_id" : "$_id" , "texts" : { "$addToSet" : "$text"}}},
{ "$project" : { "_id" : 0 , "texts" : 1}}
]
它会像你期望的那样工作