简体   繁体   English

在 mongodb 中存储/更新深度嵌套的 object 阵列

[英]Storing/updating deeply nested object array in mongodb

I have an array of object by the below format我有一个 object 数组,格式如下

{
    id: 1,
    text: "Group 1",
    complete: 35,
    start: "2021-10-04T00:00:00",
    end: "2021-10-16T00:00:00",
    taskOwner: "test owner 10",
    wbsNumber: "1",
    children: [
      {
        id: 2,
        start: "2021-10-04T00:00:00",
        end: "2021-10-11T00:00:00",
        text: "Task 1",
        complete: 60,
        children: [
          {
            id: 1113,
            start: "2021-10-11T00:00:00",
            end: "2021-10-16T00:00:00",
            text: "Task New",
            complete: 0,
          },
        ],
      },
    ],
  }

The children array can be nested N times.子数组可以嵌套 N 次。 What is the best way to store/update this array in mongodb?在 mongodb 中存储/更新此数组的最佳方法是什么?

From the official Mongo documentation :来自Mongo 官方文档

MongoDB supports no more than 100 levels of nesting for BSON documents. MongoDB 支持不超过 100 级的 BSON 文档嵌套。

So if N could ever be expected to be larger than 100, then your Mongo driver would throw an error when you try to write.因此,如果N可以预期大于 100,那么当您尝试写入时,您的 Mongo 驱动程序会抛出错误。 But assuming N would not be near this threshhold, then you may work with your data as you would with any other data.但是假设N不会接近这个阈值,那么您可以像处理任何其他数据一样处理您的数据。

One workaround here might be to use the GridFS collection to store your massively nested JSON objects.这里的一种解决方法可能是使用 GridFS 集合来存储大量嵌套的 JSON 对象。 Your data would actually be stored as binary chunks.您的数据实际上将存储为二进制块。 However, realize that querying and updated data in GridFS will tend to be much slower than using BSON documents in a regular Mongo collection.但是,要意识到在 GridFS 中查询和更新数据往往比在常规 Mongo 集合中使用 BSON 文档要慢得多。 Before you go in the direction of GridFS, you might want to rethink your data model.在将 go 转向 GridFS 之前,您可能需要重新考虑您的数据 model。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM