【问题标题】:Adding child document to existing mongodb document将子文档添加到现有的 mongodb 文档
【发布时间】:2015-12-21 13:23:03
【问题描述】:

基本上我只是想向我现有的具有以下架构的 mongodb 文档添加一个新的子文档

/models/server/destination.js

// this is the "destination" model for mongoose
var mongoose = require('mongoose')
var Adventure = require('../models/adventure')

// this is the schema that every entry will get when a new trip is made.
var tripSchema = mongoose.Schema({
    name: { type: String, required: true },
    city: { type: String, required: true },
    dateStart: { type: Date, required: true },
    dateFinish: { type: Date, required: true },
    adventures: [Adventure]
})

// module.exports makes this model available to other file 
module.exports = mongoose.model('Destination', tripSchema)

/server/models/adventure.js

var mongoose = require('mongoose')

var adventure = mongoose.Schema({
    site: String,
    rating: String,
    photo: String,
    website: String,
    date: Date
})

module.exports = mongoose.model('Adventure', adventure)

发布到冒险的 REST 路径

app.post('/api/destinations/:id/addAdventures', destinationsController.addAdventures)

/server/controllers/controller.js

module.exports.addAdventures = function(req, res) {
    var id = req.params.id;
    Destination.findOne({ _id: id }, function(err, result) {
        var adventure = new Adventure(req.body)
        var destination = result
        destination.adventures.push(adventure)
        destination.save(function(err, advresult) {
            console.log('push worked')
            res.json(advresult);
        })
    })
}

当我从destination.adventures.push() 中取出冒险时,代码不会中断,但是当我插入冒险时,我得到一个错误

/Travellog/node_modules/mongoose/lib/types/array.js:117
    return this._schema.caster.cast(value, this._parent, false);
                               ^ TypeError: undefined is not a function
    at Array.MongooseArray.mixin._cast (/Travellog/node_modules/mongoose/lib/types/array.js:117:32)
    at Array.MongooseArray.mixin._mapCast (/Travellog/node_modules/mongoose/lib/types/array.js:286:17)
    at Object.map (native)
    at Array.MongooseArray.mixin.push (/Travellog/node_modules/mongoose/lib/types/array.js:299:25)
    at Query.<anonymous> (/Travellog/server/controllers/destinations-controller.js:28:28)
    at /Travellog/node_modules/mongoose/node_modules/kareem/index.js:177:19
    at /Travellog/node_modules/mongoose/node_modules/kareem/index.js:109:16
    at process._tickCallback (node.js:355:11)

【问题讨论】:

    标签: mongodb mongoose mean-stack addchild mongoose-schema


    【解决方案1】:

    您遇到的错误是由于嵌入了 Adventure 模型而不是架构。您需要在目标架构定义中添加Adventure 架构Adventure 模型的架构属性:

    // this is the "destination" model for mongoose
    var mongoose = require('mongoose');
    var AdventureSchema = require('../models/adventure').schema; /* <- access the schema via its Model.schema property */
    
    var tripSchema = mongoose.Schema({
        name: { type: String, required: true },
        city: { type: String, required: true },
        dateStart: { type: Date, required: true },
        dateFinish: { type: Date, required: true },
        adventures: [AdventureSchema]
    });
    

    【讨论】:

    • 太棒了!效果很好,谢谢你永远坚持下去。我一直在关注 Mongoose 文档,但从外观上看,这与 node.js 真的正确吗?
    • 谢谢。我也有同样的问题。
    • 非常感谢。尝试将架构分成不同的文件,最终无意中包含了模型或其他东西。 5 小时后。
    • 这是迄今为止最好的帮助,谢谢@chridam。
    • 亲爱的耶稣!那个 .schema 正在杀死我。我现在明白了,谢谢!
    猜你喜欢
    • 2014-07-29
    • 1970-01-01
    • 2017-09-16
    • 2015-06-23
    • 2019-06-11
    • 1970-01-01
    • 2021-06-17
    • 1970-01-01
    • 2012-09-28
    相关资源
    最近更新 更多