mongoose基础详细步骤

1.mongoose连接数据库

1.1下载mongoose模块

1.1.1初始化package.json

输入npm init

 1.1.2下载模块npm install mongoose

 1.1.3向js文件中引入mongoose模块

const mongoose = require("mongoose")

 2.1.1连接数据库

mongoose.connect("mongodb://127.0.0.1:27017/haha").then(()=>{
   console.log("数据库连接成功")
}).catch((err)=>{
   console.log(err)
})

2.1.2创建集合规则

const Schema = mongoose.Schema;
const GoodsSchema = new Schema({
   title:String,
   price:Number,
   sales:String
});

2.1.3创建集合引入规则

mongoose.model('集合名称',集合规则);

const GoodsModel = mongoose.model('Goods',GoodsSchema);

2.1.4查询数据

GoodsModel.find().then((result)=>{
   console.log(result);
})

GoodsModel.find({ ptice:"999" }).then((result)=>{
   console.log(result)
})

GoodsModel.find().limit(1).then((result)=>{
   console.log(result)
})

GoodsModel.find().skip(1).limit(1).then((result)=>{
   console.log(result)
})

2.2.1给集合创建数据

方法1

let data = {
   title:"苹果手机",
   peice:9999,
   sales:9
}
const goods = new GoodsModel(data)
//保存数据
goods.save()

方法2

GoodsModel.creat({
   title:"红米手机",
   price:999,
   sales:99
}).then(()=>{
   console.log("成功") 
})

2.3.1删除数据

deleteOne删除一条

GoodsMondel.deleteOne({ title:"苹果手机" }).then((result)=>{
   console.log(result)
})

deleteMany删除多条数据

GoodsModel.deleteMany({ title: "红米手机" }).then((result) => {
    console.log(result);
})

2.3.2修改数据

updateOne修改一条数据

GoodsModel.updateOne({ price:3999 },{ $set: { price:7999 }}).then((result) => {
   console.log(result)
})

updateMany修改多条数据

GoodsModel.updateMany({ price: 6999 }, { $set: { price: 7999 } }).then((result) => {
    console.log(result);
})

3.1案例:数据渲染网页

sever.js页面  接收数据

// 引入http模块
let http = require("http");
let { GoodsModel } = require("./module/db.js")
// 创建一个服务器对象
let server = http.createServer((req, res) => {

    GoodsModel.find().then(result => {
        console.log(result);
        let htmlStr = `
        <!DOCTYPE html>
        <html lang="en">
        
        <head>
            <meta charset="UTF-8">
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
            <title>Document</title>
        </head>
        
        <body>
            <h2>好谷学堂</h2>
            <ul>
                <li>${result[0].title}--${result[0].price}</li>
                <li>${result[1].title}</li>
                <li>${result[2].title}</li>
            </ul>
        </body>
        
        </html>
        `
        res.write(htmlStr);
        res.end()

    })

})

// 监听端口号
server.listen(3000, () => {
    console.log("3000running");
})

db.js页面  导出数据

// 使用nodejs了解mongodb数据库
// 需要借助于第三方模块mongoose
// 打开www.npmjs.com,搜索mongoose模块,使用npm install mongoose下载
const mongoose = require("mongoose"); // 引入模块
// console.log(mongoose);

// 使用mongoose来连接数据库
mongoose.connect("mongodb://127.0.0.1:27017/haogu").then(() => {
    console.log("数据库连接成功");
}).catch((err) => {
    console.log(err);
})


// 创建集合的规则
const Schema = mongoose.Schema;
const GoodsSchema = new Schema({
    title: String,
    price: Number,
    sales: String
});



// 创建集合
// 根据集合规则创建集合,集合通常都是复数形式
// mongoose.model('集合名称', 集合规则);
const GoodsModel = mongoose.model('Goods', GoodsSchema);


// 导出
module.exports = {
    GoodsModel
}

在浏览器地址输入localhost:3000就可以看到数据了

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

飞不起来的飞机耶耶耶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值