MongoDB和Mongoose之间的区别 [英] Difference between MongoDB and Mongoose

查看:360
本文介绍了MongoDB和Mongoose之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用mongodb数据库,但是我注意到有两个不同的数据库,它们都有自己的网站和安装方法:mongodb和mongoose.因此,我问自己一个问题:我使用哪个?".

因此,为了回答这个问题,我问社区是否可以解释这两者之间的区别?以及是否有利弊?因为它们看起来和我非常相似.

我假设您已经知道MongoDB是一个NoSQL数据库系统,它以BSON文档的形式存储数据.但是,您的问题是关于Node.js的软件包的.

就Node.js而言, mongodb 本地驱动程序 >用于与mongodb实例和 mongoose 进行交互的对象建模工具用于MongoDB.

Mongoose构建在MongoDB驱动程序之上,为程序员提供了一种对其数据进行建模的方法.

我不想评论哪个更好,因为这会使这个答案变得自以为是.但是,我将列出同时使用这两种方法的优点和缺点.

使用猫鼬,用户可以为特定集合中的文档定义架构.它为MongoDB中的数据创建和管理提供了很多便利.不利的一面是,学习猫鼬可能会花费一些时间,并且在处理非常复杂的模式时会有一些限制.

但是,如果您的收集模式是不可预测的,或者您想在Node.js中体验类似Mongo-shell的体验,那么请继续使用MongoDB驱动程序.这是最简单的拿起.不利之处在于,您将不得不编写大量代码来验证数据,并且出错的风险更高.

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

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.

解决方案

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.

In terms of Node.js, mongodb is the native driver for interacting with a mongodb instance and mongoose is an Object modeling tool for MongoDB.

Mongoose is built on top of the MongoDB driver to provide programmers with a way to model their data.

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.

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.

这篇关于MongoDB和Mongoose之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆