前面需要准备的内容可看前面的文章:
连接 mongoose数据库需要使用 node.js 的 mongoose驱动程序。在 express 应用程序中使用 mongoose驱动程序时,需要执行以下步骤
先创建一个js文档
db.js文档
安装 mongodb 驱动程序:
在你的项目目录下使用 npm 或 yarn 安装 mongoose驱动程序。
npm install mongoose
引入 mongodb 模块:
在 express 应用程序的文件中引入 mongodb 模块。
const mongodb = require('mongoose');
设置数据库连接:
创建一个 mongoose客户端,并通过客户端连接到 mongoose数据库。
const mongoose = require('mongoose'); // mongoose连接字符串,包括数据库地址和名称 mongoose.connect('mongodb://localhost:27017/mydatabase') .then(() => { console.log('connected to the database'); }) .catch((err) => { console.error('failed to connect to the database:', err); });
在上面的代码中,uri
变量包含了 mongoose数据库的连接字符串,其中包括数据库地址和名称。然后,创建一个新的 mongoose
实例,并通过 connect()
方法连接到数据库。在连接成功后,可以执行数据库操作,例如查询、插入、更新或删除文档。
新建一个表试试
const uservalue =new mongoose.schema({ name: string, age: number, email: string, password: string, phone: string, address: string, gender: string, dob: date, createdat: { type: date, default: date.now }, updatedat: { type: date, default: date.now } }); const user = mongoose.model('user', uservalue);
在上面的代码中,先声明一个表的格式。使用new mongoose
的schema
函数,内容为需要保存的字段。
再使用module.exports将表传出去:
const mongoose = require('mongoose'); // mongodb 连接字符串,包括数据库地址和名称 mongoose.connect('mongodb://localhost:27017/mydatabase') .then(() => { console.log('connected to the database'); }) .catch((err) => { console.error('failed to connect to the database:', err); }); const uservalue =new mongoose.schema({ name: string, age: number, email: string, password: string, phone: string, address: string, gender: string, dob: date, createdat: { type: date, default: date.now }, updatedat: { type: date, default: date.now } }); const user = mongoose.model('user', uservalue); module.exports = { user };
再使用index页面接收一下:
const { user }= require('./db');
执行数据库操作:
在连接成功后,可以在回调函数中执行数据库操作。
// 在连接成功后执行数据库操作 client.connect(err => { if (err) { console.error('failed to connect to the database:', err); return; } console.log('connected successfully to the database'); const db = client.db(); // 查询所有文档 db.collection('mycollection').find().toarray((err, docs) => { if (err) { console.error('error fetching documents:', err); return; } console.log('documents:', docs); }); // 插入文档 db.collection('mycollection').insertone({ name: 'john', age: 30 }, (err, result) => { if (err) { console.error('error inserting document:', err); return; } console.log('document inserted:', result.insertedid); }); // 更新文档 db.collection('mycollection').updateone({ name: 'john' }, { $set: { age: 35 } }, (err, result) => { if (err) { console.error('error updating document:', err); return; } console.log('document updated:', result.modifiedcount); }); // 删除文档 db.collection('mycollection').deleteone({ name: 'john' }, (err, result) => { if (err) { console.error('error deleting document:', err); return; } console.log('document deleted:', result.deletedcount); }); });
在上面的代码中,db.collection()
方法用于获取集合对象,然后可以使用该集合对象执行查询、插入、更新或删除操作。
关闭数据库连接:
在完成数据库操作后,记得关闭数据库连接,释放资源。
// 关闭数据库连接 client.close();
这样,你的 express 应用程序就可以连接到 mongoose数据库并执行数据库操作了。记得根据你的实际需求修改连接字符串和数据库操作。
到此这篇关于nodejs使用express连接数据库mongoose的示例的文章就介绍到这了,更多相关express连接mongoose内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论