Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用对象将数据保存到mongodb模式_Javascript_Node.js_Mongodb - Fatal编程技术网

Javascript 如何使用对象将数据保存到mongodb模式

Javascript 如何使用对象将数据保存到mongodb模式,javascript,node.js,mongodb,Javascript,Node.js,Mongodb,我一直在尝试将用户数据保存到mongodb模式,下面是示例模式,问题是如何在对象中插入数据。请帮忙我已经做了好几个小时了 e、 g.User.js const mongoose = require('mongoose'); const UserSchema = new mongoose.Schema({ data:{ type: String, requred: true }, data2:{ type: String,

我一直在尝试将用户数据保存到mongodb模式,下面是示例模式,问题是如何在对象中插入数据。请帮忙我已经做了好几个小时了

e、 g.User.js

const mongoose = require('mongoose');

const UserSchema = new mongoose.Schema({
    data:{
        type: String,
        requred: true
    },
    data2:{
        type: String,
        requred: true
    },
    work: {
        industry: String,
        name: String,
        position: String,
    },
    status:{
        type: Boolean,
        default: true,
        requred: true
    },
    subscription:{
        product: String,
        license: String,
        price: String,
        status: Boolean,
        default: false,
        renew: Boolean,
        default : false
    },
    date:{
        type: Date,
        default: Date.now
    }
});

const User = mongoose.model('User', UserSchema);

module.exports = User;
样本登记处理

const newUser = new User({
    data1,
    data2,
    work,
    subscription
});

newUser.save()
.then(user => {
    req.flash('success_mg', 'Successfully Registered your can now Login.');
    res.redirect('/login');
})
.catch(err => console.log(err));


您希望使用_id字段,并按如下方式调用您的模式:

User.findByIdAndUpdate({_id: id}, {...updates}, {new: true})
 .then(updated_user=>{ DO SOMETHING...  })


…updates将是一个JSON对象,包含您正在更新的属性的键和值。

请解释更多。。此代码是否将
文档
保存在数据库中?是否将文档保存在MongoDB中?您所说的在对象中插入数据是什么意思?是否要更新保存的已用数据。。或者别的什么。你连接了DB吗?@BloodyLogic当然