MongoDB入门需知(For PHPer)

来源:互联网 发布:安卓写轮眼优化教程 编辑:程序博客网 时间:2024/05/20 06:42

MongoDB入门需知(For PHPer

***************************************************************************

*官网:http://www.mongodb.org

* APIhttp://www.php.net/manual/en/book.mongo.php

* Tutorialhttp://php.net/manual/en/mongo.tutorial.php

*下载:http://downloads.mongodb.org/win32/mongodb-win32-i386-1.8.1.zip

*驱动For phphttps://github.com/mongodb/mongo-php-driver/downloads

 

内容简介:

windows XP环境下MongoDB的安装、MongoDB基本语法、MongoDB GridFS基本操作、PHPmongodb的基本操作

***************************************************************************

一、安装

下载后将压缩包解压到一个目录

创建数据库文件的默认存放位置mgdatadb目录。比如:e:\mgdata\db

 

好了,安装就这么简单~~~

 

二、启动/关闭服务

2.1启动服务

方法一:新建一个bat批处理文件,比如”start mongo service.bat”

里面写上如下语句:

@echo off

mongod --bind_ip 127.0.0.1 --logpath e:\mgdata\logs\dblog.txt --logappend --dbpath e:\mgdata\db

 

--bind_ip 127.0.0.1               这个是指定IP的,还可以指定端口,这个可以自己深入了解

--logpath e:\mgdata\logs\dblog.txt –logappend             这个是指定日志文件以及日志写入方式

--dbpath e:\mgdata\db           这个是指定数据库文件存放地址的

 

通常直接使用mongod --logappend --dbpath e:\mgdata\db就可以直接启动服务了

 

方法二:命令行模式

===使用命令行模式,最好先配置好环境变量

例如mogonDB解压到d:/mongodb

可以配置环境变量:path=d:\mongodb\bin

这样可以直接在命令行模式使用mongomongodb等命令

 

启动服务的命令:

> mongod --logappend --dbpath e:\mgdata\db

 

方法三:将mongodb服务安装成系统服务

这个就自己上网找下吧-_-!

 

2.2关闭服务

方法一:服务端命令行模式下,快捷键Ctrl+C,特别简单吧~

 

方法二:客户端mongo shell模式下,db.shutdownserver()

 

==这里需要注意的一点是,如果mongdb服务没有正常关闭,在e:\mgdata\db下面会产生一个mongod.lock,可能下次不能正常启动,这时候,只要把这个lock文件删掉就好了

 

2.3启动客户端

新打开一个cmd窗口

> mongo

回车,如果出现如下信息,就表示启动成功

MongoDB shell version: 1.8.1

connecting to: test

 

然后可以输入help命令,查看相关的命令,后面列出mongodb数据库的数据库语法

 

三、基本语法

Mongo语法

db.AddUser(username,password)  添加用户

db.auth(usrename,password)     设置数据库连接验证

db.cloneDataBase(fromhost)     从目标服务器克隆一个数据库

db.commandHelp(name)           returns the help for the command

db.copyDatabase(fromdb,todb,fromhost)  复制数据库fromdb---源数据库名称,todb---目标数据库名称,fromhost---源数据库服务器地址

db.createCollection(name,{size:3333,capped:333,max:88888})  创建一个数据集,相当于一个表

db.currentOp()                 取消当前库的当前操作

db.dropDataBase()              删除当前数据库

db.eval(func,args)             run code server-side

db.getCollection(cname)        取得一个数据集合,同用法:db['cname'] or db.cname

db.getCollenctionNames()       取得所有数据集合的名称列表

db.getLastError()              返回最后一个错误的提示消息

db.getLastErrorObj()           返回最后一个错误的对象

db.getMongo()                  取得当前服务器的连接对象get the server connection object

db.getMondo().setSlaveOk()     allow this connection to read from then nonmaster membr of a replica pair

db.getName()                   返回当操作数据库的名称

db.getPrevError()              返回上一个错误对象

db.getProfilingLevel()         ?什么等级

db.getReplicationInfo()        ?什么信息

db.getSisterDB(name)           get the db at the same server as this onew

db.killOp()                    停止(杀死)在当前库的当前操作

db.printCollectionStats()      返回当前库的数据集状态

db.printReplicationInfo()

db.printSlaveReplicationInfo()

db.printShardingStatus()       返回当前数据库是否为共享数据库

db.removeUser(username)        删除用户

db.repairDatabase()            修复当前数据库

db.resetError()                

db.runCommand(cmdObj)          run a database command. if cmdObj is a string, turns it into {cmdObj:1}

db.setProfilingLevel(level)    0=off,1=slow,2=all

db.shutdownServer()            关闭当前服务程序

db.version()                   返回当前程序的版本信息

 

 

数据集()操作语法

db.linlin.find({id:10})          返回linlin数据集ID=10的数据集

db.linlin.find({id:10}).count()  返回linlin数据集ID=10的数据总数

db.linlin.find({id:10}).limit(2)返回linlin数据集ID=10的数据集从第二条开始的数据集

db.linlin.find({id:10}).skip(8)  返回linlin数据集ID=10的数据集从0到第八条的数据集

db.linlin.find({id:10}).limit(2).skip(8)  返回linlin数据集ID=1=的数据集从第二条到第八条的数据

db.linlin.find({id:10}).sort()   返回linlin数据集ID=10的排序数据集

db.linlin.findOne([query])       返回符合条件的一条数据

db.linlin.getDB()                返回此数据集所属的数据库名称

db.linlin.getIndexes()           返回些数据集的索引信息

db.linlin.group({key:...,initial:...,reduce:...[,cond:...]})

db.linlin.mapReduce(mayFunction,reduceFunction,<optional params>)

db.linlin.remove(query)                      在数据集中删除一条数据

db.linlin.renameCollection(newName)          重命名些数据集名称

db.linlin.save(obj)                          往数据集中插入一条数据

db.linlin.stats()                            返回此数据集的状态

db.linlin.storageSize()                      返回此数据集的存储大小

db.linlin.totalIndexSize()                   返回此数据集的索引文件大小

db.linlin.totalSize()                        返回些数据集的总大小

db.linlin.update(query,object[,upsert_bool])在此数据集中更新一条数据                         

db.linlin.validate()                         验证此数据集                                       

db.linlin.getShardVersion()                  返回数据集共享版本号

 

 

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

db.linlin.find()                     select * from linlin

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

db.linlin.find().skip(10).limit(20)  从查询结果的第十条开始读20条数据  select * from linlin limit 10,20  ----------mysql

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

db.linlin.find().sort({'ID':-1})      select * from linlin order by ID desc

db.linlin.distinct('name',{'ID':{$lt:20}})   select distinct(name) from linlin where ID<20

 

db.linlin.group({key:{'name':true},cond:{'name':'foo'},reduce:function(obj,prev){prev.msum+=obj.marks;},initial:{msum:0}})

select name,sum(marks) from linlin group by name

db.linlin.find('this.ID<20',{name:1})     select name from linlin where ID<20

 

 

db.linlin.insert({'name':'foobar','age':25})  insert into linlin ('name','age') values('foobar',25)

db.linlin.insert({'name':'foobar','age':25,'email':'cclove2@163.com'})

 

 

db.linlin.remove({})                   delete * from linlin

db.linlin.remove({'age':20})           delete linlin where age=20

db.linlin.remove({'age':{$lt:20}})     delete linlin where age<20

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

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

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

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

 

 

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

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

原创粉丝点击