mongodb的基本操作命令

來源:互聯網
上載者:User

標籤:mongodb的基本操作命令


=== mongodb的基本操作命令================= 


DDL操作===========================================================================================

建立架構:use  Schema ;  在Schema中建立一個對象即可 db.createCollection("CollectionName")。相當於 create database DBName ;

建立一個集合: db.createCollection("test"); 相當於DBMS中的建立表 create table TableName();

展示架構: show dbs;   相當於mysql 中的 show databases ;

展示集合個數: show  collections; show tables 

刪除當前架構:use Schema ;  db.dropDatabase();    drop  database DBName

修改集合名:db.CollectionName.renameCollection("NewName")   ;  類似 mysql 中的 alter table oldTable  rename  to  NewTable ;

刪除集合:db.collections.drop(); 類似 mysql 中 drop table tableName;

查看當前所在的DB:db


使用者相關操作================================================================

添加使用者:db.addUser("UserName","PWD")

驗證使用者是否添加成功:db.auth("UserName","PWD")

刪除使用者:db.removUsers("testUser")

許可權相關:

http://blog.itpub.net/22664653/viewspace-715617/


DML操作=====================================================================

查詢集合:db.CollectionName.find({}) ; 相當於 select * from TableName

等式條件查詢:db.CollectionName.find({key01:value01}); 相當於 select * from  TableName where key01=value01  

查詢欄位的不同值(去重):db.CollectionName.distinct("Key") 類似 select distinct(key) from  table

模糊查詢:db.collections.find({key:/ang/}); select * from  tableName where key like  ‘%ang%‘ ;

        :db.collections.find({key:/^ang/}) ; select * from  tableName where key like ‘ang%‘ ; 

        :db.collections.find({key:/g$/});  select * from  tableName where key like ‘%ang‘

查詢出指定的列明:db.collections.find({},{key01:false,key02:true,key03:true}); select key02,key03 from  tableName 

範圍尋找:db.collections.find({key01:{$gt:20}}); select * from  tableName where  key01 > 20

        :db.collections.find({key01:{$lge:20}}); select * from tableName where key01 >= 20

:db.collections.find({key01:{$lt:20}}); select * from  tableName where key01 < 20

:db.collections.find(){key01:{$lte:20}}; select * from tableName where key01 <= 20

升序排序:db.collections.find({}).sort({key:1}); select * from  tableName order  by  key asc

降序排序:db.collections.find({}).sort({key:-1}); select * from  tableName order by  key desc

記錄匯總:db.collections.find({}).count({}); select count(*) from tableName 

        :db.collections.find({}).count({key:false}) select count(key) from tableName

返回指定行數:DBQuery.shellBatchSize=num/db.collections.find({}).limit(num) 類似於 sqlserver set rowcount  num 

or的操作:db.collections.find({"$or":[{key01:valu01},{key02:value02}]}); select * from  tableName where key01=valu01 or  key02=value02

in操作:db.test.find({key01:{$in:[valu01,valu02,valu03]}});  select  * from  tableName where key01  in (valu01,valu02,valu03)

and的操作:db.collections.find({key01:{$gte:24,$lte:28}}); select * from  tableName where key01 >=24 and key01 <= 28 

:db.collections.find({key01:valu02,key02:valu02}); select * from tableName where key01=valu01 and  key02=valu02

不等於操作:db.collections.find({key01:{$ne:valu01}}); select * from  tableName where key01 <>  valu01  

$not的相關操作:db.collections.find({key:{$not:{$in:[valu01,valu02]}}}); select * from tableName  where key not in (valu01,valu02)

               注意:db.collections.find(key:{$not:valu01});  沒有這種寫法  

Regex:後續研究

數組的相關操作

$mod:模數函數

$inc

$maxscan:制定本次查詢中掃描文檔的最大數量

$min:查詢的開始條件,

$max:查詢的結束條件

$showDiskLoc:顯示該條結果在磁碟上的位置。

刪除資料:db.CollectionName.remove({key:value});

修改資料:db.collections.update({key01:valu01}},{"$set":{key02:NewValue}}); 

添加資料:db.CollectionName.Save({key01:value01,key02:value02})/db.CollectionName.insert({key01:value01,key02:value02})


查看協助: db.CollectionName.help();




本文出自 “SQLServer MySQL” 部落格,請務必保留此出處http://dwchaoyue.blog.51cto.com/2826417/1602588

mongodb的基本操作命令

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.