MongoDB Shell簡單操作

1.連接數據庫:數據庫客戶端shell操作,使用bin目錄中的mongo文件,在mongo DB的安裝中說是一個腳本,是不正確的,它是一個可執行的二進制文件:
連接入數據庫如下:
[root@localhost bin]# ./mongo
MongoDB shell version: 2.2.2
connecting to: test
>
>
>
有點類似於mysql中的客戶端bin目錄中的mysql可執行文件
2.基本操作:
在步驟1中連接上數據庫服務後,默認是連接到test這個數據庫的,可以從步驟1的執行過程中看到。
  1)使用help幫助命令:

> help
  db.help()                    help on db methods
  db.mycoll.help()             help on collection methods
  sh.help()                    sharding helpers
  rs.help()                    replica set helpers
  help admin                   administrative help
  help connect                 connecting to a db help
  help keys                    key shortcuts
  help misc                    misc things to know
  help mr                      mapreduce

  show dbs                     show database names
  show collections             show collections in current database
  show users                   show users in current database
  show profile                 show most recent system.profile entries with time >= 1ms
  show logs                    show the accessible logger names
  show log [name]              prints out the last segment of log in memory, 'global' is default
  use <db_name>                set current database
  db.foo.find()                list objects in collection foo
  db.foo.find( { a : 1 } )     list objects in foo where a == 1
  it                           result of the last line evaluated; use to further iterate
  DBQuery.shellBatchSize = x   set default number of items to display on shell
  exit                         quit the mongo shell
>
可以看出可以在mongoDB中可以進行的一些操作。
2).數據庫的查看:使用show dbs命令
> show dbs
local   (empty)
test    0.0625GB
>
其中test數據庫是在安裝第一次接入mongodb的命令行時,使用show dbs命令,是不會看到test數據庫的。
3).查看數據庫中的集合(在mongodb中,集合就相當於關係型數據庫中的表,在後面的文章中將具體說這個):
> show collections
c1
c2
system.indexes
>
看到以上結果,是因爲在當前數據庫中已經創建了c1和c2兩個集合,其中還有一個創建集合的時候所生成的索引文件。
4).數據庫數據查看(後面會具體寫到數據庫數據查詢操作):
如查看test數據庫中c1集合中的數據:

> db.c1.find();
{ "_id" : ObjectId("4fc14404703fa637a073651a"), "name" : "user2", "age" : 15 }
{ "_id" : ObjectId("4fc145e3703fa637a073651b"), "name" : "user3", "age" : 16 }
{ "_id" : ObjectId("4fc147a3703fa637a073651c"), "name" : "user4", "age" : 17 }
{ "_id" : ObjectId("4fc1c121b9137657a0d19cbd"), "name" : "user5", "age" : 22 }
{ "_id" : ObjectId("4fc1c121b9137657a0d19cbe"), "name" : "user6", "age" : 23 }
 >
在mongodb的集合中存放的數據格式是json格式的數據,以key-value的形式,在mongodb中叫做bson數據格式。
5).添加數據:
如往c3集合中添加數據:> db.c3.insert({name:"kolle",age:14});添加數據的格式也是json格式的。
查看數據:
> db.c3.find();
{ "_id" : ObjectId("4fc3c93ceb59c2fe573dba71"), "name" : "kolle", "age" : 14 }
--------------------------------------------------------------------------------------------
以上是mongodb的簡單操作包括:shell的使用,數據庫的查看,集合查看,數據的簡單插入和查詢,後面對於各個方面講詳細講解

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章