欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

MongoDB基礎(chǔ)操作

MongoDB:

慶云網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián)公司,慶云網(wǎng)站設(shè)計制作,有大型網(wǎng)站制作公司豐富經(jīng)驗。已為慶云成百上千提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\成都外貿(mào)網(wǎng)站建設(shè)公司要多少錢,請找那個售后服務(wù)好的慶云做網(wǎng)站的公司定做!

有集合和文檔概念
集合==表
文檔==數(shù)據(jù)庫表里的行

!!建立數(shù)據(jù)庫(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 }

!!只查詢一個數(shù)據(jù):
> db.t1.findOne()
{ "_id" : ObjectId("56023de2575c3e0a6920d324"), "x" : 1 }

(One的首字母要大寫)

!!刪掉bosenrui這個庫:

> db.dropDatabase()
{ "dropped" : "bosenrui", "ok" : 1 }

(Database的首字母要大寫)

!!使用集合(表),文檔(行記錄)
插入多條數(shù)據(jù):
mongodb可以使用json語法插入多條數(shù)據(jù)

_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的值,讓系統(tǒng)給分配)


> 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 }"


!!插入多條數(shù)據(jù):


> 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行記錄,根據(jù)(x)這個字段,(1)為順序值(正序),(-1)(倒序)

查看(_id:2)的數(shù)據(jù)

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

!查看(x:?)的數(shù)據(jù)

> db.t1.find({'x':2})
{ "_id" : ObjectId("56024852d5f225b12c0b67ac"), "x" : 2 }
> db.t1.find({'x':4})
{ "_id" : ObjectId("56024852d5f225b12c0b67ae"), "x" : 4 }

!!把(x:1)的數(shù)據(jù)更新成(x:999)

> db.t1.update({x:1},{x:999})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

再查看一下(x:1)的數(shù)據(jù)
> db.t1.find({'x':1})
{ "_id" : ObjectId("56024852d5f225b12c0b67ab"), "x" : 1 }
{ "_id" : 2, "x" : 1 }

再查看一下(x:999)的數(shù)據(jù)
> db.t1.find({'x':999})
{ "_id" : ObjectId("560247edd5f225b12c0b67aa"), "x" : 999 }

*如果集合中需要跟新的值有重復(fù)的那么只更新第一個

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

> 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 }

*這種方法是錯的

!!正確的方法:

重新插入這個條數(shù)據(jù)

> 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為部分更新操作符,更新存在的字段,不存在的字段會保持原樣。


!!更新集合中不存在數(shù)據(jù):

> db.t1.find({y:100})
>
沒有這條數(shù)據(jù)

把(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)

插入三條數(shù)據(jù):

> 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()
>
沒有數(shù)據(jù)了

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

!!如何刪除集合:

> db.t1.drop()
true

查看一下:

> show tables;
system.indexes

沒有t1這個文檔了

文章標題:MongoDB基礎(chǔ)操作
文章起源:http://www.chinadenli.net/article12/gpcddc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站建設(shè)定制開發(fā)微信公眾號做網(wǎng)站網(wǎng)站排名網(wǎng)頁設(shè)計公司

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

綿陽服務(wù)器托管