node.js MongoDB 和猫鼬的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28712248/
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
Difference between MongoDB and Mongoose
提问by Ravindra Galav
I wanted to use the mongodb database, but I noticed that there are two different databases with either their own website and installation methods: mongodb and mongoose. So I came up asking myself this question: "Which one do I use?".
我想使用 mongodb 数据库,但我注意到有两个不同的数据库,它们都有自己的网站和安装方法:mongodb 和 mongoose。所以我问自己这个问题:“我使用哪个?”。
So in order to answer this question I ask the community if you could explain what are the differences between these two? And if possible pros and cons? Because they really look very similar to me.
因此,为了回答这个问题,我问社区您是否可以解释这两者之间的区别?如果可能的话,利弊?因为他们真的很像我。
回答by ZeMoon
I assume you already know that MongoDB is a NoSQL database system which stores data in the form of BSON documents. Your question, however is about the packages for Node.js.
我假设您已经知道 MongoDB 是一个 NoSQL 数据库系统,它以 BSON 文档的形式存储数据。但是,您的问题是关于 Node.js 的包。
In terms of Node.js, mongodbis the native driverfor interacting with a mongodb instance and mongooseis an Object modeling toolfor MongoDB.
就 Node.js 而言,mongodb是与 mongodb 实例交互的本机驱动程序,而mongoose是 MongoDB的对象建模工具。
Mongoose is built on top of the MongoDB driver to provide programmers with a way to model their data.
Mongoose 建立在 MongoDB 驱动程序之上,为程序员提供了一种对其数据建模的方法。
EDIT:I do not want to comment on which is better, as this would make this answer opinionated. However I will list some advantages and disadvantages of using both approaches.
编辑:我不想评论哪个更好,因为这会使这个答案自以为是。但是,我将列出使用这两种方法的一些优点和缺点。
Using Mongoose, a user can define the schema for the documents in a particular collection. It provides a lot of convenience in the creation and management of data in MongoDB. On the downside, learning mongoose can take some time, and has some limitations in handling schemas that are quite complex.
使用 Mongoose,用户可以为特定集合中的文档定义架构。它为MongoDB中数据的创建和管理提供了很多便利。不利的一面是,学习 mongoose 可能需要一些时间,并且在处理非常复杂的模式方面有一些限制。
However, if your collection schema is unpredictable, or you want a Mongo-shell like experience inside Node.js, then go ahead and use the MongoDB driver. It is the simplest to pick up. The downside here is that you will have to write larger amounts of code for validating the data, and the risk of errors is higher.
但是,如果您的集合模式不可预测,或者您想要在 Node.js 中获得类似 Mongo-shell 的体验,那么请继续使用 MongoDB 驱动程序。捡起来是最简单的。这里的缺点是您将不得不编写大量代码来验证数据,并且出错的风险更高。
回答by Anubhav Singh
Mongo is NoSQL Database.
Mongo 是 NoSQL 数据库。
If you don't want to use any ORMfor your data models then you can also use native driver mongo.js: https://github.com/mongodb/node-mongodb-native.
如果您不想对数据模型使用任何ORM,那么您也可以使用本机驱动程序 mongo.js:https: //github.com/mongodb/node-mongodb-native。
Mongoose is one of the orm's who give us functionality to access the mongo data with easily understandable queries.
Mongoose 是 orm 之一,它为我们提供了通过易于理解的查询访问 mongo 数据的功能。
Mongoose plays as a role of abstraction over your database model.
Mongoose 充当数据库模型的抽象角色。
回答by Rahul
One more difference I found with respect to both is that it is fairly easy to connect to multiple databaseswith mongodb native driverwhile you have to use work arounds in mongoosewhich still have some drawbacks.
我发现两者的另一个区别是它很容易使用connect to multiple databases,mongodb native driver而您必须使用mongoose仍然存在一些缺点的变通方法。
So if you wanna go for a multitenant application, go for mongodb native driver.
因此,如果您想使用多租户应用程序,请选择 mongodb 本机驱动程序。
回答by Shreyash Pandey
mongo-dbis likely not a great choice for new developers.
On the other hand mongooseas an ORM (Object Relational Mapping) can be a better choice for the new-bies.
mongo-db对于新开发人员来说,这可能不是一个很好的选择。
另一方面,mongoose作为 ORM(对象关系映射)对于新手来说可能是更好的选择。
回答by dhaker
Mongodb and Mongoose are two different drivers to interact with MongoDB database.
Mongodb 和 Mongoose 是与 MongoDB 数据库交互的两个不同的驱动程序。
Mongoose: object data modeling (ODM) library that provides a rigorous modeling environment for your data. Used to interact with MongoDB, it makes life easier by providing convenience in managing data.
Mongoose:对象数据建模 (ODM) 库,可为您的数据提供严格的建模环境。用于与 MongoDB 交互,通过提供管理数据的便利性使生活更轻松。
Mongodb: native driver in Node.js to interact with MongoDB.
Mongodb:Node.js 中的本地驱动程序,用于与 MongoDB 交互。
回答by ANK
If you are planning to use these components along with your proprietary code then please refer below information.
如果您计划将这些组件与您的专有代码一起使用,请参阅以下信息。
Mongodb:
MongoDB:
- It's a database.
- This component is governed by the Affero General Public License (AGPL) license.
- If you link this component along with your proprietary code then you have to release your entire source code in the public domain, because of it's viral effect like (GPL, LGPL etc)
- If you are hosting your application over the cloud, the (2) will apply and also you have to release your installation information to the end users.
- 它是一个数据库。
- 此组件受 Affero 通用公共许可证 (AGPL) 许可证管理。
- 如果您将此组件与您的专有代码链接在一起,那么您必须在公共领域发布您的整个源代码,因为它具有病毒效应,例如(GPL、LGPL 等)
- 如果您通过云托管您的应用程序,则 (2) 将适用,并且您还必须向最终用户发布您的安装信息。
Mongoose:
猫鼬:
- It's an object modeling tool.
- This component is governed by the MIT license.
- Allowed to use this component along with the proprietary code, without any restrictions.
- Shipping your application using any media or host is allowed.
- 它是一个对象建模工具。
- 该组件受 MIT 许可管理。
- 允许将此组件与专有代码一起使用,没有任何限制。
- 允许使用任何媒体或主机发送您的应用程序。
回答by Secret
Mongodb and Mongoose are two completely different things!
Mongodb 和 Mongoose 是两个完全不同的东西!
Mongodb is the database itself, while Mongoose is an object modeling toolfor Mongodb
Mongodb是数据库本身,而Mongoose是Mongodb的对象建模工具。
EDIT: As pointed out MongoDB is the npm package, thanks!
编辑:正如所指出的 MongoDB 是 npm 包,谢谢!

