简体   繁体   English

不了解猫鼬模型?

[英]Not understanding the Mongoose Model?

I am currently in the process of learning the MEAN stack, and in my studies I am currently on Mongoose. 我目前正在学习MEAN堆栈,并且在学习中目前正在使用Mongoose。

I have an issue understanding what is the model below used for: 我在了解以下模型的用途时遇到问题:

 var campgroundSchema = new mongoose.Schema({ name: String, image: String }); var Campground = mongoose.model("Campground", campgroundSchema); 

Why is it needed if the MongoDB itself is a NoSQL database? 如果MongoDB本身是NoSQL数据库,为什么需要它? The only explanation I can think of is that it is made so that you can use the standard functions such as find() and create(). 我能想到的唯一解释是,它可以使您可以使用诸如find()和create()之类的标准函数。

Are there other ways to do these things? 还有其他方法可以做这些事情吗?

Also a MORE IMPORTANT QUESTION : What does the first argument ("Campground") in the code above refer to? 还有一个更重要的问题 :上面的代码中的第一个参数(“ Campground”)指的是什么? I can't find it anywhere in the database. 我在数据库的任何地方都找不到它。

I've read the documentation and I still don't understand it. 我已经阅读了文档,但仍然不明白。

MongoDB is a NoSQL database, which means the documents (their term for records) stored in the database are unstructured unlike SQL database. MongoDB是NoSQL数据库,这意味着与SQL数据库不同,存储在数据库中的文档 (其记录术语)是非结构化的。 There are no set of fixed columns. 没有固定列的集合。 A great example of a use case for MongoDB is if you're storing data for social media things like Facebook where data about a user can take many forms. MongoDB用例的一个很好的例子是,如果您要存储诸如Facebook之类的社交媒体数据,其中有关用户的数据可以采用多种形式。

However, it lacks the ability to perform any data validation or provide some form of structure for its documents. 但是,它缺乏执行任何数据验证或为其文档提供某种形式的结构的能力。 If say you have a web service that takes food orders and a hacker manages to figure out how to send requests to make food order and decides to add another property that does not exist in your collection (equivalent to SQL's table), he/she will be able to do so due to the flexibility of MongoDB. 如果说您有一个接受食物订单的Web服务,而黑客设法弄清楚如何发送发出食物订单的请求,并决定添加您的集合中不存在的另一个属性(等同于SQL的表),他/她将由于MongoDB的灵活性,因此能够做到这一点。

Mongoose solves this problem. 猫鼬解决了这个问题。 It allows you to define something called a schema , which provides a backbone for how data inside a collection should look like. 它允许您定义一个称为schema的东西,它为集合中的数据外观提供了基础。 You can also use a schema to restrict data types, set the fields to be required when storing data, and much more as a result of using this tool. 您还可以使用架构来限制数据类型,设置存储数据时必填的字段,以及使用此工具的更多结果。 The side effect is that you get data validation and data structure all in one package. 副作用是您可以将数据验证和数据结构全部打包在一个包中。

If you didn't use Mongoose, you'd have to write all this validation code yourself. 如果您不使用Mongoose,则必须自己编写所有验证代码。 However, keep in mind that mongoose is not simply a validation and schema tool. 但是,请记住,猫鼬不仅仅是一个验证和架构工具。 It also is packed with lots of other features such as sorting data that is returned. 它还包含许多其他功能,例如返回的排序数据。 Think of it as an add-on for MongoDB to make it even easier to use. 可以将其视为MongoDB的附加组件,以使其更易于使用。

Even though MongoDB is a NOSQL database, you need Schema in the design because you can have a structure. 即使MongoDB是NOSQL数据库,在设计中仍需要Schema,因为您可以拥有结构。 It'is an object that defines the structure of any documents that will be stored in your MongoDB collection; 它是一个对象,用于定义将存储在MongoDB集合中的所有文档的结构; it enables you to define types and validators for all of your data items. 它使您能够为所有数据项定义类型和验证器。

In the above Campground is the name that you have given for the model which you can use to do the crud operations. 在上面的Campground中,您为模型指定了名称,可用于执行分类操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM