
var mOngoose= require('mongoose'); var Schema = mongoose.Schema; mongoose.connect('mongodb://localhost/test'); var db = mongoose.connection; db.on('error', console.error.bind(console, 'connection error:')); db.once('open', function (callback) { // yay! var blogSchema = new Schema({ title: String, author: String, body: String, comments: [{ body: String, date: Date }], date: { type: Date, default: Date.now }, hidden: Boolean, meta: { votes: Number, favs: Number } }); var Blog = mongoose.model('Blog', blogSchema); var blog = new Blog({ title: 'this is my blog title', author: 'me', body: 'the body of my blog. can you see that?' }); blog.save(); }); 开发环境: Nodejs + Express + Mac + MongoDB, 都在最新版本
1 DoraJDJ 2016-06-01 14:57:20 +08:00 via Android 用其他工具查的时候试试看 Blogs ? |
2 GordianZ 2016-06-01 14:58:04 +08:00 .save 的返回你都没处理,你怎么知道保存成功了…… |
3 Mirachael 2016-06-01 15:10:25 +08:00 是 blogs 命名的 collection 吧,如果你要指定自定义的 collection, 需要在 schema 中设置,{collection: 'Blog'} |
4 gyteng 2016-06-01 15:3:03 +08:00 其它工具是指 Robomongo 吗,旧版本的连接 mongodb 3.2 会有这个问题 |
6 CareiOS OP @GordianZ 我在 save 中加了一个 function(error, model) {} ,error 为空, model 是有值的。 |
7 CareiOS OP @mojixiang1102 我直接安装好 mongodb, 然后运行的 shell:mongod --dbpath ./data , 运行以上代码后,用 Robomongo 查看数据库 test 创建成功了,就是没有 Collections, test 数据库下面 Collections,Functions,Users 都为 0 |
10 doublelam 2016-06-01 17:17:05 +08:00 mongoose 会自动在集合名后加 s ,所以你的集合其实是 blogs ,如果要指定是 blog ,这样写: var Blog = mongoose.model('Blog', blogSchema,'Blog'); |