天天看點

MongoDB應用學習

但是這個時候show dbs是看不到的,隻有加入了資料才算真正建立了。

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

相當于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")}) 

隻查找一個

增删改查分别對應

save

remove

update

find/findOne

建立索引有兩種方法:

db.system.indexes.insert(XXX)

db.system.ensureIndex(XXXX)

第二種更常用

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

<a href="http://blog.nosqlfan.com/html/3656.html">http://blog.nosqlfan.com/html/3656.html</a>

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

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

<a href="http://api.mongodb.org/js/2.3.2/index.html">http://api.mongodb.org/js/2.3.2/index.html</a>

&gt; db.pet.distinct("skill") 

[ "1", "2" ]

&gt; db.pet.find({},{'skill':1}) 

{ "_id" : ObjectId("513d4adbf596e5c47cf26c2b"), "skill" : "1" } 

{ "_id" : ObjectId("513d489ff596e5c47cf26c28"), "skill" : "2" }

這裡的_id是預設的Id,必須要顯示出來的

mysql中limit start,num

&gt; db.pet.find().skip(1).limit(1) 

{ "Id" : "1", "Name" : "cat1", "_id" : ObjectId("513d489ff596e5c47cf26c28"), "pic" : "noooo", "skill" : "2" }

db.test.find({'name':'foobar'})&lt;==&gt; select * from test where name='foobar'

db.test.find() &lt;==&gt; select *from test

db.test.find({'ID':10}).count()&lt;==&gt; select count(*) from test where ID=10

db.test.find().skip(10).limit(20)&lt;==&gt; select * from test limit 10,20

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

db.test.find().sort({'ID':-1}) &lt;==&gt; select * from test order by IDdesc

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

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

db.test.find('this.ID&lt;20',{name:1}) &lt;==&gt; select name from test whereID&lt;20

db.test.insert({'name':'foobar','age':25})&lt;==&gt;insertinto test ('name','age') values('foobar',25)

db.test.remove({}) &lt;==&gt; delete * from test

db.test.remove({'age':20}) &lt;==&gt; delete test where age=20

db.test.remove({'age':{$lt:20}}) &lt;==&gt; elete test where age&lt;20

db.test.remove({'age':{$lte:20}}) &lt;==&gt; delete test where age&lt;=20

db.test.remove({'age':{$gt:20}}) &lt;==&gt; delete test where age&gt;20

db.test.remove({'age':{$gte:20}})&lt;==&gt; delete test where age&gt;=20

db.test.remove({'age':{$ne:20}}) &lt;==&gt; delete test where age!=20

db.test.update({'name':'foobar'},{$set:{'age':36}})&lt;==&gt; update test set age=36 where name='foobar'

db.test.update({'name':'foobar'},{$inc:{'age':3}})&lt;==&gt; update test set age=age+3 where name='foobar'

文檔在這裡:

<a href="http://docs.mongodb.org/ecosystem/drivers/php/">http://docs.mongodb.org/ecosystem/drivers/php/</a>

php的mongo擴充的使用文檔在這裡:

<a href="http://blog.mongodb.org/post/24960636131/mongodb-for-the-php-mind-part-1">http://blog.mongodb.org/post/24960636131/mongodb-for-the-php-mind-part-1</a>

本文轉自軒脈刃部落格園部落格,原文連結:http://www.cnblogs.com/yjf512/archive/2013/03/18/2965919.html,如需轉載請自行聯系原作者