我是 node.js 和 mongodb 的新手。
我正在尝试使用以下代码从 node.js 应用程序中为 mongolab mongodb 数据库中的用户集合创建模式。代码似乎没有失败(至少,我没有收到错误消息),但我也没有看到任何成功的迹象。也就是说,当我去 mongolab 并查看我的数据库时,我没有看到创建了任何模式 - https://dzwonsemrish7.cloudfront.net/items/01263Y1c312s233V0R17/mongodb-schema.png?v=7fdc20e3。
有人可以解释我可能做错了什么,或者我如何验证我的代码是否成功并且实际上是为我的集合创建了一个模式?
// 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 + " ...");
});