0812_作業(yè)Mongodb學習使用手冊

認識mongodb

mongodb簡介

MongoDB 是一個基于分布式文件存儲的數據庫。由C++語言編寫。旨在為WEB應用提供可擴展的高性能數據存儲解決方案。

MongoDB 是一個介于關系數據庫和非關系數據庫之間的產品,是非關系數據庫當中功能最豐富,最像關系數據庫的。他支持的數據結構非常松散,是類似json的bson格式,因此可以存儲比較復雜的數據類型。Mongo最大的特點是他支持的查詢語言非常強大,其語法有點類似于面向對象的查詢語言,幾乎可以實現類似關系數據庫單表查詢的絕大部分功能,而且還支持對數據建立索引。

mongodb特點

它的特點是高性能、易部署、易使用,存儲數據非常方便。主要功能特性有:

  • 面向集合存儲,易存儲對象類型的數據。
  • 模式自由。
  • 支持動態(tài)查詢。
  • 支持完全索引,包含內部對象。
  • 支持查詢。
  • 支持復制和故障恢復。
  • 使用高效的二進制數據存儲,包括大型對象(如視頻等)。
  • 自動處理碎片,以支持云計算層次的擴展性。
  • 支持RUBY,PYTHON,JAVA,C++,PHP,C#等多種語言。
  • 文件存儲格式為BSON(一種JSON的擴展)。
  • 可通過網絡訪問。

mongodb安裝(Ubuntu32位機)

mongodb安裝總結

mongodb操作

啟動mongodb

run 直接啟動:
例如:mongod run

--dbpath 指定存儲目錄啟動:
例如:./mongod --dbpath=../lilei_db

--port 指定端口啟動:(默認端口是:27017)
例如:mongod --port 12345。

關閉mongodb

在窗口模式中,可以直接使用Ctrl+C停止服務。

mongodb基本操作

語句 含義
show dbs 查詢所有數據庫列表
db 查看當前連接在哪個數據庫下面
use test 切換到test數據庫下面
show collections 查看test下有哪些表或者叫collection
help 查看mongodb支持哪些命令
db.help(); 查看當前數據庫支持哪些方法
db.user.help();(user為表名) 查看當前數據庫下的表或者表collection支持哪些方法
db.addUser(username,password) 添加用戶
db.auth(usrename,password) 設置數據庫連接驗證
db.cloneDataBase(fromhost) 從目標服務器克隆一個數據庫
db.commandHelp(name) returns the help for the command
db.copyDatabase(fromdb,todb,fromhost) 復制數據庫fromdb---源數據庫名稱,todb---目標數據庫名稱,fromhost---源數據庫服務器地址
db.createCollection(name,{size:3333,capped:333,max:88888}) 創(chuàng)建一個數據集,相當于一個表
db.currentOp() 取消當前庫的當前操作
db.dropDataBase() 刪除當前數據
db.eval(func,args) run code server-side
db.getCollection(cname) 取得一個數據集合,同用法:db['cname'] or
db.getCollenctionNames() 取得所有數據集合的名稱列表
db.getLastErrorObj() 返回最后一個錯誤的對象
db.getMongo() 取得當前服務器的連接對象get the server
db.getMondo().setSlaveOk() allow this connection to read from then nonmaster membr of a replica pair
db.getName() 返回當前操作數據庫的名稱
db.getPrevError() 返回上一個錯誤對象
db.getReplicationInfo() 獲得重復的數據
db.getSisterDB(name) get the db at the same server as this onew
db.killOp() 停止(殺死)在當前庫的當前操作
db.printCollectionStats() 返回當前庫的數據集狀態(tài)
db.printReplicationInfo()
db.printSlaveReplicationInfo()
db.printShardingStatus() 返回當前數據庫是否為共享數據庫
db.removeUser(username) 刪除用戶
db.repairDatabase() 修復當前數據庫
db.resetError()
db.runCommand(cmdObj) run a database command. if cmdObj is a string, turns it into {cmdObj:1}
db.setProfilingLevel(level) 0=off,1=slow,2=all
db.shutdownServer() 關閉當前服務程序
db.version() 返回當前程序的版本信息

mongodb增刪改查語句

db.user.innert(
    {
      "name":"liwei",
      "tel":18843436650
    }
  );
db.user.innert(
  {
    "name":"liwei",
    "tel":18843436650,
    "yz":"100"
  }
);

db.user.innertMany(
  [
    {
      "name":"longdage",
      "sex":1,
      "jushu":"good"
    },
    {
      "name":"gaoluofeng",
      "zhiwu":"1ge",
      "jushu":"yiliu"
    }
  ]
);

db.test.remove({})                 <==> delete * from test

db.test.remove({'age':20})         <==> delete test where age=20

db.test.remove({'age':{$lt:20}})   <==> elete test where age<20

db.test.remove({'age':{$lte:20}})  <==> delete test where age<=20

db.test.remove({'age':{$gt:20}})  <==> delete test where age>20

db.test.remove({'age':{$gte:20}}) <==> delete test where age>=20

db.test.remove({'age':{$ne:20}})  <==> delete test where age!=20

test.update({'name':'foobar'},{$set:{'age':36}}) <==> update test set age=36 where name='foobar'

db.test.update({'name':'foobar'},{$inc:{'age':3}}) <==> update test set age=age+3 where name='foobar'

語句 含義
db.user.find()[.toArray()或.prety()];
db.foo.find(...).count()
db.foo.find(...).limit(n) 根據條件查找數據并返回指定記錄數
db.foo.find(...).skip(n)
db.foo.find(...).sort(...) 查找排序
db.foo.findOne([query]) 根據條件查詢只查詢一條數據

高級查詢

1.這六個就不用解釋了,最常用的,也是最簡單的。
// 大于  : field > value
db.collection.find({ "field" : { $gt: value } } )   

// 小于  :  field < value
db.collection.find({ "field" : { $lt: value } } )   

// 大于等于 : field >= value
db.collection.find({ "field" : { $gte: value } } )  

// 小于等于 : field <= value
db.collection.find({ "field" : { $lte: value } } )  

// 不等于 : field != value
db.things.find( { "field" : { $ne : value } } )

// 條件相當于a % 10 == 1 即a除以10余數為1的。
db.things.find( { a : { $mod : [ 10 , 1 ] } } )

2.如果要同時滿足多個條件
// value1 < field < value
db.collection.find({ "field" : { $gt: value1, $lt: value2 } } )

3.不屬于,屬于,全部屬于
// 條件相當于 j 不等于 [2,4,6] 中的任何一個。
db.things.find({j:{$nin: [2,4,6]}})

// 條件相當于j等于[2,4,6]中的任何一個。
db.things.find({j:{$in: [2,4,6]}})

// 與$in類似,但必須是[]的值全部都存在。
db.things.find( { a: { $all: [ 2, 3 ] } } )
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容