MongoDB應用學習


創建數據庫可以直接使用use test

但是這個時候show dbs是看不到的,只有加入了數據才算真正創建了。

 

collection對應mysql中表的概念

collection中每條數據在生成的時候會自行生成_id的字段

 

db.pet.find()

相當於select * from pet

> db.pet.find()
{ "_id" : ObjectId("513d489ff596e5c47cf26c28"), "Id" : "1", "Name" : "cat1", "skill" : "2" }

db.pet.find(<json>)

代表select * from pet where XXX

> db.pet.find({_id : ObjectId("513d489ff596e5c47cf26c28")})
{ "_id" : ObjectId("513d489ff596e5c47cf26c28"), "Id" : "1", "Name" : "cat1", "skill" : "2" }

db.pet.findOne({'Id': '2'})

只查找一個

 

增刪改查分別對應

save

remove

update

find/findOne

 

db.currentOp來獲取當前操作線程的相關信息

創建索引有兩種方法:

db.system.indexes.insert(XXX)

db.system.ensureIndex(XXXX)

第二種更常用

索引和mysql的索引一樣,是按照B+樹來排序的,所以需要設置B+樹的順序,排列順序為從小到大設置為1,排列順序為從大到小設置為-1

 

mongo的索引分為三種:

唯一索引

松散索引(null行不加入索引)

多值索引(可以對一個array進行索引)

http://blog.nosqlfan.com/html/3656.html

 

索引在后台創建

db.values.ensureIndex({open: 1, close: 1}, {background: true})

 

mongodb中各個結構是有提供各自的api的,具體需要參考:

http://api.mongodb.org/js/2.3.2/index.html

 

查詢一個collection中某個字段有哪些值:

> db.pet.distinct("skill")
[ "1", "2" ]

只顯示一些字段:

> db.pet.find({},{'skill':1})
{ "_id" : ObjectId("513d4adbf596e5c47cf26c2b"), "skill" : "1" }
{ "_id" : ObjectId("513d489ff596e5c47cf26c28"), "skill" : "2" }

這里的_id是默認的Id,必須要顯示出來的

mysql中limit start,num

> db.pet.find().skip(1).limit(1)
{ "Id" : "1", "Name" : "cat1", "_id" : ObjectId("513d489ff596e5c47cf26c28"), "pic" : "noooo", "skill" : "2" }

 

MongoDB語法 MySql語法

db.test.find({'name':'foobar'})<==> select * from test where name='foobar'

db.test.find() <==> select *from test

db.test.find({'ID':10}).count()<==> select count(*) from test where ID=10

db.test.find().skip(10).limit(20)<==> select * from test limit 10,20

db.test.find({'ID':{$in:[25,35,45]}})<==> select * from test where ID in (25,35,45)

db.test.find().sort({'ID':-1}) <==> select * from test order by IDdesc

db.test.distinct('name',{'ID':{$lt:20}}) <==> select distinct(name) from testwhere ID<20

db.test.group({key:{'name':true},cond:{'name':'foo'},reduce:function(obj,prev){prev.msum+=obj.marks;},initial:{msum:0}}) <==> select name,sum(marks) from testgroup by name

db.test.find('this.ID<20',{name:1}) <==> select name from test whereID<20

db.test.insert({'name':'foobar','age':25})<==>insertinto test ('name','age') values('foobar',25)

db.test.remove({}) <==> delete * from test

db.test.remove({'age':20}) <==> delete test where age=20

db.test.remove({'age':{$lt:20}}) <==> elete test where age<20

db.test.remove({'age':{$lte:20}}) <==> delete test where age<=20

db.test.remove({'age':{$gt:20}}) <==> delete test where age>20

db.test.remove({'age':{$gte:20}})<==> delete test where age>=20

db.test.remove({'age':{$ne:20}}) <==> delete test where age!=20

db.test.update({'name':'foobar'},{$set:{'age':36}})<==> update test set age=36 where name='foobar'

db.test.update({'name':'foobar'},{$inc:{'age':3}})<==> update test set age=age+3 where name='foobar'

from:http://blog.csdn.net/shellching/article/details/7651979

 

php安裝mongo需要使用擴展

文檔在這里:

http://docs.mongodb.org/ecosystem/drivers/php/

php的mongo擴展的使用文檔在這里:

http://blog.mongodb.org/post/24960636131/mongodb-for-the-php-mind-part-1


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM