MongoDB $push in Array of Array Error: No value exists in scope for the shorthand property 'elements'

MongoDB $push in Array of Array Error: No value exists in scope for the shorthand property 'elements'

我想将数据添加到 Elements 中,但出现错误。如何解决这个 mongodb/typescript 问题?

尝试 1 错误:shorthand属性'elements' 的范围内不存在任何值。声明一个或提供一个 initializer.ts(18004)

尝试 1

  async add<T extends { id: string; parentID: string, elementNum: number, name: string, link: string}>(collectionName: string, args: T) {
    const db = await this.getDB();
    const collection = db.collection(collectionName);

    return new Promise((resolve, reject) => {
        collection.updateOne({ id: args.id }, {$push: {elements[args.elementNum]: {id: uuid(), name: args.name, link: args.link, elements: [] }}}, (err, res) => {
          if (err) {
            reject(err);
          }
          resolve(res);
        });
    });
  }

尝试 2 更改了以下内容

    collection.updateOne({ id: args.id }, {$push: {elements: {id: uuid(), name: args.name, link: args.link, elements: [] }}},

尝试 2 结果最后添加了 Null

        {
            "id": "1",
            "name": "wk1",
            "iconFile": "icon.png",
            "elements": [
                [
                    {
                        "id": "2",
                        "name": "element2",
                        "link": "https",
                        "elements": [
                            {
                                "id": "1",
                                "name": "element1",
                                "link": "https:"
                            }
                        ]
                    }
                ],
                [
                    {
                        "id": "3",
                        "name": "element3",
                        "link": "https://",
                        "elements": [
                            {
                                "id": "4",
                                "name": "w",
                                "link": "http:/"
                            }
                        ]
                    }
                ],
                [
                    {
                        "id": "3",
                        "name": "element3",
                        "link": "https://",
                        "elements": [
                            {
                                "id": "4",
                                "name": "w",
                                "link": "http://"
                            }
                        ]
                    },
                    {
                        "id": "3",
                        "name": "element3",
                        "link": "https://",
                        "elements": [
                            {
                                "id": "4",
                                "name": "w",
                                "link": "http://www."
                            }
                        ]
                    }
                ],
                null,
            ]
        }

我想达到的效果如下


        {
            "id": "1",
            "name": "wk1",
            "iconFile": "icon.png",
            "elements": [
                [
                    {
                        "id": "2",
                        "name": "element2",
                        "link": "https",
                        "elements": [
                            {
                                "id": "1",
                                "name": "element1",
                                "link": "https:"
                            }
                        ]
                    },
                    {
                                "id": "newid",
                                "name": "newname",
                                "link": "newlink"
                                "elements":[]
                    }
                ],
                [
                    {
                        "id": "3",
                        "name": "element3",
                        "link": "https://",
                        "elements": [
                            .......
            ]
        }

演示 - https://mongoplayground.net/p/Dnmg3lL2891

使用 - $[]

The all positional operator $[] indicates that the update operator should modify all elements in the specified array field.

The $[] operator has the following form:

{ <update operator>: { "<array>.$[]" : value } }

db.collection.update({ _id: 1, "add.id": "1"},
{ $push: { "add.$[].elements": { id: "3", name: "a",  link: "" } } })

推送数组而不是对象的演示 - https://mongoplayground.net/p/dh3NSutIv4-

db.collection.update({ _id: 1, "add.id": "1"},
{ $push: { "add.$[].elements": [{ id: "3", name: "a",  link: "" }] } })

const args = {};
args.elementNum = 0;

const update = {
  [`add.$[].elements.${args.elementNum}`]: {
    a: 1
  }
};

console.log(update);

//collection.updateOne({ id: args.id }, update); // use like this