Node.js   发布时间:2022-04-24  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了node.js – 从Mongoose 3.x升级到Mongoose 4.x时,如何在模式中使用模式?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有2个文件

关键特点:

@H_911_9@mongoose = require('mongoose'); scheR_448_11845@a = mongoose.scheR_448_11845@a; DescriptionscheR_448_11845@a = require('./description'); KeyFeaturescheR_448_11845@a = new scheR_448_11845@a({ descriptions: [DescriptionscheR_448_11845@a],title: String,key: String,display: String,feature: { type: scheR_448_11845@a.Types.ObjectId,ref: 'Module' } }); module.exports = KeyFeaturescheR_448_11845@a;

描述:

@H_911_9@mongoose = require('mongoose'); DescriptionscheR_448_11845@a = new mongoose.scheR_448_11845@a(); DescriptionscheR_448_11845@a.add({ display: String,subdescriptions: [String] }); module.exports = DescriptionscheR_448_11845@a;

如何重写它以与Mongoose 4 .add语法兼容?

解决方法

我不确定我是否理解你的问题,但这就是我得到的.

根据mongoose .add documentation for 4.5.2,添加语法仍然相同.所以我用你的模式制作了一个简单的tesTing.js文件.我创建了一个keyFeature对象,其中一个对象在description数组中并保存.之后,我使用mongo控制台连接到mongod并查询keyfeatures集合.该对象已正确插入db.

这是我的代码

var mongoose = require('mongoose');
var DescriptionscheR_448_11845@a = require('./description.js');
var KeyFeaturescheR_448_11845@a = require('./keyFeature.js');

mongoose.connect('mongodb://localhost/mongoose-tesTing',function(err)    {
    if(err){
        console.log('Could not connect to mongoose',err);
    }else{
        console.log('successfully connected to mongoose');
        mongoose.model('Description',DescriptionscheR_448_11845@a);
        mongoose.model('Keyfeature',KeyFeaturescheR_448_11845@a);
        var Keyfeature = mongoose.model('Keyfeature');

        new Keyfeature({
            descriptions: [{
                display: 'This is the display content from descriptions',subdescriptions: ['Subdescription 1']
            }],title: 'tittle',key: 'key',display: 'display'
        }).save(function(err){
            if(err){
                console.log('Error when saving',err);
            }else{
                console.log('Saved');
            }
        });
    }
});

用mongoose@4.5.2测试

大佬总结

以上是大佬教程为你收集整理的node.js – 从Mongoose 3.x升级到Mongoose 4.x时,如何在模式中使用模式?全部内容,希望文章能够帮你解决node.js – 从Mongoose 3.x升级到Mongoose 4.x时,如何在模式中使用模式?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。