摘要:開始之前,沒什么比過一遍官方文檔更有必要的了是啥有啥用是操作的一個(gè)對象模型庫它封裝了對文檔操作的常用處理方法增刪改查,讓操作數(shù)據(jù)庫變得快捷靈活。由創(chuàng)建的實(shí)體,可操作數(shù)據(jù)庫。
開始之前,沒什么比過一遍官方文檔更有必要的了:http://mongoosejs.com/
mongoose 是啥?有啥用?
mongoose 是操作 MongoDB 的一個(gè)對象模型庫;它封裝了MongoDB對文檔操作的常用處理方法(增刪改查),讓 NodeJS 操作 Mongodb 數(shù)據(jù)庫變得快捷靈活。
本文所用到的完整代碼:源碼
安裝 mongoose新建目錄 s4_mongoose 和 test.js 文件:
mkdir s4_mongoose cd s4_mongoose touch test.js
初始化目錄生成 package.json 并安裝 mongoose:
npm init cnpm install mongoose --save連接數(shù)據(jù)庫
編輯 test.js :
var mongoose = require("mongoose"); var db = mongoose.connect("mongodb://127.0.0.1:27017/test"); db.connection.on("error", function(error){ console.log("數(shù)據(jù)庫test連接失?。? + error); }); db.connection.on("open", function(){ console.log("數(shù)據(jù)庫test連接成功"); });
接著先打開一個(gè) iTerm2 終端,開啟 mongodb 服務(wù):
mongod
再打開另一個(gè) iTerm2 終端,運(yùn)行 test.js:
node test.js //成功后便會(huì)輸出:數(shù)據(jù)庫test連接成功Schema/Model/Entity
沒有比文檔更詳細(xì)的了:http://mongoosejs.com/docs/guide.html
Schema:數(shù)據(jù)庫集合的結(jié)構(gòu)對象。
Model :由Schema構(gòu)造而成,可操作數(shù)據(jù)庫。
Entity:由Model創(chuàng)建的實(shí)體,可操作數(shù)據(jù)庫。
看完文檔后,再看看下面一段代碼配合理解一下:
var mongoose = require("mongoose"); var db = mongoose.connect("mongodb://127.0.0.1:27017/test"); // var testModel = db.model("test1", testSchema); // 集合名稱;集合的結(jié)構(gòu)對象 var TestSchema = new mongoose.Schema({ name : { type:String }, age : { type:Number, default:0 }, email: { type:String }, time : { type:Date, default:Date.now } }); var TestModel = db.model("test1", TestSchema ); var TestEntity = new TestModel({ name : "helloworld", age : 28, email: "[email protected]" }); TestEntity.save(function(error,doc){ if(error){ console.log("error :" + error); }else{ console.log(doc); } });model 數(shù)據(jù)插入
在前面的數(shù)據(jù)庫連接成功的前提下,我們在數(shù)據(jù)庫 test 下新建一個(gè)集合 test1 、并往里面插入保存一組數(shù)據(jù):
var testSchema = new mongoose.Schema({ name: {type: String}, age: {type: Number, default: 0}, email: {type: String}, time: {type: Date, default: Date.now} }); var testModel = db.model("test1", testSchema); // 集合名稱;集合的結(jié)構(gòu)對象 // Document文檔(關(guān)聯(lián)數(shù)組式的對象) < Collection集合 < 數(shù)據(jù)庫 // 插入保存一段數(shù)據(jù) testModel.create([ {name: "test1", age: 8}, {name: "test2", age: 18}, {name: "test3", age: 28}, {name: "test4", age: 38}, {name: "test5", age: 48}, {name: "test6", age: 58, email:"[email protected]"}, {name: "test7", age: 68, email:"[email protected]"}, {name: "test8", age: 18}, {name: "test9", age: 18, email:"[email protected]"}, {name: "test10",age: 18} ], function (error, docs) { if(error) { console.log(error); } else { console.log("save ok"); console.log(docs); } });find 數(shù)據(jù)查詢
mongoose 提供了find、findOne、和findById方法用于文檔查詢。
基本語法:
model.find(Conditions,fields,options,callback(err, doc));
Conditions: 查詢條件
fields: 返回的字段
options: 游標(biāo)(sort,limit)
callback: 回調(diào)函數(shù),參數(shù)doc為查詢出來的結(jié)果
條件查詢的基礎(chǔ):
$lt (小于<)
$lte (小于等于<=)
$gt (大于>)
$gte (大于等于>=)
$ne (不等于,不包含!=)
$in (包含)
$or (查詢多個(gè)鍵值的任意給定值)
$exists (判斷某些屬性是否存在)
$all (全部)
具體的一些實(shí)例,代碼里已有詳細(xì)注釋:
// find(Conditions,fields,callback); // 省略或?yàn)榭?、返回所有記錄;只包含name,age字段,去掉默認(rèn)的_id字段;執(zhí)行回調(diào)函數(shù) testModel.find({}, {name:1, age:1, _id:0}, function(err, docs){ if (err) { console.log("查詢出錯(cuò):" + err); } else { console.log("{}查詢結(jié)果為:"); console.log(docs); } }); // 查詢age大于等于28,小于等于48 testModel.find({age: {$gte: 28, $lte: 48}}, {name:1, age:1, _id:0}, function(err, docs){ if (err) { console.log("查詢出錯(cuò):" + err); } else { console.log("$gte,$lte查詢結(jié)果為:"); console.log(docs); } }); // 查詢age為58、68的2條數(shù)據(jù) testModel.find({age: {$in: [58, 68]}}, {name:1, age:1, _id:0}, function(err, docs){ if (err) { console.log("查詢出錯(cuò):" + err); } else { console.log("$in查詢結(jié)果為:"); console.log(docs); } }); // 查詢name為test3、或者age為18的全部數(shù)據(jù) testModel.find({$or: [{name: "test3"}, {age: 18}]}, {name:1, age:1, _id:0}, function(err, docs){ if (err) { console.log("查詢出錯(cuò):" + err); } else { console.log("$or查詢結(jié)果為:"); console.log(docs); } }); // step3:游標(biāo)查詢 // 查詢name為test3、或者age為18的全部數(shù)據(jù);但限制只查詢2條數(shù)據(jù) testModel.find({$or: [{name: "test3"}, {age: 18}]}, {name:1, age:1, _id:0}, {limit: 2}, function(err, docs){ if (err) { console.log("查詢出錯(cuò):" + err); } else { console.log("limit查詢結(jié)果為:"); console.log(docs); } });update 數(shù)據(jù)更新
基本使用:model.update(查詢條件,更新對象,callback);
var conditions = {name: "test1"}; var update = {$set: {age: 11 }}; testModel.update(conditions, update, function(error){ if(error) { console.log(error); } else { console.log("Update success!"); testModel.find({name: "test1"}, {name:1, age:1, _id:0}, function(err, docs){ if (err) { console.log("查詢出錯(cuò):" + err); } else { console.log("更新test1后的查詢結(jié)果為:"); console.log(docs); // 更新test_update后的查詢結(jié)果為空數(shù)組:[ ]; // 更新test1后的查詢結(jié)果為: [ { name: "test1", age: 11 } ] // 只能更新本來已存在的數(shù)據(jù) } }); } });remove 數(shù)據(jù)刪除
基本使用:model.remove(查詢條件,callback);
var conditions = {name: "test2"}; testModel.remove(conditions, function(error){ if(error) { console.log(error); } else { console.log("Delete success!"); testModel.find({name: "test2"}, {name:1, age:1, _id:0}, function(err, docs){ if (err) { console.log("查詢出錯(cuò):" + err); } else { console.log("刪除test2后的查詢結(jié)果為:"); console.log(docs); // 刪除test2后的查詢結(jié)果為空數(shù)組:[ ]; } }); } });robomongo mongodb可視化工具
安裝 mongodb 可視化工具 robomongo
在 iTerm2 開啟本地mongodb后(執(zhí)行mongod),打開 robomongo,新建 connection 即可連上本地的 mongodb 數(shù)據(jù)庫。
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/19044.html
摘要:安裝然后,我們需要將引入我們的項(xiàng)目中,使用連接我們在本地運(yùn)行實(shí)例名為數(shù)據(jù)庫。在連接到本地的數(shù)據(jù)庫,我們需要知道連接的是否成功在中,全部來源于那么,到目前為止,我們創(chuàng)建了一個(gè)只有一個(gè)屬性值為類型的的。 起步 首先先確定MongoDB和Node.js已經(jīng)安裝。安裝Mongoose: npm install mongoose 然后,我們需要將mongoose引入我們的項(xiàng)目中,使用mongoo...
摘要:文檔是的核心概念,是鍵值對的一個(gè)有序集,在里文檔被表示成對象。創(chuàng)建集合數(shù)據(jù)庫中的集合名稱當(dāng)我們對其添加數(shù)據(jù)時(shí)如果已經(jīng)存在,則會(huì)保存到其目錄下,如果未存在,則會(huì)創(chuàng)建集合,然后在保存數(shù)據(jù)。使用創(chuàng)建,如下示例連接成功許巍男保存成功保存失敗參考 mongoose簡介 mongoose網(wǎng)站:https://mongoosejs.com/ 為什么要用Mongoose Mongoose就是一個(gè)讓我們...
摘要:如圖連接成功后,顯示你的數(shù)據(jù)庫,在這個(gè)節(jié)目可以對數(shù)據(jù)庫進(jìn)行操作。如圖安裝與加載首先假定你已經(jīng)安裝了,命令行工具輸入在使用的文件中即可。創(chuàng)建讀取更新刪除單值讀取上文是在中基于對進(jìn)行增刪查改操作的簡單介紹,以后會(huì)有進(jìn)階的文章。 關(guān)鍵詞:mongodb安裝 mongoose使用 robomongo mongoose的CRUD操作 mongoose的查詢,增加,修改,刪除 工具介紹 Mon...
摘要:如圖連接成功后,顯示你的數(shù)據(jù)庫,在這個(gè)節(jié)目可以對數(shù)據(jù)庫進(jìn)行操作。如圖安裝與加載首先假定你已經(jīng)安裝了,命令行工具輸入在使用的文件中即可。創(chuàng)建讀取更新刪除單值讀取上文是在中基于對進(jìn)行增刪查改操作的簡單介紹,以后會(huì)有進(jìn)階的文章。 關(guān)鍵詞:mongodb安裝 mongoose使用 robomongo mongoose的CRUD操作 mongoose的查詢,增加,修改,刪除 工具介紹 Mon...
摘要:前言要做一個(gè)全沾的工程師,對于后端和數(shù)據(jù)庫來說,即使不認(rèn)識也要見個(gè)面的。基本了解的概念就好,主要是安裝上數(shù)據(jù)庫,并進(jìn)行簡單的增刪操作。 前言:要做一個(gè)全沾的工程師,對于后端和數(shù)據(jù)庫來說,即使不認(rèn)識也要見個(gè)面的。本文給的例子很簡單,也貼出來源碼,只要一步步下來,就可以跑起來啦~~~ 思考一個(gè)需求:做一個(gè)登錄頁面,自己搭建服務(wù)和數(shù)據(jù)庫,將用戶輸入的登錄信息保存到數(shù)據(jù)庫如何完成呢:首先選擇...
閱讀 2215·2021-11-25 09:43
閱讀 1181·2021-11-23 09:51
閱讀 3514·2021-11-23 09:51
閱讀 3643·2021-11-22 09:34
閱讀 1576·2021-10-09 09:43
閱讀 2138·2019-08-30 15:53
閱讀 3173·2019-08-30 14:07
閱讀 582·2019-08-28 18:14