关于mongodb数据库相关问题
最近被一个MongoDB数据库的问题困扰了会很久,我把问题写在下面,如果社区中有懂得相关知识的小伙伴,麻烦你们帮我看一下,看看问题的根源在哪,谢谢了。

目录层次如上面的图所示:
model中comment.js文件代码:
var mongoose = require("mongoose");
var db = require("./db.js");
var Schema = mongoose.Schema;
var ObjectId = Schema.Types.ObjectId;
var CommentSchema = new mongoose.Schema({
movie: {type: ObjectId, ref: 'Movie'},
from: {type: ObjectId, ref: 'User'},
to: {type: ObjectId, ref: 'User'},
content: String,
meta: {
createAt: {
type: Date,
default: Date.now()
},
updateAt: {
type: Date,
default: Date.now()
}
}
})
var CommentModel = db.model('comment', CommentSchema);
module.exports = CommentModel;
其中引入的db模块是用于连接数据库:
var mongoose = require('mongoose');
mongoose.Promise = global.Promise;
var db = mongoose.createConnection('mongodb://localhost/newproject');
db.once('open',function (callback) {
console.log("数据库打开成功");
})
module.exports = db;
models中comment.js代码:
var Comment = require('../models/comment');
//comment save
exports.save = function (req,res) {
var _comment = req.body.comment;
var movieId = _comment.movie;
var comment = new Comment(_comment);
console.log(comment);
comment.save(function (err, result) {
if (err) {
console.log(err);
return;
}
else {
console.log("存储成功了!");
}
})
}
问题描述:当触发路由调用save方法时,数据无法存储入数据库
如果有会的小伙伴,请指教一下,谢谢了。

目录层次如上面的图所示:
model中comment.js文件代码:
var mongoose = require("mongoose");
var db = require("./db.js");
var Schema = mongoose.Schema;
var ObjectId = Schema.Types.ObjectId;
var CommentSchema = new mongoose.Schema({
movie: {type: ObjectId, ref: 'Movie'},
from: {type: ObjectId, ref: 'User'},
to: {type: ObjectId, ref: 'User'},
content: String,
meta: {
createAt: {
type: Date,
default: Date.now()
},
updateAt: {
type: Date,
default: Date.now()
}
}
})
var CommentModel = db.model('comment', CommentSchema);
module.exports = CommentModel;
其中引入的db模块是用于连接数据库:
var mongoose = require('mongoose');
mongoose.Promise = global.Promise;
var db = mongoose.createConnection('mongodb://localhost/newproject');
db.once('open',function (callback) {
console.log("数据库打开成功");
})
module.exports = db;
models中comment.js代码:
var Comment = require('../models/comment');
//comment save
exports.save = function (req,res) {
var _comment = req.body.comment;
var movieId = _comment.movie;
var comment = new Comment(_comment);
console.log(comment);
comment.save(function (err, result) {
if (err) {
console.log(err);
return;
}
else {
console.log("存储成功了!");
}
})
}
问题描述:当触发路由调用save方法时,数据无法存储入数据库
如果有会的小伙伴,请指教一下,谢谢了。
...全文
相关推荐
mongodb集群搭建
mongodb集群搭建
MongoDB日常使用的技巧与注意事项汇总
主要给大家总结介绍了关于MongoDB日常使用的一些技巧与注意事项,文中通过示例代码介绍的非常详细,对大家学习或者使用MongoDB具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。
java_postgredb_mongodb
java_postgredb_mongodb
18170003游英甲_sql_database_
数据仓库,数据库基础知识,数据仓库设计,学校编程课作业
nodejs连接mongodb数据库实现增删改查
本篇文章主要结合了nodejs操作mongodb数据库实现增删改查,包括对数据库的增加,删除,查找和更新,有兴趣的可以了解一下。


community_281
