Saturday 15 March 2014

node.js - Mongoose ODM use -


I've read an article in this link, here's a code:

  export. Teamlist = action (gemma, callback) {db.once ('open', function () {var teamSchema = new mongoose.Schema ({country: string, groupname: string}) var team = db.model ('team', Team (circumscribed); Team.find ({'GroupName': gname}, function (error, team) {if (err) {onErr (err, callback);} else {mongoose.connection.close (); console.log (Team); callback ("", teams);}}) // and team.fund}); // and db.ones open};   

Here db.once method Is called, while used in other places

  var mongoose = require ('mongoose'), Schema = Mongoose schema, object ID = schema. Object ID; var postSchema = new schema ({thread: object ID, date: {type: Date, default: date.nuh}, author: {type: string, default: 'anonymous'}, post: string}); Module.exports = mongoose.model ('post', postSchema);   

use it in the router part such as

  export.show = (function (Rick, Race) {Thread.findOne ({title: req. Params.title}, function (error, thread) {var posts = post.fund ({thread: thread._id}, function (error, post) {res.send ([{thread: thread, post: post}}) ;});})});   

and in app.js

  var mongoose = is required ('magos'); Mongoose.connect ('MongoDB: // localhost / norum');   

I do not understand why the two have different perspectives and who is better and why? Can someone help me What I have seen is that the second method is most used. So please help me, which has a better view about. I know that this is mainly concerned with the construction of plans in Mongodb, and therefore once the method looks better but I am not perfect at all. help please.

Actually, in the first approach - this shows you with Fife that what you can do with Mongoose Are there. It seems that somewhere you were opening a connection to the database (possibly returning some code of code). Then he listens to the incident and later all the jobs are done. It's just to show you what you can do.

For intent, it creates a Mongoose model (this is not dependent on database connection) - so it can be easily included from a separate file, usually written as a module Because it is in another approach

  var teamSchema = new mongoose.Schema ({country: string, groupname: string}); Var team = DB.Model ('Team', Team Skaima);   

Later in the first approach you close the connection to the database, but this is not what you want to do in the actual application. You have a connection pool, and to use it often to query the database, the connection is only required when you want to stop running the application.

  Team.find ({'GroupName': callback ("", teams);} <{EGR (mistake, callback);} and {Mongoose.connection.clos ()); Console.log (teams);} / code>  

The first line - you search the database, if there was no mistake - you close the connection, log the results, etc.

Nevertheless, for yoga - the first approach is usually a show-off, second - it's like it in the real world - where you separate the argument, the model is reusable and Inclusive, where you have router logic

No comments:

Post a Comment