【问题标题】:How to retrieve data from a complex data structure in MongoDB?如何从 MongoDB 中的复杂数据结构中检索数据?
【发布时间】:2021-02-24 11:32:11
【问题描述】:

数据:

有没有什么方法可以通过单个查询从 MongoDB 的集合中访问所有这些“lineText”字段?

【问题讨论】:

  • 你能发布一个示例数据吗?只是数据而不是图像
  • 您希望lineText 是哪种形状?我的意思是作为单个值数组或其他东西?
  • 您可以从这里开始Array Query Operators。您还可以在互联网上搜索“查询 MongoDB 中的嵌套数组”。
  • @prasad_ 我认为嵌套查询运算符在我的情况下不起作用

标签: javascript mongodb mongodb-query


【解决方案1】:

试试这个

db.collectionName.aggregate([
    { $unwind: "$text" },
    { $unwind: "$text.paragraphs" },
    {
        $group: {
            _id: null,
            result: {
                $push: {
                    $arrayElemAt: ["$text.paragraphs.lineText", 0]
                }
            }
        }
    },
    {
        $project: {
            result: {
                $reduce: {
                    input: "$result",
                    initialValue: "",
                    in: { "$concat": ["$$value", "$$this"] }
                }
            }
        }
    }
]);

【讨论】:

  • 哇,工作就像一个魅力,谢谢,Dheemanth Bhat。
  • 我不确定性能。看起来像海量的数据。还要考虑性能。
  • 它工作正常,基本上,我只想在我的数据库上使用它一次来从旧文档中获取数据。
猜你喜欢
  • 1970-01-01
  • 2018-08-18
  • 1970-01-01
  • 1970-01-01
  • 2020-03-09
  • 1970-01-01
  • 1970-01-01
  • 2023-04-06
  • 1970-01-01
相关资源
最近更新 更多