Node.js中使用mongoose操作mongodb數(shù)據(jù)庫的方法
如何利用mongoose將數(shù)據(jù)寫入mongodb數(shù)據(jù)庫?
1.利用npm下載安裝mongoose;
npm install mongoose
2.建立js文件,引入mongoose
var mongoose = require('mongoose');
3.mongoose.connect連接數(shù)據(jù)庫
//連服務(wù)器
mongoose.connect('mongodb://127.0.0.1:27017/test'); //數(shù)據(jù)庫的名字
var connection = mongoose.connection;
connection.on('error', function (err) {
console.error(err);
});
connection.on('open', function () {
console.log('opened');
});//判斷是否連接上數(shù)據(jù)庫
4.schema規(guī)定數(shù)據(jù)的格式,model定義數(shù)據(jù)集合名稱,并引入schema的樣式
//建立 schema
var monsterSchema = mongoose.Schema({
name: {type: String},
age: {type: Number, default: 1}, //設(shè)置默認(rèn)值
gender: {type: Number, default: 1}, //約定:1.表示男妖怪,2表示女妖怪,
address: String,
skill: String //大招
});
//model 符合 schema
var monsterModel = mongoose.model('monster', monsterSchema);
5.使用entity和model兩種方式寫入數(shù)據(jù)
//需求: 1.使用 model 寫入數(shù)據(jù)庫
// 2.使用 entity 寫入數(shù)據(jù)庫
//定義黃眉怪
var yellow = {
name: 'yellow',
age: '2000', //設(shè)置默認(rèn)值
gender: '1', //約定:1.表示男妖怪,2表示女妖怪,
address: '小西天',
skill: 'bag' //大招
};
/*
//1.使用 model 寫入數(shù)據(jù)庫
monsterModel.create(yellow, function (err, data) {
if(err){
console.error(err);
}else {
console.log(data);
}
});
*/
//2.使用 entity 寫入數(shù)據(jù)庫
var entity = new monsterModel(yellow);
entity.save(function (err, doc) {
if(err){
console.error(err);
}else {
console.log(doc);
}
});
//引入
var mongoose = require('mongoose');
//連服務(wù)器
mongoose.connect('mongodb://127.0.0.1:27017/test'); //數(shù)據(jù)庫的名字
var connection = mongoose.connection;
connection.on('error', function (err) {
console.error(err);
});
connection.on('open', function () {
console.log('opened');
});//判斷是否連接上數(shù)據(jù)庫
//建立 schema
var monsterSchema = mongoose.Schema({
name: {type: String},
age: {type: Number, default: 1}, //設(shè)置默認(rèn)值
gender: {type: Number, default: 1}, //約定:1.表示男妖怪,2表示女妖怪,
address: String,
skill: String //大招
});
//model 符合 schema
var monsterModel = mongoose.model('monster', monsterSchema);
//需求: 1.使用 model 寫入數(shù)據(jù)庫
// 2.使用 entity 寫入數(shù)據(jù)庫
//定義黃眉怪
var yellow = {
name: 'yellow',
age: '2000', //設(shè)置默認(rèn)值
gender: '1', //約定:1.表示男妖怪,2表示女妖怪,
address: '小西天',
skill: 'bag' //大招
};
/*
//1.使用 model 寫入數(shù)據(jù)庫
monsterModel.create(yellow, function (err, data) {
if(err){
console.error(err);
}else {
console.log(data);
}
});
*/
//2.使用 entity 寫入數(shù)據(jù)庫
var entity = new monsterModel(yellow);
entity.save(function (err, doc) {
if(err){
console.error(err);
}else {
console.log(doc);
}
});
總結(jié)
以上所述是小編給大家介紹的Node.js中使用mongoose操作mongodb數(shù)據(jù)庫的方法,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- Node.js操作MongoDB數(shù)據(jù)庫實(shí)例分析
- node.js連接MongoDB數(shù)據(jù)庫的2種方法教程
- Node.js對(duì)MongoDB數(shù)據(jù)庫實(shí)現(xiàn)模糊查詢的方法
- Node.js連接MongoDB數(shù)據(jù)庫產(chǎn)生的問題
- node.js連接mongoDB數(shù)據(jù)庫 快速搭建自己的web服務(wù)
- 了不起的node.js讀書筆記之mongodb數(shù)據(jù)庫交互
- node.js操作mongoDB數(shù)據(jù)庫示例分享
- Node.js中Mongodb數(shù)據(jù)庫操作方法(最新推薦)
相關(guān)文章
express項(xiàng)目文件目錄說明以及功能描述詳解
這篇文章主要給大家介紹了關(guān)于express項(xiàng)目文件目錄說明以及功能描述的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
用C/C++來實(shí)現(xiàn) Node.js 的模塊(二)
上篇文章的主要內(nèi)容講訴了用C/C++來實(shí)現(xiàn) Node.js 的模塊,本文更深一步繼續(xù)探討這個(gè)問題,有需要的朋友可以參考下2014-09-09
node.JS md5加密中文與php結(jié)果不一致的解決方法
本篇文章主要介紹了node.JS md5加密中文與php結(jié)果不一致的解決方法,具有很好的參考價(jià)值。下面跟著小編一起來看下吧2017-05-05
使用nodejs?spider爬取圖片及數(shù)據(jù)實(shí)現(xiàn)
這篇文章主要為大家介紹了使用nodejs?spider爬取圖片及數(shù)據(jù)實(shí)現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07
nodejs中sleep功能實(shí)現(xiàn)暫停幾秒的方法
本篇文章主要介紹了nodejs中sleep功能實(shí)現(xiàn)暫停幾秒的方法,具有一定的參考價(jià)值,有興趣的可以了解一下2017-07-07
使用Node.js制作圖片上傳服務(wù)的詳細(xì)教程
在現(xiàn)代 Web 應(yīng)用開發(fā)中,圖片上傳是一項(xiàng)常見且重要的功能,借助 Node.js 強(qiáng)大的生態(tài)系統(tǒng),我們可以輕松搭建高效的圖片上傳服務(wù),本文將深入探討如何使用 Node.js 構(gòu)建一個(gè)功能完備的圖片上傳服務(wù),需要的朋友可以參考下2025-04-04
NodeJS創(chuàng)建基礎(chǔ)應(yīng)用并應(yīng)用模板引擎
這篇文章主要介紹了NodeJS創(chuàng)建基礎(chǔ)應(yīng)用并應(yīng)用模板引擎的相關(guān)資料,需要的朋友可以參考下2016-04-04

