简体   繁体   中英

Storing/updating deeply nested object array in mongodb

I have an array of object by the below format

{
    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. What is the best way to store/update this array in mongodb?

From the official Mongo documentation :

MongoDB supports no more than 100 levels of nesting for BSON documents.

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. But assuming N would not be near this threshhold, then you may work with your data as you would with any other data.

One workaround here might be to use the GridFS collection to store your massively nested JSON objects. 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. Before you go in the direction of GridFS, you might want to rethink your data model.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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