node.js 为什么需要,使用猫鼬有什么好处

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18531696/
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 15:27:50  来源:igfitidea点击:

Why do we need, what advantages to use mongoose

node.jsmongodbmongoose

提问by codeofnode

I have just started up with mongodband I recently gone through Mongoose, an ODM framework.

我刚刚开始使用,mongodb并且最近经历了Mongoose一个 ODM 框架。

On the documentation, I couldn't find why we need to use Mongoose. One reason I can give is we can define application schema from Mongoose.

在文档中,我找不到为什么我们需要使用Mongoose. 我可以给出的一个原因是我们可以从Mongoose.

I am looking for more possible reasons, and needs that will attract me to use Mongoose.

我正在寻找更多可能的原因,以及吸引我使用Mongoose.

Please list all possible advantages and reasons/needs why use Mongoose.

请列出所有可能的优点和原因/需要为什么使用Mongoose.

回答by moka

Main advantage is abstraction over pure mongo.

主要优点是对纯 mongo 的抽象。

Many developers who come from SQL database types feel very uncomfortable working with dynamic collections that have no structure defined. So Schemasin the first place helps with that.
Additionally, it implements validation and other neat features in order to make sure your schema is consistent when inserting/updating/finding documents from collections.

许多来自 SQL 数据库类型的开发人员在处理没有定义结构的动态集合时感到非常不舒服。因此,Schemas首先对此有所帮助。
此外,它还实现了验证和其他简洁的功能,以确保在从集合中插入/更新/查找文档时您的架构是一致的。

It also creates Modelabstraction which makes it easier to work with, so it looks like you are working with just objects rather than pure data.

它还创建了模型抽象,使其更易于使用,因此看起来您只是在处理对象而不是纯数据。

There are many other goodies like middleware, plugins, population, validation. Please check mongoose docsfor more information:

还有许多其他好东西,如中间件插件人口验证。请查看mongoose 文档以获取更多信息:

Personally, I prefer pure mongo as it is more consistent with official 10gen mongo docs and does not create abstractions that always costs some limits and rules that you will have to follow.

就我个人而言,我更喜欢纯 mongo,因为它与官方 10gen mongo 文档更一致,并且不会创建总是需要遵循一些限制和规则的抽象。

回答by Nick Pineda

If you are working with Node.js and you are pretty new NoSQL I'd recommend using the the native Node Driver(mongodb) at first.

如果您正在使用 Node.js 并且您是非常新的 NoSQL,我建议您首先使用本机 Node Driver(mongodb)。

Reasons:

原因:

  1. The syntax between the Node Driver and the Mongo shell is very similar and so you'll get a quicker grasp of how to use MongoDB in general.

  2. Models are only useful when you are scaling into a big application with a large API that needs to be broken up into a MVC system(mongoose being your models).

  1. Node Driver 和 Mongo shell 之间的语法非常相似,因此您可以更快地掌握如何使用 MongoDB。

  2. 模型是唯一有用的,当你缩放到一个大的API,它需要被分解成一个大的应用程序中号VC系统(猫鼬是你的模型)。

Pros/Cons of using Mongoose:

使用猫鼬的优点/缺点:

Pros:

优点:

  • Biggest Pro is that it has the data validation built into it(requirements of what data you will allow to be added or to update your database). It will take some work to build that yourself.(but not THAT hard)
  • It will abstract away most of the mongoDB code from the rest of the application.
  • 最大的优点是它内置了数据验证(您将允许添加哪些数据或更新数据库的要求)。自己构建它需要一些工作。(但并不难)
  • 它将从应用程序的其余部分中抽象出大部分 mongoDB 代码。

Cons

缺点

  • Biggest con is starting off with schemas right out of the gate will really defeat the purpose of using NoSQL and it will be hard to experience what is good about having a loose structured data system during the stages of rapid development.

  • Not all of your data operations will nicely fit into a characterization that can be encapsulated with a model. Encapsulation is especially hard initially - unless you have a very clear idea of the data flow before you start (which is ideal, but not easy when you are building something conceptually new and requires a lot of experimentation and change/redesign).

  • 最大的缺点是一开始就使用模式将真正违背使用 NoSQL 的目的,并且在快速开发阶段很难体验到拥有松散结构化数据系统的好处。

  • 并非所有的数据操作都能很好地适应可以用模型封装的特征。封装最初特别困难 - 除非您在开始之前对数据流有一个非常清晰的了解(这是理想的,但当您构建概念上新的东西并且需要大量实验和更改/重新设计时,这并不容易)。