前端学习(1334):mongodb增2
生活随笔
收集整理的這篇文章主要介紹了
前端学习(1334):mongodb增2
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/playground', { useUnifiedTopology: true }).then(() => console.log('數據庫連接成功')).catch(err => console.log(err, '數據庫連接失敗'))//創建集合規則
const courseSchema = new mongoose.Schema({name: String,author: String,isPublished: Boolean
});
//使用集合并應用規則
const Course = mongoose.model('Course', courseSchema);//創建實例
const course = new Course({name: 'node.js',author: 'geyao',isPublished: true
});
course.save();//向結合中插入文檔
/* Course.create({name:'javascript',author:'hema',isPublished:false},(err,result)=>{console.log(err)console.log(result)
}) */
Course.create({ name: 'javascript123', author: 'hema', isPublished: false }).then(result => {console.log(result)})
運行結果
總結
以上是生活随笔為你收集整理的前端学习(1334):mongodb增2的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 前端学习(1377):express路由
- 下一篇: 2017年html5行业报告,云适配发布