Reputation: 60
I'm trying to create a database in MongoDB using npm Mongoose and Robomongo as my GUI. I followed the MongoDB Quickstart Docs at http://mongoosejs.com/docs/index.html, but the database does not appear in Robomongo. Basically, I'm just trying to have the database "appear" in Robomongo when I run the server.js file through Node.
When I enter the code below, from the MongoDB Quickstart Docs, there is NO database created/appearing in Robomongo.
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/test');
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
// we're connected!
});
var kittySchema = mongoose.Schema({
name: String
});
var Kitten = mongoose.model('Kitten', kittySchema);
But... When I add in the employeeid field (below) into the Schema, then the database appears in Robomongo. Can somebody explain the difference between these two pieces of code? Why one works and the other doesn't?
var kittySchema = mongoose.Schema({
name: {type: String},
employeeid:{
type: Number,
unique: true,
required: true
}
});
Upvotes: 0
Views: 602
Reputation: 5538
It's because employeeid
has an index on it (from unique
, at least -- probably required
too? not 100% sure on how mongoose handles required fields). Mongoose will automatically create collections which have indexes defined on them.
Otherwise, the collection will be created after you "do" something with it (like creating a Kitten
and using .save()
wiht it).
Upvotes: 1