Mongoose Models

34 views
Skip to first unread message

Saransh Mohapatra

unread,
Jun 21, 2013, 1:39:46 PM6/21/13
to mongoo...@googlegroups.com
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.










Aaron Heckmann

unread,
Jun 28, 2013, 4:16:17 PM6/28/13
to mongoo...@googlegroups.com
Which was is better is debatable. Mongoose buffers commands before the connection opens, once opened all commands are executed. This is a little magical leaving some folks to prefer to explicitly register a callback to the open event and continue from there.


--
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.
 
 



--
Aaron


Simon Holmes

unread,
Jun 29, 2013, 6:06:15 PM6/29/13
to mongoo...@googlegroups.com
For what it's worth I tend to use the 'normal' Mongoose approach. In the article I was building on a series of previous posts which had just used MongoDB natively, and in this instance shoe-horned the Mongoose code into the existing framework.

When starting from scratch I let Mongoose do all of the hard work ... that's what it's good at!
Reply all
Reply to author
Forward
0 new messages