I read an article in this link http://theholmesoffice.com/mongoose-and-node-js-tutorial/
here there is a code
exports.teamlist = function(gname,callback){
db.once('open', function(){
var teamSchema = new mongoose.Schema({
country: String,
GroupName: String
});
var Team = db.model('Team', teamSchema);
Team.find({'GroupName':gname}, function (err, teams) {
if(err){
onErr(err,callback);
}else{
mongoose.connection.close();
console.log(teams);
callback("",teams);
}
})// end Team.find
});// end db.once open
};
Here it calls db.once method whereas in other places it like this
var mongoose = require('mongoose') ,Schema = mongoose.Schema ,ObjectId = Schema.ObjectId; var postSchema = new Schema({ thread: ObjectId, date: {type: Date, default: Date.now}, author: {type: String, default: 'Anon'}, post: String}); module.exports = mongoose.model('Post', postSchema);
And than in the router part its used like this
exports.show = (function(req, res) { Thread.findOne({title: req.params.title}, function(error, thread) { var posts = Post.find({thread: thread._id}, function(error, posts) { res.send([{thread: thread, posts: posts}]); }); })});
And in the app.js there is:
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/norum');
I dont understand why these two have different approach and which one is the better one and why? Can Anyone please help me. One thing that I have observed is that the second approach is the one most used. SO please help me as to which one is a better approach.
I know this is mainly concerned regarding creation of the schemes in Mongodb, and so the once method looks little better. But still I am not at all sure. Please help.
--
Documentation - http://mongoosejs.com/
Plugins - http://plugins.mongoosejs.com/
Bug Reports - http://github.com/learnboost/mongoose
Production Examples - http://mongoosejs.tumblr.com/
StackOverflow - http://stackoverflow.com/questions/tagged/mongoose
Google Groups - https://groups.google.com/forum/?fromgroups#!forum/mongoose-orm
Twitter - https://twitter.com/mongoosejs
IRC - #mongoosejs
---
You received this message because you are subscribed to the Google Groups "Mongoose Node.JS ODM" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongoose-orm...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.