【问题标题】:mongodb/mongoose - how to insert new subdocument with a nested array fieldmongodb/mongoose - 如何使用嵌套数组字段插入新的子文档
【发布时间】:2019-02-01 23:53:25
【问题描述】:

原问题:如何插入type作为字段名。

所以我得到了错误:

CastError: Cast to embedded failed for value "{ details:\n   [ { content: \'\',\n       hidden: false,\n       _id: \'5b83ed6410bd2b146b13741b\',\n       title: \'Name\',\n       order: 0 },\n     { content: \'\',\n       hidden: false,\n       _id: \'5b83ed6410bd2b146b13741a\',\n       title: \'Price\',\n       order: 1 },\n     { content: \'\',\n       hidden: false,\n       _id: \'5b83ed6410bd2b146b137419\',\n       title: \'Company\',\n       order: 2 } ] }" at path "items"

这是对象:

[
    {
        "title": "Name",
        "content": "",
        "hidden": false,
        "order": 0,
        "type": "text"
    },
    {
        "title": "Price",
        "content": "",
        "hidden": false,
        "order": 1,
        "type": "text"
    },
    {
        "title": "Company",
        "content": "",
        "hidden": false,
        "order": 2,
        "type": "text"
    }
]

我有疑问:

ListModel
    .findOneAndUpdate(
        {
            _id: list_id
        },
        {   
            $push: {
                items: {
                    details: the_template
                }
            }
        }
    )

正在尝试进行插入/更新

我不确定是什么错误。

注意我正在将一个新的 item 插入到 items 数组中。如果我先创建项目,然后使用 $push: {details} 查询更新该项目,它会起作用。

这是架构。

列表架构:

var List = new Schema(
    {
        items: {
            index: true,
            type: [Item]
        },
        hidden: {
            default: false,
            index: true,
            type: Boolean
        },
        name: {
            index: true,
            type: String,
        },
        item_details_template: {
            default: [
                {
                    "title": "Name",
                    "content": "",
                    "hidden": false,
                    "order": 0,
                    "type": "text"
                },
                {
                    "title": "Price",
                    "content": "",
                    "hidden": false,
                    "order": 1,
                    "type": "text"
                },
                {
                    "title": "Company",
                    "content": "",
                    "hidden": false,
                    "order": 2,
                    "type": "text"
                }
            ],
            type: [Item_Detail]
        },
        // Other users who have access
        shared_with: {
            index: true,
            refs: "users",
            type: [Shared_With]
        },
        // Owner
        user_id: {
            index: true,
            ref: "users",
            required: true,
            type: ObjectId,
        }
    },
    {
        strict: false
    }
)

项目架构:

var Item = new Schema(
    {
        // Template_Item
        based_on: {
            index: true,
            type: ObjectId
        },
        details: {
            default: [],
            index: true,
            type: [Item_Detail],
        },
        display_name: {
            default: "",
            index: true,
            type: String,
        },
        image: {
            default: "http://via.placeholder.com/700x500/ffffff/000000/?text=No%20Image&",
            index: true,
            type: String
        },
        hidden: {
            default: true,
            index: true,
            type: Boolean
        },
        tags: {
            default: [],
            index: true,
            type: [Tag]
        }
    },
    {
        strict: false
    }
)

ItemDetail 架构:

var Item_Detail = new Schema(
    {
        content: {
            default: "",
            index: true,
            type: String,
            trim: true,
            validate: {
                validator(v)
                {
                    if (this.title.trim() === "price")
                    {
                        return !isNaN(v.trim())
                    }
                    return true
                }
            }
        },
        hidden: {
            default: false,
            index: true,
            type: Boolean
        },
        order: {
            index: true,
            required: true,
            type: Number
        },
        table: {
            default: {},
            type: Mixed
        },
        title: {
            required: true,
            index: true,
            type: String,
        },
        type: {
            default: "text",
            enum: ["text", "table"],
            index: true,
            type: String
        },
    },
    {
        strict: false
    }
)

【问题讨论】:

标签: mongodb mongoose


【解决方案1】:

它给出这个错误的原因是由于变量的定义。 Item 是在 Item_Detail 之前声明的,这意味着它当时无法访问 Item_Detail

var Item = new Schema(...) // Item_Detail is referenced in here
var Item_Detail = new Schema(...)

固定为

var Item_Detail = new Schema(...) // declare before
var Item = new Schema(...)

【讨论】:

    猜你喜欢
    • 2018-09-04
    • 2017-04-01
    • 2016-11-23
    • 2021-03-26
    • 2020-06-07
    • 1970-01-01
    • 2020-03-27
    • 2020-02-07
    • 2016-10-06
    相关资源
    最近更新 更多