mongodb / mongoose-如何使用嵌套数组字段插入新的子文档

时间:2018-08-27 12:27:19

标签: mongodb mongoose

原始问题:如何插入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
    }
)

1 个答案:

答案 0 :(得分:0)

出现此错误的原因是由于变量的定义。 ItemItem_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(...)