使用moogose

简介

var mongoose = require(‘mongoose‘)
mongoose.connect(‘mongodb://123.206.112.122:27017/blog‘)

var db = mongoose.connection;
db.on(‘error‘, console.error.bind(console, ‘connection error:‘));
db.once(‘open‘, function() {
    console.log(‘hehe‘)
    var essaySchema = mongoose.Schema({
        id:Number,
        title:String,
        content:String
    })
    essaySchema.methods.log = function(){
        console.log(this.title)
    }
    var Essay = mongoose.model(‘Essay‘,essaySchema)

    var essay1 = new Essay({id:1,title:‘essay title‘,content:‘1‘})

    essay1.save(function(err,essay1){
        if(err) return console.error(err);
        essay1.log()
    })
});
时间: 2024-12-30 00:04:57

使用moogose的相关文章

node--20 moogose demo2

db.js /** * Created by Danny on 2015/9/28 16:44. */ //引包 var mongoose = require('mongoose'); //创建数据库连接,每一个用户都会创建一个db, var db = mongoose.createConnection('mongodb://127.0.0.1:27017/haha'); //监听open事件 db.once('open', function (callback) { console.log("

node--19 moogose demo1

db.js /** * Created by Danny on 2015/9/28 16:44. */ //引包 var mongoose = require('mongoose'); //创建数据库连接,每一个用户都会创建一个db, var db = mongoose.createConnection('mongodb://127.0.0.1:27017/haha'); //监听open事件 db.once('open', function (callback) { console.log("

使用mongodb

1.安装 yarn add moogose 2.启动 在命令行 mongod --dbpath D:\data//这是最后出现 waiting for connections on port 27017,不要关闭在打开一个命令行mongo 3.连接数据库 var mongoose = require('mongoose'); mongoose.connect('mongodb://localhost:27017/lagou', { useNewUrlParser: true }); var db