21.30 mongodb创建集合、数据管理
创建集合语法:
db.createCollection(name,options)
name就是集合的名字,options可选,用来配置集合的参数。
例如我要创建一个名为mycol的集合,命令如下:
> db.createCollection("mycol", { capped : true, size : 6142800, max : 10000 } ) { "ok" : 1 }
以上命令创建了一个名为mycol的集合,在参数中指定了启用封顶集合,并且设置该集合的大小为6142800个字节,以及设置该集合允许在文件的最大数量为10000。
可配置集合的参数如下:
capped true/false (可选)如果为true,则启用封顶集合。封顶集合是固定大小的集合,当它达到其最大大小,会自动覆盖最早的条目。如果指定true,则也需要指定尺寸参数。
autoindexID true/false (可选)如果为true,自动创建索引_id字段的默认值是false。
size (可选)指定最大大小字节封顶集合。如果封顶如果是 true,那么你还需要指定这个字段。单位B
max (可选)指定封顶集合允许在文件的最大数量。
MongoDB其他的一些常用命令:
show collections命令可以查看集合,或者使用show tables也可以:
> show tables mycol > show collections mycol >
插入数据命令,一个集合的数据结构是在插入数据时定义的:
// 如果集合不存在,直接插入数据,则mongodb会自动创建集合 db.Account.insert({AccountID:1,UserName:"test",password:"123456"}) WriteResult({ "nInserted" : 1 }) > show tables Account mycol > db.mycol.insert({AccountID:1,UserName:"test",password:"123456"}) WriteResult({ "nInserted" : 1 })
更新数据命令:
// $set是一个动作,以下这条语句是在集合中新增了一个名为Age的key,设置的value为20 > db.Account.update({AccountID:1},{"$set":{"Age":20}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
查看所有的文档:
> db.Account.insert({AccountID:2,UserName:"test2",password:"123456"}) WriteResult({ "nInserted" : 1 }) > db.Account.find() // 查看指定集合中的所有文档 > db.Account.find() { "_id" : ObjectId("5bf3ebafca77e33a30f6f800"), "AccountID" : 1, "UserName" : "test", "password" : "123456", "Age" : 20 } { "_id" : ObjectId("5bf3ebedca77e33a30f6f801"), "AccountID" : 1, "UserName" : "test", "password" : "123456" } { "_id" : ObjectId("5bf3ec24ca77e33a30f6f802"), "AccountID" : 1, "UserName" : "test", "password" : "123456" } { "_id" : ObjectId("5bf3ed5679fbe69049de7330"), "AccountID" : 2, "UserName" : "test2", "password" : "123456" }
可以根据条件进行查询,例如我要指定id进行查看:
> db.Account.find({AccountID:1}) { "_id" : ObjectId("5bf3ebafca77e33a30f6f800"), "AccountID" : 1, "UserName" : "test", "password" : "123456", "Age" : 20 } { "_id" : ObjectId("5bf3ebedca77e33a30f6f801"), "AccountID" : 1, "UserName" : "test", "password" : "123456" } { "_id" : ObjectId("5bf3ec24ca77e33a30f6f802"), "AccountID" : 1, "UserName" : "test", "password" : "123456" } db.Account.find({AccountID:2}) > db.Account.find({AccountID:2}) { "_id" : ObjectId("5bf3ed5679fbe69049de7330"), "AccountID" : 2, "UserName" : "test2", "password" : "123456" }
根据条件删除数据:
> db.Account.remove({AccountID:1}) WriteResult({ "nRemoved" : 3 }) > db.Account.find() { "_id" : ObjectId("5bf3ed5679fbe69049de7330"), "AccountID" : 2, "UserName" : "test2", "password" : "123456" }
删除集合:
> db.Account.drop() true > show tables mycol
查看集合的状态:
db.printCollectionStats() > db.printCollectionStats() mycol { "ns" : "test.mycol", "size" : 81, "count" : 1, "avgObjSize" : 81, "storageSize" : 16384, "capped" : true, "max" : 10000, "maxSize" : 6142976, "sleepCount" : 0, "sleepMS" : 0, "wiredTiger" : { "metadata" : { "formatVersion" : 1
21.31 php的mongodb扩展
php的官方给出了两个mongodb的扩展,一个是mongodb.so,另一个是mongo.so。mongodb.so是针对新版本的php扩展,而mongo.so则是对旧版本的php扩展。
以下是官方给出的关于两个扩展的参考文档:
https://docs.mongodb.com/ecosystem/drivers/php/
由于现在新旧版本的php都有在使用,所以我们需要了解两种扩展的安装方式,首先介绍mongodb.so的安装方式:
有两种方式可以安装mongodb.so,第一种是通过git安装:
[root@localhost ~]# cd /usr/local/src/ [root@localhost /usr/local/src]# git clone https://github.com/mongodb/mongo-php-driver [root@localhost /usr/local/src/mongo-php-driver]# git submodule update --init [root@aminglinux-130 mongo-php-driver]# /usr/local/php-fpm/bin/phpize Configuring for: PHP Api Version: 20131106 Zend Module Api No: 20131226 Zend Extension Api No: 220131226 [root@localhost /usr/local/src/mongo-php-driver]# ./configure --with-php-config=/usr/local/php-fpm/bin/php-config /bin/php-config checking for grep that handles long lines and -e... /usr/bin/grep checking for egrep... /usr/bin/grep -E checking for a sed that does not truncate output... /usr/bin/sed checking for cc... cc checking for C compiler default output file name... a.out checking whether the C compiler works... yes checking whether we are cross compiling... no [root@localhost /usr/local/src/mongo-php-driver]# make && make install B/CursorInterface.c -o src/MongoDB/CursorInterface.lo cc -I. -I/usr/local/src/mongo-php-driver -DPHP_ATOM_INC -I/usr/local/src/mongo-php-driver/include -I/usr/local/src/mongo-php-driver/main -I/usr/local/src/mongo-php-driver -I/usr/local/php-fpm/include/php -I/usr/local/php-fpm/include/php/main -I/usr/local/php-fpm/include/php/TSRM -I/usr/local/php-fpm/include/php/Zend -I/usr/local/php-fpm/include/php/ext -I/usr/local/php-fpm/include/php/ext/date/lib -I/usr/local/src/mongo-php-driver/src/libmongoc/src/common/ -I/usr/local/src/mongo-php-driver/src/libmongoc/src/libbson/src/ -I/usr/local/src/mongo-php-driver/src/libmongoc/src/libbson/src/jsonsl/ -I/usr/local/src/mongo-php-driver/src/libmongoc/src/libmongoc/src/ -I/usr/local/src/mongo-php-driver/src/BSON/ -I/usr/local/src/mongo-php-driver/src/MongoDB/ -I/usr/local/src/mongo-php-driver/src/MongoDB/Exception/ -I/usr/local/src/mongo-php-driver/src/MongoDB/Monitoring/ -I/usr/local/src/mongo-php-driver/src/contrib/ -DHAVE_CONFIG_H -g -O2 -c /usr/local/src/mongo-php-driver/src/MongoDB/CursorInterface.c -fPIC -DPIC -o src/MongoDB/.libs/CursorInterface.o [root@localhost /usr/local/src/mongo-php-driver]# vim /usr/local/php/etc/php.ini extension = mongodb.so // 增加这一行 [root@localhost /usr/local/src/mongo-php-driver]# /usr/local/php/bin/php -m |grep mongodbmongodb [root@localhost /usr/local/src/mongo-php-driver]#
由于国内连GitHub不是很流畅,所以这种安装方式会有点慢。
第二种是通过源码包安装:
[root@localhost ~] [root@localhost /usr//src] [root@localhost /usr//src/mongodb-1.3.0]extension = mongodb.so // 增加这一行 [root@localhost /usr//src/mongodb-1.3.0]mongodb [root@localhost /usr//src/mongodb-1.3.0]
21.32 php的mongo扩展
安装过程如下:
[root@aminglinux-130 ~]# cd /usr/local/src/ [root@aminglinux-130 src]# wget --2018-11-25 18:16:04-- 正在解析主机 pecl.php.net (pecl.php.net)... 104.236.228.160 正在连接 pecl.php.net (pecl.php.net)|104.236.228.160|:443... 已连接。 已发出 HTTP 请求,正在等待回应... 200 OK 长度:210341 (205K) [application/octet-stream] 正在保存至: “mongo-1.6.16.tgz”
100%[==============================================>] 210,341 7.30KB/s 用时 28s
2018-11-25 18:16:43 (7.30 KB/s) - 已保存 “mongo-1.6.16.tgz” [210341/210341])
[root@aminglinux-130 src]# tar -zxvf mongo-1.6.16.tgz [root@aminglinux-130 mongo-1.6.16]# /usr/local/php-fpm/bin/phpize Configuring for: PHP Api Version: 20131106 Zend Module Api No: 20131226 Zend Extension Api No: 220131226 [root@aminglinux-130 mongo-1.6.16]# ./configure --with-php-config=/usr/local/php-fpm/bin/php-config checking for grep that handles long lines and -e... /usr/bin/grep checking for egrep... /usr/bin/grep -E checking for a sed that does not truncate output... /usr/bin/sed checking for cc... cc checking for C compiler default output file name... a.out checking whether the C compiler works... yes checking whether we are cross compiling... no checking for suffix of executables... checking for suffix of object files... o checking whether we are using the GNU C compiler... yes checking whether cc accepts -g... yes checking for cc option to accept ISO C89... none needed checking how to run the C preprocessor... cc -E checking for icc... no checking for suncc... no checking whether cc understands -c and -o together... yes checking for system library directory... lib checking if compiler supports -R... no checking if compiler supports -Wl,-rpath,... yes checking build system type... x86_64-unknown-linux-gnu checking host system type... x86_64-unknown-linux-gnu checking target system type... x86_64-unknown-linux-gnu checking for PHP prefix... /usr/local/php-fpm checking for PHP includes... -I/usr/local/php-fpm/include/php -I/usr/local/php-fpm/include/php/main -I/usr/local/php-fpm/include/php/TSRM -I/usr/local/php-fpm/include/php/Zend -I/usr/local/php-fpm/include/php/ext -I/usr/local/php-fpm/include/php/ext/date/lib checking for PHP extension directory... /usr/local/php-fpm/lib/php/extensions/no-debug-non-zts-20131226 checking for PHP installed headers prefix... /usr/local/php-fpm/include/php checking if debug is enabled... no checking if zts is enabled... no checking for re2c... no configure: WARNING: You will need re2c 0.13.4 or later if you want to regenerate PHP parsers. checking for gawk... gawk checking whether to enable Mongo extension... yes, shared checking Build with OpenSSL support... yes checking for pkg-config... /usr/bin/pkg-config checking whether byte ordering is bigendian... no checking whether to include code coverage symbols... no checking Build with Cyrus SASL support... no checking for ld used by cc... /usr/bin/ld checking if the linker (/usr/bin/ld) is GNU ld... yes checking for /usr/bin/ld option to reload object files... -r checking for BSD-compatible nm... /usr/bin/nm -B checking whether ln -s works... yes checking how to recognize dependent libraries... pass_all checking for ANSI C header files... yes checking for sys/types.h... yes checking for sys/stat.h... yes checking for stdlib.h... yes checking for string.h... yes checking for memory.h... yes checking for strings.h... yes checking for inttypes.h... yes checking for stdint.h... yes checking for unistd.h... yes checking dlfcn.h usability... yes checking dlfcn.h presence... yes checking for dlfcn.h... yes checking the maximum length of command line arguments... 1572864 checking command to parse /usr/bin/nm -B output from cc object... ok checking for objdir... .libs checking for ar... ar checking for ranlib... ranlib checking for strip... strip checking if cc supports -fno-rtti -fno-exceptions... no checking for cc option to produce PIC... -fPIC checking if cc PIC flag -fPIC works... yes checking if cc static flag -static works... no checking if cc supports -c -o file.o... yes checking whether the cc linker (/usr/bin/ld -m elf_x86_64) supports shared libraries... yes checking whether -lc should be explicitly linked in... no checking dynamic linker characteristics... GNU/Linux ld.so checking how to hardcode library paths into programs... immediate checking whether stripping libraries is possible... yes checking if libtool supports shared libraries... yes checking whether to build shared libraries... yes checking whether to build static libraries... no
creating libtool appending configuration tag "CXX" to libtool configure: creating ./config.status config.status: creating config.h
make && make install
vim /usr/local/php-fpm/php/etc/php.ini /usr/local/php/bin/php -m |grep mongo
测试mongo扩展:
先去掉MongoDB的用户认证,然后编辑测试页:
[root@aminglinux-130 php]# vim /usr/lib/systemd/system/mongod.service # 将--auth去掉
[root@aminglinux-130 php]# systemctl daemon-reload
[root@aminglinux-130 php]# systemctl restart mongod.service
vim /data/wwwroot/abc.com/index.php # 编辑测试页; = ->createCollection(); ; ?>