日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

mongoose的基本使用

發(fā)布時間:2025/7/14 编程问答 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mongoose的基本使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

開始之前,沒什么比過一遍官方文檔更有必要的了:http://mongoosejs.com/

mongoose 是啥?有啥用?
mongoose 是操作 MongoDB 的一個對象模型庫;它封裝了MongoDB對文檔操作的常用處理方法(增刪改查),讓 NodeJS 操作 Mongodb 數據庫變得快捷靈活。

本文所用到的完整代碼:源碼

安裝 mongoose

新建目錄 s4_mongoose 和 test.js 文件:

mkdir s4_mongoose cd s4_mongoose touch test.js

初始化目錄生成 package.json 并安裝 mongoose:

npm init cnpm install mongoose --save

連接數據庫

編輯 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('數據庫test連接失敗:' + error); }); db.connection.on('open', function(){console.log('數據庫test連接成功'); });

接著先打開一個 iTerm2 終端,開啟 mongodb 服務:

mongod

再打開另一個 iTerm2 終端,運行 test.js:

node test.js //成功后便會輸出:數據庫test連接成功

Schema/Model/Entity

沒有比文檔更詳細的了:http://mongoosejs.com/docs/guide.html

  • Schema:數據庫集合的結構對象。

  • Model :由Schema構造而成,可操作數據庫。

  • Entity:由Model創(chuàng)建的實體,可操作數據庫。

  • 看完文檔后,再看看下面一段代碼配合理解一下:

    var mongoose = require("mongoose"); var db = mongoose.connect("mongodb://127.0.0.1:27017/test"); // var testModel = db.model('test1', testSchema); // 集合名稱;集合的結構對象 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: "helloworld@qq.com" }); TestEntity.save(function(error,doc){if(error){console.log("error :" + error);}else{console.log(doc);} });

    model 數據插入

    在前面的數據庫連接成功的前提下,我們在數據庫 test 下新建一個集合 test1 、并往里面插入保存一組數據:

    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); // 集合名稱;集合的結構對象 // Document文檔(關聯數組式的對象) < Collection集合 < 數據庫 // 插入保存一段數據 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:"tttt@qq.com"},{name: "test7", age: 68, email:"ssss@qq.com"},{name: "test8", age: 18},{name: "test9", age: 18, email:"rrrr@qq.com"},{name: "test10",age: 18} ], function (error, docs) {if(error) {console.log(error);} else {console.log('save ok');console.log(docs);} });

    find 數據查詢

    mongoose 提供了find、findOne、和findById方法用于文檔查詢。
    基本語法:

    model.find(Conditions,fields,options,callback(err, doc));

    Conditions: 查詢條件
    fields: 返回的字段
    options: 游標(sort,limit)
    callback: 回調函數,參數doc為查詢出來的結果

    條件查詢的基礎:
    $lt (小于<)
    $lte (小于等于<=)
    $gt (大于>)
    $gte (大于等于>=)
    $ne (不等于,不包含!=)
    $in (包含)
    $or (查詢多個鍵值的任意給定值)
    $exists (判斷某些屬性是否存在)
    $all (全部)

    具體的一些實例,代碼里已有詳細注釋:

    // find(Conditions,fields,callback); // 省略或為空、返回所有記錄;只包含name,age字段,去掉默認的_id字段;執(zhí)行回調函數 testModel.find({}, {name:1, age:1, _id:0}, function(err, docs){if (err) {console.log('查詢出錯:' + err);} else {console.log('{}查詢結果為:');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('查詢出錯:' + err);} else {console.log('$gte,$lte查詢結果為:');console.log(docs);} }); // 查詢age為58、68的2條數據 testModel.find({age: {$in: [58, 68]}}, {name:1, age:1, _id:0}, function(err, docs){if (err) {console.log('查詢出錯:' + err);} else {console.log('$in查詢結果為:');console.log(docs);} }); // 查詢name為test3、或者age為18的全部數據 testModel.find({$or: [{name: 'test3'}, {age: 18}]}, {name:1, age:1, _id:0}, function(err, docs){if (err) {console.log('查詢出錯:' + err);} else {console.log('$or查詢結果為:');console.log(docs);} });// step3:游標查詢 // 查詢name為test3、或者age為18的全部數據;但限制只查詢2條數據 testModel.find({$or: [{name: 'test3'}, {age: 18}]}, {name:1, age:1, _id:0}, {limit: 2}, function(err, docs){if (err) {console.log('查詢出錯:' + err);} else {console.log('limit查詢結果為:');console.log(docs);} });

    update 數據更新

    基本使用: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('查詢出錯:' + err);} else {console.log('更新test1后的查詢結果為:');console.log(docs); // 更新test_update后的查詢結果為空數組:[ ];// 更新test1后的查詢結果為: [ { name: 'test1', age: 11 } ]// 只能更新本來已存在的數據}});} });

    remove 數據刪除

    基本使用: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('查詢出錯:' + err);} else {console.log('刪除test2后的查詢結果為:');console.log(docs); // 刪除test2后的查詢結果為空數組:[ ];}});} });

    robomongo mongodb可視化工具

    安裝 mongodb 可視化工具 robomongo
    在 iTerm2 開啟本地mongodb后(執(zhí)行mongod),打開 robomongo,新建 connection 即可連上本地的 mongodb 數據庫。

    總結

    以上是生活随笔為你收集整理的mongoose的基本使用的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。