MongoDB基礎操作

來源:互聯網
上載者:User

標籤:操作   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
switched to db bosenru

> show tables;
system.indexes
ti

> db.t1.find()
{ "_id" : ObjectId("5602402d575c3e0a6920d326"), "x" : 1 }
是它唯一的標實
ObjectId("5602402d575c3e0a6920d326")


> db.t1.insert({x:2})
WriteResult({ "nInserted" : 1 })
> db.t1.find()
{ "_id" : ObjectId("5602402d575c3e0a6920d326"), "x" : 1 }
{ "_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});
WriteResult({ "nInserted" : 1 })


WriteResult({ "nInserted" : 1 })

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

> db.t1.findOne()
{ "_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})
WriteResult({ "nInserted" : 1 })

> db.t1.find()
{ "_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("56024852d5f225b12c0b67ab"), "x" : 1 }
{ "_id" : ObjectId("56024852d5f225b12c0b67ac"), "x" : 2 }
{ "_id" : ObjectId("56024852d5f225b12c0b67ad"), "x" : 3 }
{ "_id" : ObjectId("56024852d5f225b12c0b67ae"), "x" : 4 }
{ "_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})
{ "_id" : 2, "x" : 1 }

!查看(x:?)的資料

> db.t1.find({‘x‘:2})
{ "_id" : ObjectId("56024852d5f225b12c0b67ac"), "x" : 2 }
> db.t1.find({‘x‘:4})
{ "_id" : ObjectId("56024852d5f225b12c0b67ae"), "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})
{ "_id" : ObjectId("56024852d5f225b12c0b67ab"), "x" : 1 }
{ "_id" : 2, "x" : 1 }

再查看一下(x:999)的資料
> db.t1.find({‘x‘:999})
{ "_id" : ObjectId("560247edd5f225b12c0b67aa"), "x" : 999 }

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

!!多欄位更新時,只需更新部分欄位

> db.t1.insert({x:100,y:100,z:100})
WriteResult({ "nInserted" : 1 })

> db.t1.find()
{ "_id" : ObjectId("56024f44d5f225b12c0b680e"), "x" : 100, "y" : 100, "z" : 100 }

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

db.t1.update({z:100},{y:99})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

查看一下:

> db.t1.find()
{ "_id" : ObjectId("56024f44d5f225b12c0b680e"), "y" : 99 }

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

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

可以找到(y:99)的值
> db.t1.find({‘y‘:99})
{ "_id" : ObjectId("56024f44d5f225b12c0b680e"), "y" : 99 }

*這種方法是錯的

!!正確的方法:

重新插入這個條資料

> db.t1.insert({x:100,y:100,z:100})
WriteResult({ "nInserted" : 1 })


> db.t1.update({z:100},{$set:{y:99}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })


查看一下:

> db.t1.find()
{ "_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)
WriteResult({
     "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})
WriteResult({ "nInserted" : 1 })
> db.t1.insert({c:1})
WriteResult({ "nInserted" : 1 })
> db.t1.insert({c:1})
WriteResult({ "nInserted" : 1 })

查看一下:

> db.t1.find()

{ "_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 })

查看一下:

> db.t1.find()

{ "_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.find()
>
沒有資料了

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

!!如何刪除集合:

> db.t1.drop()
true

查看一下:

> show tables;
system.indexes

沒有t1這個文檔了

本文出自 “DBSpace” 部落格,請務必保留此出處http://dbspace.blog.51cto.com/6873717/1869916

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.