在node.js中的mongolab mongodb数据库中创build一个集合模式

我是新的node.js和mongodb …请帮助。

我正在尝试使用下面的代码从node.js应用程序为mongolab mongodb数据库中的用户集合创build模式。 代码似乎没有失败(至less,我没有得到任何错误消息),但我没有看到任何迹象表明它是成功的。 也就是说,当我去mongolab,看看我的数据库,我没有看到任何模式创build – http://cl.ly/image/0f1y273m2i0X 。

有人可以解释我可能做错了什么,或者我怎么可以validation我的代码是否成功,并且实际上为我的集合创build了一个模式?

—- BEGIN CODE —-

// file: app.js var express = require('express'), http = require('http'), mongoose = require('mongoose'); var app = express(), port = 3000; // Connect to database in the cloud (mongolab) mongoose.connect('mongodb://username:password@ds041344.mongolab.com:41344/stockmarket'); // Create a schema for User collection mongoose.connection.on('open', function () { console.log(">>> Connected!"); var UserSchema = new mongoose.Schema({ username: {type: String, unique: true}, password: String }); var UserModel = mongoose.model('User', UserSchema); }); app.get('/', function(req, res){ res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello, World!\n'); }); http.createServer(app).listen(port, function(){ console.log("Express server listening on port " + port + " ..."); }); 

—-结束代码—-

您必须先插入文件。 模式在MongoDB中没有明确的定义。 一旦你插入一个文件,该集合将自动创build,你会看到它在蒙戈拉布控制台。

来自http://mongoosejs.com/的示例

 var mongoose = require('mongoose'); var db = mongoose.createConnection('localhost', 'test'); var schema = mongoose.Schema({ name: 'string' }); var Cat = db.model('Cat', schema); var kitty = new Cat({ name: 'Zildjian' }); kitty.save(function (err) { if (err) // ... console.log('meow'); }); 

在集合之上的保存调用之后将被创build

MongoDB中的数据具有灵活的模式。 同一集合中的文档不需要具有相同的字段或结构集合,集合文档中的公共字段可以包含不同types的数据。