天天看點

MongoDB基礎操作

mongoDB:

有集合和文檔概念

集合==表

文檔==資料庫表裡的行

!!建立資料庫(bosenru)

> use bosenru;

switched to db bosenru

!!往bosenrui裡插入一張表(t1),表裡插入(x:1)這個字段(x為字段,1為它的值)

> db.ti.insert({x:1});

WriteResult({ "nInserted" : 1 })

檢視一下

>show tadabases;

admin     (empty)

bosenrui  0.078GB

local     0.078GB

> use bosenru

> show tables;

system.indexes

ti

> db.t1.find()

{ "_id" : ObjectId("5602402d575c3e0a6920d326"), "x" : 1 }

是它唯一的标實

ObjectId("5602402d575c3e0a6920d326")

> db.t1.insert({x:2})

{ "_id" : ObjectId("560240ea575c3e0a6920d327"), "x" : 2 }

!!隻查詢一個資料:

> db.t1.findOne()

{ "_id" : ObjectId("56023de2575c3e0a6920d324"), "x" : 1 }

(One的首字母要大寫)

!!删掉bosenrui這個庫:

> db.dropDatabase()

{ "dropped" : "bosenrui", "ok" : 1 }

(Database的首字母要大寫)

!!使用集合(表),文檔(行記錄)

插入多條資料:

mongodb可以使用json文法插入多條資料

_id:全局唯一值

> use bosenrui

switched to db bosenrui

指定(_id)為2

> db.t1.insert({x:1,_id:2});

{ "_id" : 2, "x" : 1 }

再插入db.t1.insert({x:1,_id:2}),會有報錯,是主鍵沖突。(不要去指定_id的值,讓系統給配置設定)

> db.t1.insert({x:1,_id:2})

WriteResult({

     "nInserted" : 0,

     "writeError" : {

          "code" : 11000,

          "errmsg" : "insertDocument :: caused by :: 11000 E11000 duplicate key error index: bosenru.t1.$_id_  dup key: { : 2.0 }"

!!插入多條資料:

> for(i=1;i<100;i++)db.t1.insert({x:i})

{ "_id" : ObjectId("560247edd5f225b12c0b67aa"), "x" : 1 }

{ "_id" : ObjectId("56024852d5f225b12c0b67ab"), "x" : 1 }

{ "_id" : ObjectId("56024852d5f225b12c0b67ac"), "x" : 2 }

{ "_id" : ObjectId("56024852d5f225b12c0b67ad"), "x" : 3 }

{ "_id" : ObjectId("56024852d5f225b12c0b67ae"), "x" : 4 }

Type "it" for more(可以用it進行翻頁)

> db.t1.find().count()

100

總共有100條記錄

> db.t1.find().skip(1).limit(5).sort({x:1})

{ "_id" : ObjectId("56024852d5f225b12c0b67af"), "x" : 5 }

> db.t1.find().skip(1).limit(5).sort({x:-1})

{ "_id" : ObjectId("56024852d5f225b12c0b680c"), "x" : 98 }

{ "_id" : ObjectId("56024852d5f225b12c0b680b"), "x" : 97 }

{ "_id" : ObjectId("56024852d5f225b12c0b680a"), "x" : 96 }

{ "_id" : ObjectId("56024852d5f225b12c0b6809"), "x" : 95 }

{ "_id" : ObjectId("56024852d5f225b12c0b6808"), "x" : 94 }

!!跳過1行,檢視5行記錄,根據(x)這個字段,(1)為順序值(正序),(-1)(倒序)

檢視(_id:2)的資料

> db.t1.find({'_id':2})

!檢視(x:?)的資料

> db.t1.find({'x':2})

> db.t1.find({'x':4})

!!把(x:1)的資料更新成(x:999)

> db.t1.update({x:1},{x:999})

WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

再檢視一下(x:1)的資料

> db.t1.find({'x':1})

再檢視一下(x:999)的資料

> db.t1.find({'x':999})

{ "_id" : ObjectId("560247edd5f225b12c0b67aa"), "x" : 999 }

*如果集合中需要跟新的值有重複的那麼隻更新第一個

!!多字段更新時,隻需更新部分字段

> db.t1.insert({x:100,y:100,z:100})

{ "_id" : ObjectId("56024f44d5f225b12c0b680e"), "x" : 100, "y" : 100, "z" : 100 }

!!更新z為100時,y為99

db.t1.update({z:100},{y:99})

檢視一下:

{ "_id" : ObjectId("56024f44d5f225b12c0b680e"), "y" : 99 }

這時(z,x)的值找不到

> db.t1.find({'z':100})

> db.t1.find({'x':100})

可以找到(y:99)的值

> db.t1.find({'y':99})

*這種方法是錯的

!!正确的方法:

重新插入這個條資料

> db.t1.update({z:100},{$set:{y:99}})

{ "_id" : ObjectId("56025482d5f225b12c0b6810"), "x" : 100, "y" : 99, "z" : 100 }

db.t1.update({z:100},{$set:{y:99}}),set為部分更新操作符,更新存在的字段,不存在的字段會保持原樣。

!!更新集合中不存在資料:

> db.t1.find({y:100})

>

沒有這條資料

把(y:100)更新成為(y:99)

> db.t1.update({y:100},{y:999},true)

     "nMatched" : 0,

     "nUpserted" : 1,

     "nModified" : 0,

     "_id" : ObjectId("5602576b03e4970d981cb3bf")

> db.t1.find({y:999})

{ "_id" : ObjectId("5602576b03e4970d981cb3bf"), "y" : 999 }

!!如何批量更新:

将(c:1)改為(c:2)

插入三條資料:

> db.t1.insert({c:1})

{ "_id" : ObjectId("56025d10d5f225b12c0b6814"), "c" : 1 }

{ "_id" : ObjectId("56025d28d5f225b12c0b6815"), "c" : 1 }

{ "_id" : ObjectId("56025d29d5f225b12c0b6816"), "c" : 1 }

進行批量更新:

> db.t1.update({c:1},{$set:{c:2}},false,true)

WriteResult({ "nMatched" : 3, "nUpserted" : 0, "nModified" : 3 })

{ "_id" : ObjectId("56025d10d5f225b12c0b6814"), "c" : 2 }

{ "_id" : ObjectId("56025d28d5f225b12c0b6815"), "c" : 2 }

{ "_id" : ObjectId("56025d29d5f225b12c0b6816"), "c" : 2 }

!!如何删除一個值:

> db.t1.remove({c:2})

WriteResult({ "nRemoved" : 3 })

沒有資料了

這個方法可以删除集合下的文檔

!!如何删除集合:

> db.t1.drop()

true

沒有t1這個文檔了

本文轉自 DBAspace 51CTO部落格,原文連結:http://blog.51cto.com/dbaspace/1869916