乐闻世界logo
搜索文章和话题

What is the difference between mongodb and mongoose

7 个月前提问
3 个月前修改
浏览次数71

6个答案

1
2
3
4
5
6

MongoDB 是一个非关系型数据库管理系统,也被称为NoSQL数据库,它使用文档存储和JSON样式的查询语言,非常适合处理大量数据和高并发的场景。MongoDB 存储数据的基本单元是文档(Document),这些文档被组织在集合(Collection)中,一个集合相当于关系数据库中的表(Table)。MongoDB 的主要特点包括横向可扩展性、灵活的文档模型以及支持复杂的查询操作。

Mongoose 则是一个运行在Node.js环境下的对象数据模型(ODM)库,用于连接Node.js应用与MongoDB数据库。它的主要功能是提供了一套简洁的Schema定义接口、中间件处理逻辑和数据验证功能,使得开发者可以用类似于传统ORM框架的方式来处理MongoDB文档数据。Mongoose通过定义Schema来管理数据结构,并提供了一系列的方法和属性,使得在Node.js中操作MongoDB变得更加直观和方便。

举例来说,假设我们需要在一个博客系统中存储用户信息,使用MongoDB,我们直接操作数据库来插入、查询、更新或删除文档。而使用Mongoose,我们会先定义一个用户的Schema,指定字段及其类型,然后基于这个Schema创建一个模型(Model),通过这个模型来进行数据的CRUD操作。这样的好处是,我们的操作是类型安全的,并且可以很方便地进行数据验证和中间件处理。简而言之,Mongoose作为一个抽象层存在,为MongoDB的操作提供了更多的结构和简便性。

例如,使用Mongoose时,定义用户模型代码可能如下:

javascript
const mongoose = require('mongoose'); const { Schema } = mongoose; const userSchema = new Schema({ username: { type: String, required: true, unique: true }, password: { type: String, required: true }, email: { type: String, required: true, unique: true } }); const User = mongoose.model('User', userSchema); module.exports = User;

然后,我们可以用这个模型来创建新用户,如下:

javascript
const User = require('./path/to/user.model'); const newUser = new User({ username: 'johndoe', password: '123456', email: 'johndoe@example.com' }); newUser.save() .then(() => console.log('User created!')) .catch(error => console.error('Error creating user:', error));

在这个例子中,Mongoose会处理数据的验证,保证存入数据库的数据符合我们预先定义的Schema。而如果直接使用MongoDB,我们需要手动编写这些验证逻辑。

2024年6月29日 12:07 回复

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

就 Node.js 而言,mongodb是与 mongodb 实例交互的本机驱动程序, mongoose是 MongoDB 的对象建模工具。

mongoose构建在mongodb驱动程序之上,为程序员提供一种对其数据进行建模的方法。

编辑: 我不想评论哪个更好,因为这会使这个答案变得固执己见。不过,我将列出使用这两种方法的一些优点和缺点。

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

但是,如果您的集合模式不可预测,或者您希望在 Node.js 中获得类似 Mongo-shell 的体验,那么请继续使用该mongodb驱动程序。这是最简单的拾取方式。这样做的缺点是您必须编写大量代码来验证数据,并且出错的风险更高。

2024年6月29日 12:07 回复

我发现两者的另一个区别是,它相当容易使用,connect to multiple databasesmongodb native driver您必须使用mongoose仍然存在一些缺点的解决方法。

因此,如果您想要多租户应用程序,请选择 mongodb 本机驱动程序。

2024年6月29日 12:07 回复

从第一个答案来看,

“使用 Mongoose,用户可以为特定集合中的文档定义模式。它为 MongoDB 中的数据创建和管理提供了很多便利。”

您现在还可以使用 mongoDB 本机驱动程序定义架构

##新系列

shell
db.createCollection("recipes", validator: { $jsonSchema: { <<Validation Rules>> } } )

##对于现有集合

shell
db.runCommand({ collMod: "recipes", validator: { $jsonSchema: { <<Validation Rules>> } } })

##完整示例

shell
db.createCollection("recipes", { validator: { $jsonSchema: { bsonType: "object", required: ["name", "servings", "ingredients"], additionalProperties: false, properties: { _id: {}, name: { bsonType: "string", description: "'name' is required and is a string" }, servings: { bsonType: ["int", "double"], minimum: 0, description: "'servings' is required and must be an integer with a minimum of zero." }, cooking_method: { enum: [ "broil", "grill", "roast", "bake", "saute", "pan-fry", "deep-fry", "poach", "simmer", "boil", "steam", "braise", "stew" ], description: "'cooking_method' is optional but, if used, must be one of the listed options." }, ingredients: { bsonType: ["array"], minItems: 1, maxItems: 50, items: { bsonType: ["object"], required: ["quantity", "measure", "ingredient"], additionalProperties: false, description: "'ingredients' must contain the stated fields.", properties: { quantity: { bsonType: ["int", "double", "decimal"], description: "'quantity' is required and is of double or decimal type" }, measure: { enum: ["tsp", "Tbsp", "cup", "ounce", "pound", "each"], description: "'measure' is required and can only be one of the given enum values" }, ingredient: { bsonType: "string", description: "'ingredient' is required and is a string" }, format: { bsonType: "string", description: "'format' is an optional field of type string, e.g. chopped or diced" } } } } } } } });

插入集合示例

shell
db.recipes.insertOne({ name: "Chocolate Sponge Cake Filling", servings: 4, ingredients: [ { quantity: 7, measure: "ounce", ingredient: "bittersweet chocolate", format: "chopped" }, { quantity: 2, measure: "cup", ingredient: "heavy cream" } ] });
2024年6月29日 12:07 回复

Mongodb 和 Mongoose 是与 MongoDB 数据库交互的两个不同的驱动程序。

Mongoose:对象数据建模(ODM)库,为您的数据提供严格的建模环境。用于与 MongoDB 交互,通过提供管理数据的便利使生活变得更轻松。

Mongodb:Node.js 中用于与 MongoDB 交互的本机驱动程序。

2024年6月29日 12:07 回复

你的答案