Rav*_*lav 114 mongoose mongodb node.js
我想使用mongodb数据库,但我注意到有两个不同的数据库,有自己的网站和安装方法:mongodb和mongoose.所以我想问自己这个问题:"我使用哪一个?".
所以为了回答这个问题,我问社区你是否可以解释这两者之间有什么区别?如果可能的利弊?因为他们看起来和我很相似.
ZeM*_*oon 183
我假设您已经知道MongoDB是一个NoSQL数据库系统,它以BSON文档的形式存储数据.然而,你的问题是关于Node.js的软件包.
就Node.js而言,mongodb是与mongodb实例交互的本机驱动程序,而mongoose是 MongoDB 的Object建模工具.
Mongoose基于MongoDB驱动程序,为程序员提供了一种对数据建模的方法.
编辑: 我不想评论哪个更好,因为这会使这个答案自以为是.但是,我将列出使用这两种方法的一些优点和缺点.
使用Mongoose,用户可以为特定集合中的文档定义模式.它为MongoDB中的数据创建和管理提供了很多便利.在缺点方面,学习mongoose可能需要一些时间,并且在处理相当复杂的模式方面存在一些限制.
但是,如果您的集合架构不可预测,或者您希望在Node.js中有类似Mongo-shell的体验,那么请继续使用MongoDB驱动程序.这是最简单的选择.这里的缺点是你必须编写更大量的代码来验证数据,并且错误的风险更高.
Anu*_*ngh 38
Mongo是NoSQL数据库.
如果您不想对数据模型使用任何ORM,那么您也可以使用本机驱动程序mongo.js:https://github.com/mongodb/node-mongodb-native.
Mongoose是为我们提供使用易于理解的查询访问mongo数据的功能的orm之一.
Mongoose在数据库模型中扮演抽象角色.
Rah*_*hul 11
还有一个区别,我发现这两方面是,它是相当容易connect to multiple databases使用mongodb native driver,而必须使用变通,在mongoose其中还是有一些缺点.
因此,如果您想要使用多租户应用程序,请转到mongodb本机驱动程序.
小智 8
mongo-db对于新开发人员来说,这可能不是一个很好的选择。
另一方面,mongoose作为 ORM(对象关系映射)对于新手来说可能是更好的选择。
Mongodb 和 Mongoose 是与 MongoDB 数据库交互的两个不同的驱动程序。
Mongoose:对象数据建模 (ODM) 库,可为您的数据提供严格的建模环境。用于与 MongoDB 交互,通过提供管理数据的便利性使生活更轻松。
Mongodb:Node.js 中的本地驱动程序,用于与 MongoDB 交互。
如果您计划将这些组件与您的专有代码一起使用,请参阅以下信息。
MongoDB:
猫鼬:
小智 6
Mongoose 是在 mongodb 驱动程序之上构建的,mongodb 驱动程序更加底层。Mongoose 提供了简单的抽象来轻松定义模式和查询。但在性能方面,Mongdb Driver 是最好的。
小智 5
从第一个回答开始,
“使用 Mongoose,用户可以为特定集合中的文档定义模式。它为 MongoDB 中数据的创建和管理提供了很多便利。”
您现在还可以使用 mongoDB 本机驱动程序定义架构
##对于新集合
`db.createCollection("recipes",
validator: { $jsonSchema: {
<<Validation Rules>>
}
}
)`
Run Code Online (Sandbox Code Playgroud)
##对于现有集合
`db.runCommand( {
collMod: "recipes",
validator: { $jsonSchema: {
<<Validation Rules>>
}
}
} )`
Run Code Online (Sandbox Code Playgroud)
##完整示例
`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"
}
}
}
}
}
}
}
});`
Run Code Online (Sandbox Code Playgroud)
插入集合示例
`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" }
]
});`
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
69919 次 |
| 最近记录: |