21.30 mongodb創建集合、數據管理 21.31 php的mongodb擴

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-149 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][root@localhost /usr//src][root@localhost /usr//src/mongodb-1.3.0][root@localhost /usr//src/mongodb-1.3.0][root@localhost /usr//src/mongodb-1.3.0][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-149 ~]# cd /usr/local/src/
[root@aminglinux-149 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-149 src]# tar -zxvf mongo-1.6.16.tgz
[root@aminglinux-149 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-149 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擴展:

  1. 先去掉MongoDB的用戶認證,然後編輯測試頁:

     

    [root@aminglinux-149 php]# vim /usr/lib/systemd/system/mongod.service   # 將--auth去掉
    [root@aminglinux-149 php]# systemctl daemon-reload
    [root@aminglinux-149 php]# systemctl restart mongod.service
    vim /data/wwwroot/abc.com/index.php   # 編輯測試頁

  2. <?php = new MongoClient();  = ->;  = ->createCollection(); ;
      ?>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章