将子文档添加到现有的mongodb文档中

编程入门 行业动态 更新时间:2024-10-14 20:23:09
本文介绍了将子文档添加到现有的mongodb文档中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

基本上,我只是想将新的子文档添加到具有以下架构的现有mongodb文档中

Essentially I am just trying to add a new sub-document to my existing mongodb document that has the following schema

/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路线

REST route to post to adventures

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()中退出冒险时,代码不会中断,但是当我插入冒险时会出现错误

When I take the adventure out of the destination.adventures.push() the code does not break, but when I insert adventures I get an error

/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)

推荐答案

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

The error you are getting is as a result of embedding the Adventure model instead of the schema. You need to add the Adventure schema in the destination schema definition the Adventure model's schema property:

// 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] });

更多推荐

将子文档添加到现有的mongodb文档中

本文发布于:2023-11-22 14:31:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1617816.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文档   mongodb

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!