但是这个时候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")})
{ "_id" : ObjectId("513d489ff596e5c47cf26c28"), "Id" : "1", "Name" : "cat1", "skill" : "2" }
只查找一个
增删改查分别对应
save
remove
update
find/findOne
创建索引有两种方法:
db.system.indexes.insert(XXX)
db.system.ensureIndex(XXXX)
第二种更常用
索引和mysql的索引一样,是按照B+树来排序的,所以需要设置B+树的顺序,排列顺序为从小到大设置为1,排列顺序为从大到小设置为-1
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
> 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" }
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
文档在这里:
http://docs.mongodb.org/ecosystem/drivers/php/
php的mongo扩展的使用文档在这里:
http://blog.mongodb.org/post/24960636131/mongodb-for-the-php-mind-part-1
本文基于署名-非商业性使用 3.0许可协议发布,欢迎转载,演绎,但是必须保留本文的署名叶剑峰(包含链接http://www.cnblogs.com/yjf512/),且不得用于商业目的。如您有任何疑问或者授权方面的协商,请与我联系。