This time I will bring you how to operate the mongodb database in Node.js, and the precautions for operating the mongodb database in Node.js are Which ones, the following are practical cases, let’s take a look.
How to use mongoose to write data to mongodb database?
1. Use npm to download and install mongoose;
npm install mongoose
2. Create a js file and introduce mongoose
var mongoose = require('mongoose');
3.mongoose.connectConnect to database
//连服务器 mongoose.connect('mongodb://127.0.0.1:27017/test'); //数据库的名字 var connection = mongoose.connection; connection.on('error', function (err) { console.error(err); }); connection.on('open', function () { console.log('opened'); });//判断是否连接上数据库
4.Schema specifies the format of data, model defines data set and name, and introduces the schema style
//建立 schema var monsterSchema = mongoose.Schema({ name: {type: String}, age: {type: Number, default: 1}, //设置默认值 gender: {type: Number, default: 1}, //约定:1.表示男妖怪,2表示女妖怪, address: String, skill: String //大招 }); //model 符合 schema var monsterModel = mongoose.model('monster', monsterSchema);
5. Use entity and model to write data
//需求: 1.使用 model 写入数据库 // 2.使用 entity 写入数据库 //定义黄眉怪 var yellow = { name: 'yellow', age: '2000', //设置默认值 gender: '1', //约定:1.表示男妖怪,2表示女妖怪, address: '小西天', skill: 'bag' //大招 }; /* //1.使用 model 写入数据库 monsterModel.create(yellow, function (err, data) { if(err){ console.error(err); }else { console.log(data); } }); */ //2.使用 entity 写入数据库 var entity = new monsterModel(yellow); entity.save(function (err, doc) { if(err){ console.error(err); }else { console.log(doc); } }); //引入 var mongoose = require('mongoose'); //连服务器 mongoose.connect('mongodb://127.0.0.1:27017/test'); //数据库的名字 var connection = mongoose.connection; connection.on('error', function (err) { console.error(err); }); connection.on('open', function () { console.log('opened'); });//判断是否连接上数据库 //建立 schema var monsterSchema = mongoose.Schema({ name: {type: String}, age: {type: Number, default: 1}, //设置默认值 gender: {type: Number, default: 1}, //约定:1.表示男妖怪,2表示女妖怪, address: String, skill: String //大招 }); //model 符合 schema var monsterModel = mongoose.model('monster', monsterSchema); //需求: 1.使用 model 写入数据库 // 2.使用 entity 写入数据库 //定义黄眉怪 var yellow = { name: 'yellow', age: '2000', //设置默认值 gender: '1', //约定:1.表示男妖怪,2表示女妖怪, address: '小西天', skill: 'bag' //大招 }; /* //1.使用 model 写入数据库 monsterModel.create(yellow, function (err, data) { if(err){ console.error(err); }else { console.log(data); } }); */ //2.使用 entity 写入数据库 var entity = new monsterModel(yellow); entity.save(function (err, doc) { if(err){ console.error(err); }else { console.log(doc); } });
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the PHP Chinese website!
Recommended reading:
The above is the detailed content of How to operate mongodb database in Node.js. For more information, please follow other related articles on the PHP Chinese website!