Home > Web Front-end > JS Tutorial > How to operate mongodb database in Node.js

How to operate mongodb database in Node.js

php中世界最好的语言
Release: 2018-04-18 09:40:20
Original
1628 people have browsed it

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
Copy after login

2. Create a js file and introduce mongoose

var mongoose = require('mongoose');
Copy after login

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');
});//判断是否连接上数据库
Copy after login

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);
Copy after login

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);
  }
});
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template