MongoDB 和 Mongoose 的区别

Difference between MongoDB and Mongoose

我想使用 mongodb 数据库,但我注意到有两个不同的数据库有自己的网站和安装方法:mongodb 和 mongoose。所以我想问自己这个问题:"Which one do I use?".

因此,为了回答这个问题,我向社区询问您能否解释一下这两者之间的区别?如果可能的话,利弊?因为他们真的和我长得很像。

Mongodb和Mongoose是完全不同的两个东西!

Mongodb 是 数据库本身,而 Mongoose 是对象建模工具 Mongodb

编辑:正如所指出的 MongoDB 是 npm 包,谢谢!

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

的软件包

就 Node.js 而言,mongodb is the native driver for interacting with a mongodb instance and mongoose 对象建模工具 用于 MongoDB。

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

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

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

但是,如果您的收集模式不可预测,或者您想要 Mongo-shell 类似 Node.js 的体验,那么请继续使用 mongodb 驱动程序.这是最简单的选择。这里的缺点是您将不得不编写大量代码来验证数据,并且出错的风险更高。

Mongo 是 NoSQL 数据库。

如果您不想使用任何 ORM for your data models then you can also use native driver mongo.js: https://github.com/mongodb/node-mongodb-native

Mongoose 是 orm 之一,它为我们提供了使用易于理解的查询访问 mongo 数据的功能。

Mongoose 对您的数据库模型起到抽象的作用。

我发现两者的另一个区别是 connect to multiple databasesmongodb native driver 相当容易,而您必须使用 mongoose 中的变通方法,这仍然有一些缺点.

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

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

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

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

如果您打算将这些组件与您的专有代码一起使用,请参考以下信息。

Mongodb:

  1. 这是一个数据库。
  2. 此组件受 Affero 通用 Public 许可 (AGPL) 许可的约束。
  3. 如果您 link 此组件连同您的专有代码,那么您必须在 public 域中发布您的整个源代码,因为它具有像(GPL、LGPL 等)这样的病毒效应
  4. 如果您通过云托管您的应用程序,则 (2) 将适用,并且您还必须向最终用户发布您的安装信息。

猫鼬:

  1. 这是一个对象建模工具。
  2. 此组件受 MIT 许可证约束。
  3. 允许将此组件与专有代码一起使用,没有任何限制。
  4. 允许使用任何媒体或主机传送您的应用程序。

mongo-db 对于新开发者来说可能不是一个很好的选择。
另一方面,mongoose 作为 ORM(对象关系映射)对于新手来说可能是更好的选择。

Mongoose 建立在 mongodb 驱动之上,mongodb 驱动更底层。 Mongoose 提供了简单的抽象来轻松定义模式和查询。但在性能方面,Mongdb Driver 是最好的。

从第一个回答开始,

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

您现在还可以使用 mongoDB 本机驱动程序使用

定义架构

##对于新的collection

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

##对于现有的collection

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

##完整示例

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"
              }
            }
          }
        }
      }
    }
  }
});

插入collection示例

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" }
  ]
});

MongoDB 是官方 MongoDB Node.js 驱动程序允许 Node.js 应用程序连接到 MongoDB 并处理数据。 另一方面,Mongoose 是建立在 mongoDB 之上的其他库。它更容易理解和使用。如果你是初学者,猫鼬更适合你。