Reputation: 2900
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 came up asking myself this question: "Which one do I use?".
So in order to answer this question I ask the community if you could explain what are the differences between these two? And if possible pros and cons? Because they really look very similar to me.
Upvotes: 269
Views: 191562
Reputation: 1
MongoDB is a type of database that stores data in the form of documents. Mongoose is a library for MongoDB.
Mongoose providing a higher-level abstraction, schema definition, validation, middleware, and query building capabilities.
Upvotes: 0
Reputation: 1
Mongoose: Mongoose is a Node js-based Object Data Modelling (ODM) library for MongoDB. Mongoose aims to allow developers to enforce a specific schema at the application layer. Mongoose also offers a variety of hooks, model validation, and other features aimed at making it easier to work with MongoDB. We have to define a schema object (defines the structure of the JSON document) for the documents to store them in the collections. We have to create a Model which is used to interact with the collection. Whenever we do an operation on the collection, the schema is validated at the application layer itself. The mongoose is the judge for the schema validation and the database is unaware of these rules. We can add the validation in 2 ways:
MongoDB: It is a Node JS driver for the MongoDB. It is very flexible when compared to the Mongoose library. There is no need to define the schema for the collection, which in turn gives flexibility in validating the schema. But even here we can add the schema validation rules for the database using the compass or the shell. We can add the rules in the validation tab of the collection in the compass. We can have the flexibility of inserting the documents even if the validation is failed. We can make the settings accordingly to warn and add the document in case the schema is invalid. We can send properties like bypassDocumentValidation if we want to skip the validation step.
It is our own choice to choose between these two based on our requirements.
You can refer to the documentation here: https://www.mongodb.com/developer/languages/javascript/mongoose-versus-nodejs-driver/
Upvotes: 0
Reputation: 1376
Validations:
One big difference between mongodb
and mongoose
is that mongoose handles validations within its software framework. This implies that when you access the database directly, you can insert whatever you want as document.
In mongodb you can use JSON Schema validation, which is a little bit more complex than the mongoose validations, but the validation happens on DB level
Performance:
For large-scale, performance-intensive projects, it is advisable to utilize MongoDB's native driver. Based on web benchmarks, mongodb
exhibits performance metrics approximately twice as fast as mongoose
.
Upvotes: 3
Reputation: 454
MongoDB is The official MongoDB Node.js driver allows Node.js applications to connect to MongoDB and work with data. On the other side Mongoose it other library build on top of mongoDB. It is more easier to understand and use. If you are a beginner than mongoose is better for you to work with.
Upvotes: 0
Reputation: 151
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" }
]
});
Upvotes: 15
Reputation: 20284
I assume you already know that MongoDB is a NoSQL database system which stores data in the form of BSON documents. Your question, however is about the packages for Node.js.
In terms of Node.js, mongodb is the native driver for interacting with a mongodb instance and mongoose is an Object modeling tool for MongoDB.
mongoose
is built on top of the mongodb
driver to provide programmers with a way to model their data.
EDIT: I do not want to comment on which is better, as this would make this answer opinionated. However I will list some advantages and disadvantages of using both approaches.
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. On the downside, learning mongoose can take some time, and has some limitations in handling schemas that are quite complex.
However, if your collection schema is unpredictable, or you want a Mongo-shell like experience inside Node.js, then go ahead and use the mongodb
driver. It is the simplest to pick up. The downside here is that you will have to write larger amounts of code for validating the data, and the risk of errors is higher.
Upvotes: 390
Reputation: 305
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.
Upvotes: 7
Reputation: 115
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.
Upvotes: 9
Reputation: 595
If you are planning to use these components along with your proprietary code then please refer below information.
Mongodb:
Mongoose:
Upvotes: 6
Reputation: 1825
Mongodb and Mongoose are two different drivers to interact with MongoDB database.
Mongoose : object data modeling (ODM) library that provides a rigorous modeling environment for your data. Used to interact with MongoDB, it makes life easier by providing convenience in managing data.
Mongodb: native driver in Node.js to interact with MongoDB.
Upvotes: 14
Reputation: 1016
Mongo is NoSQL Database.
If you don't want to use any ORM for your data models then you can also use native driver mongo.js: https://github.com/mongodb/node-mongodb-native.
Mongoose is one of the orm's who give us functionality to access the mongo data with easily understandable queries.
Mongoose plays as a role of abstraction over your database model.
Upvotes: 61
Reputation: 3358
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!
Upvotes: -2
Reputation: 47146
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.
Upvotes: 24