创建数据库可以直接使用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(
代表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