11

I'm new to node.js and mongodb.

I'm trying to create a schema for a User collection in a mongolab mongodb database from a node.js app with the code below. The code does not seem to be failing (at least, I get no error messages), but I don't see any indication that it is succeeding either. That is, when I go to mongolab and look at my database, I don't see that any schema was created - https://dzwonsemrish7.cloudfront.net/items/01263Y1c312s233V0R17/mongodb-schema.png?v=7fdc20e3.

Can someone explain what I might be doing wrong, or how I can verify that my code succeeded and a schema was, in fact, created for my collection?

// 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 + " ...");
});
Cœur
  • 37,241
  • 25
  • 195
  • 267
RBR
  • 999
  • 3
  • 13
  • 24

2 Answers2

9

You must insert a document first. Schemas are not explicitly defined in mongodb. Once you insert a document, the collection will automatically be created and you will see it in the mongolab console.

Example from 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');
});

after the save call above the collection will be created

Brian Cajes
  • 3,274
  • 3
  • 21
  • 22
  • How do I do this? What should I add/change in the code above? – RBR Nov 11 '12 at 00:41
  • You have to review the mongoose API documentation. There are also examples around stack overflow eg. http://stackoverflow.com/q/10520501/579461 – Brian Cajes Nov 11 '12 at 01:03
  • I did read the mongoose doc, and it said: "A model is a class with which we construct documents." which implies that the model comes before the document, but you need a schema to create a model. So the order seems to be: schema -> model -> document. Sounds like you're saying the order is document -> schema -> model. I'm confused. – RBR Nov 11 '12 at 01:41
  • The order you mentioned is correct, but you have not created or inserted a document. Until you insert a document, the mongodb server will not create a collection in this case. Nothing is actually done on the server side when you define your mongoose schema or model. Updating answer with an example. – Brian Cajes Nov 11 '12 at 02:30
  • Aha! Thanks, Brian. Sorry for the confusion ... I'm reading this book (in the wee hours of the morning when I really should be sleeping instead), and [I thought] it said that after I create a Schema, I should see something in mongolab that shows the schema was created. This did not happen, so I thought my code was failing silently. But now I get it. Thanks again. – RBR Nov 11 '12 at 03:03
0

Data in MongoDB has a flexible schema. Documents in the same collection do not need to have the same set of fields or structure, and common fields in a collection’s documents may hold different types of data.

Shyam Bhagat
  • 759
  • 6
  • 7