Mongoose is a MongoDB object modeling tool designed to work in an asynchronous environment.
For Project
$ npm install Then install node.js and mongodb.
$ npm install mongoose// Using Node.js `require()`
const mongoose = require('mongoose');
// Using ES6 imports
import mongoose from 'mongoose';First, we need to define a connection. If your app uses only one database, you should use mongoose.connect. If you need to create additional connections, use mongoose.createConnection.
Both connect and createConnection take a mongodb:// URI, .
var mongoose = require('mongoose');
mongoose.connect('mongodb://127.0.0.1:27017/myapp');or the parameters host, database, port, options
var mongoose = require('mongoose');
mongoose.connect('mongodb://username:password@host:port/database?options...');Once connected, the open event is fired on the Connection instance. If you're using mongoose.connect, the Connection is mongoose.connection. Otherwise, mongoose.createConnection return value is a Connection.
Important! Mongoose buffers all the commands until it's connected to the database. This means that you don't have to wait until it connects to MongoDB in order to define models, run queries, etc.
Models are defined through the Schema interface.
const bookSchema = new mongoose.Schema({
name: {
type: String,
required: true,
unique: true,
},
author: {
type: String,
required: true,
unique: true,
},
publishedYear: Number,
tags: [String],
date: {
type: Date,
default: Date.now,
},
onSale: Boolean,
price: Number,
publisher: {
type: mongoose.Schema.Types.ObjectId,
ref: "Publisher",
},
});Aside from defining the structure of your documents and the types of data you're storing, a Schema handles the definition of:
- Validators (async and sync)
- Defaults
- Indexes
- Middleware
- Methods definition
- Statics definition
The following example shows some of these features:
const bookSchema = new mongoose.Schema({
name: {
type: String,
required: true,
unique: true,
},
author: {
type: String,
required: true,
unique: true,
},
publishedYear: Number,
tags: [String],
date: {
type: Date,
default: Date.now,
},
onSale: Boolean,
price: Number,
publisher: {
type: mongoose.Schema.Types.ObjectId,
ref: "Publisher",
},
});
// middleware
bookSchema.pre("save", function (next) {
if (/[\u0080-\uffff]/.test(this.name)) {
this.name = this.name.replace(/[^\x00-\x7F]+/g, "");
}
next();
});Take a look at the example in models/book.ts for an end-to-end example of a typical setup.
Once we define a model through mongoose.model("Book", bookSchema);, we can access it through the same function
const Book = mongoose.model("Book", bookSchema);The first argument is the singular name of the collection your model is for. Mongoose automatically looks for the plural version of your model name. For example, if you use
const Publisher = mongoose.model("Publisher", publisherSchema);Then Mongoose will create the model for your publishers collection, not your publisher collection.
we can find documents from the same collection
const result = await Publisher.find({ name='test' }).sort({ name: 1 }).exec();You can also findOne, findById, update, etc. For more details check out the docs.
Important! If you opened a separate connection using mongoose.createConnection() but attempt to access the model through mongoose.model('ModelName') it will not work as expected since it is not hooked up to an active db connection. In this case access your model through the connection you created:
const conn = mongoose.createConnection(process.env.MONGODB_URI);
MyModel = conn.model('ModelName', schema),
m = new MyModel;
m.save(); // worksvs
const conn = mongoose.createConnection(process.env.MONGODB_URI);
MyModel = mongoose.model('ModelName', schema),
m = new MyModel;
m.save(); // does not work b/c the default connection object was never connectedFor saving
const saveBook = async (req: Request, res: Response) => {
try {
if (!req.body) {
return res
.status(400)
.json({ error: "Request body is missing or empty" });
}
const book = new Book(req.body);
await book.save();
const publisher = await Publisher.findById({ _id: book.publisher });
publisher?.publishedBooks.push(book.id);
await publisher?.save();
res.status(201).json({ data: book });
} catch (error) {
console.error(error);
res.status(500).json({ error: "Internal server error" });
}
};For Updating:
const updateBook = async (req: Request, res: Response) => {
try {
const book = await Book.findById(req.params.id);
if (!book) res.status(401).json({ error: "book not found!" });
else {
const { price, tags } = req.body;
book.price = price;
book.tags = tags;
const result = await book.save();
res.status(201).json(result);
}
} catch (error) {
console.error(error);
res.status(500).json({ error: "Internal server error" });
}
};For removing:
const deleteBook = async (req: Request, res: Response) => {
try {
const { id } = req.params;
const book = await Book.findByIdAndDelete(id);
res.status(201).json(book);
} catch (error) {
console.error(error);
res.status(500).json({ error: "Internal server error" });
}
};See the docs page.
You can intercept method arguments via middleware.
For example, you can define a pre-save middleware function to handle Unicode characters in the name before storing it in the database:
bookSchema.pre("save", function (next) {
if (/[\u0080-\uffff]/.test(this.name)) {
this.name = this.name.replace(/[^\x00-\x7F]+/g, "");
}
next();
});