Difference between MongoDB and Mongoose

后端 未结 9 2371
一个人的身影
一个人的身影 2020-12-02 04:52

I wanted to use the mongodb database, but I noticed that there are two different databases with either their own website and installation methods: mongodb and mongoose. So I

相关标签:
9条回答
  • 2020-12-02 05:12

    One more difference I found with respect to both is that it is fairly easy to connect to multiple databases with mongodb native driver while you have to use work arounds in mongoose which still have some drawbacks.

    So if you wanna go for a multitenant application, go for mongodb native driver.

    0 讨论(0)
  • 2020-12-02 05:12

    If you are planning to use these components along with your proprietary code then please refer below information.

    Mongodb:

    1. It's a database.
    2. This component is governed by the Affero General Public License (AGPL) license.
    3. If you link this component along with your proprietary code then you have to release your entire source code in the public domain, because of it's viral effect like (GPL, LGPL etc)
    4. If you are hosting your application over the cloud, the (2) will apply and also you have to release your installation information to the end users.

    Mongoose:

    1. It's an object modeling tool.
    2. This component is governed by the MIT license.
    3. Allowed to use this component along with the proprietary code, without any restrictions.
    4. Shipping your application using any media or host is allowed.
    0 讨论(0)
  • 2020-12-02 05:13

    From the first answer,

    "Using Mongoose, a user can define the schema for the documents in a particular collection. It provides a lot of convenience in the creation and management of data in MongoDB."

    You can now also define schema with mongoDB native driver using

    ##For new collection

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

    ##For existing collection

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

    ##full example

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

    Insert collection Example

    `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" }
      ]
    });`
    
    0 讨论(0)
  • 2020-12-02 05:15

    Mongodb and Mongoose are two completely different things!

    Mongodb is the database itself, while Mongoose is an object modeling tool for Mongodb

    EDIT: As pointed out MongoDB is the npm package, thanks!

    0 讨论(0)
  • 2020-12-02 05:16

    mongo-db is likely not a great choice for new developers.
    On the other hand mongoose as an ORM (Object Relational Mapping) can be a better choice for the new-bies.

    0 讨论(0)
  • 2020-12-02 05:16

    Mongoose is built untop of mongodb driver, the mongodb driver is more low level. Mongoose provides that easy abstraction to easily define a schema and query. But on the perfomance side Mongdb Driver is best.

    0 讨论(0)
提交回复
热议问题