node.js Mongodb 的和尚 vs 猫鼬

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23615377/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 17:19:49  来源:igfitidea点击:

monk vs mongoose for Mongodb

node.jsmongodbmongoosemonk

提问by Young

I'm learning NodeJs.

我正在学习 NodeJs。

To connect to and use MongoDB from NodeJS, I see a lot of examples using either Monk or Mongoose.

为了从 NodeJS 连接和使用 MongoDB,我看到了很多使用 Monk 或 Mongoose 的示例。

Are these two libraries equivalent ? Do they have the same features or do they each have a specific purpose ?

这两个库是等价的吗?它们具有相同的功能还是每个都有特定的用途?

As a beginner with NodeJS, which should I use ?

作为 NodeJS 的初学者,我应该使用哪个?

Here are some examples of code that uses Monk :

以下是一些使用 Monk 的代码示例:

var mongo = require('mongodb');
var monk = require('monk');
var db = monk('localhost:27017/nodejsapp');

----
exports.userlist = function(db) {
    return function(req, res) {
        var collection = db.get('users');
        collection.find({},{},function(e,docs){
            res.render('userlist', {
                "userlist" : docs
            });
        });
    };
};

and here a sample that uses Mongoose :

这里是一个使用 Mongoose 的示例:

   var mongoose = require('mongoose');
----
 mongoose.connect('localhost', 'test');
 var db = mongoose.connection;
  db.on('error', console.error.bind(console, 'connection error:'));
  db.once('open', function callback() {
   console.log('Connected to DB');
});

// User Schema
var userSchema = mongoose.Schema({
   username: { type: String, required: true, unique: true },
   email: { type: String, required: true, unique: true },
  password: { type: String, required: true},
});

回答by Peter Lyons

are they same thing do the same connection ? or do they have specific purpose ?

他们做同样的事情做同样的连接吗?或者他们有特定的目的吗?

They are different, although they are two approaches to the same basic problem. Mongoose is a quite sophisticated full-on ORM. More features, but more complexity. Monk is smaller in scope and thus easier to understand.

它们是不同的,尽管它们是解决同一基本问题的两种方法。Mongoose 是一个非常复杂的完整 ORM。更多功能,但更复杂。Monk 的范围更小,因此更容易理解。

My suggestion is start coding with the basic mongodbdriver module directly. When you understand how that works, and how parts of it are annoying, you will understand the benefit of monkand can try that out to see if you like it. I wouldn't recommend mongooseto a beginner. Mongodb is already tricky enough to learn and while mongoose can be helpful, it's API is quite magical and assumes you already know the tricky aspects of mongodb.

我的建议是mongodb直接使用基本驱动程序模块开始编码。当您了解它的工作原理以及其中的一部分令人讨厌时,您就会了解它的好处monk并可以尝试看看您是否喜欢它。我不会推荐mongoose给初学者。Mongodb 已经足够难学了,虽然 mongoose 可以提供帮助,但它的 API 非常神奇,并且假设您已经了解 mongodb 的棘手方面。

回答by alernerdev

If you are learning NodeJS and Mongo, you really dont need anything else -- in fact, MongoDB offers a free online class for MongoDB and NodeJS developers. No need for any additional wrappers.

如果您正在学习 NodeJS 和 Mongo,那么您真的不需要其他任何东西——事实上,MongoDB 为 MongoDB 和 NodeJS 开发人员提供了一个免费的在线课程。不需要任何额外的包装器。

See https://university.mongodb.com/

https://university.mongodb.com/