您的位置:首页 > 数据库 > Mongodb

mongodb 学习笔记

2016-09-22 10:02 357 查看
//验证环境是否支持
if( !extension_loaded( 'mongo' ) )
exit( ' not fonud mongo extentions' . "\n" );

if( !class_exists( 'MongoClient' ) )
exit( 'not support mongo' . "\n" );


//连接数据库
//http://php.net/manual/zh/mongoclient.construct.php
try {
$mongo = new MongoClient('mongodb://127.0.0.1:27017');
}catch ( \Exception $e ) {
exit( $e->getMessage() );
}
//选择数据库test,选择test1 ; 不存在在插入数据自动创建
$collections   = $mongo->selectDB( 'test' )->selectCollection( 'test1' );


//具体操作,看手册 http://php.net/manual/zh/mongocollection.insert.php //新增数据
$arr        = [
'name'  => 'joe',
'age'   => 40,
'pages' => 10
];
   //通过设置 insert 的第二个参数 [ 'w' => 0 ],res 返回的是布尔值,
$res        = $collections->insert( $arr );

var_dump( $res );


返回值:

服务器之间查看:


//修改
//直接修改,即找到对应信息,将其直接替换为 new_data; w => 0 也是返回为布尔值,但感觉不好用,第三个参数 upsert 匹配到了则更新,否则新增; multiple 批量
$where      = [ 'name' => 'joe' ];
$new_data   = [ 'name' => 'joe modify' ];
$res        = $collections->update( $where, $new_data, ['w' => 0] );
var_dump( $res );


原数据:

更新后


//使用 $set “用来指定一个键的值,如果这个键不存在,则创建它,这对更新模式或者增加用户定义键 非常方便”
$where      = [ 'name' => 'joe modify' ];
$new_data   = [ '$set' => ['name' => 'joe', 'age' => 39, 'pages' => "10"]];
$res        = $collections->update( $where, $new_data, ['w' => 0] );
var_dump( $res );


原数据:

更新后:


//使用 $inc "用来增加已有键的值,或者在键不存在时创建一个键,对于分析数据,因果关系,投票或者其他数据变化的地方,都非常有用" 对int类型结果进行增减 3/+3 是一样的;对字符串类型的进行inc操作,会报异常
$where      = [ 'name' => 'joe' ];
$new_data   = [ '$inc' => ['age' => +3 ] ];
$res        = $collections->update( $where, $new_data );
var_dump( $res );


更新前:

更新后:


//数组修改器 $push “会向已有的数组末尾加入一个元素,要是没有就会创建一个新的数组”
$where      = [ 'name' => 'joe' ];
$new_data   = [ '$push' => [ 'friends' => ['lily','angly', 'mike'] ] ];
$res        = $collections->update( $where, $new_data );
var_dump( $res );


更新前:

更新后:


//如果一个值不在数组中就把它添加进去
$where      = [ 'name' => 'joe' ];
$new_data   = [ '$addToSet' => ['friends'=> 'oby' ] ];
$res        = $collections->update( $where, $new_data );
var_dump( $res );


更新前:

更新后:


//如果想一次添加多个不存在的值,可以使用each
$where      = ['name'=> 'joe'];
$new_data   = [ '$addToSet' => [ 'friends' => [ '$each' => ['a','b','c','oby','lily'] ] ]];
$res        = $collections->update( $where, $new_data );
var_dump( $res );


更新前:

更新后:


//$pop  从数组的任何一端删除元素 1 表示从数组的尾部剔除一个;-1 表示从数组的头部
$where      = ['name' => 'joe'];
$new_data   = [ '$pop' => ['friends' => 1] ];
$res        = $collections->update( $where, $new_data );
var_dump( $res );


更新前:

更新后:


//$pull 根据指定条件,删除数组的成员,pull会将所匹配的成员全部删掉,[1,2,1,1] 如果匹配1,那么数组最后只剩2
$where      = [ 'name' => 'joe' ];
$new_data   = [ '$pull' => ['friends' => 'lily'] ];
$res        = $collections->update( $where, $new_data );
var_dump( $res );


更新前:

更新后:


//数组定位器 ,用来定位查询文档已经匹配的元素,并进行更新 ,定位符只能更新第一个匹配的元素,如果有多少,只更新第一个
$where      = [ 'arrs.a' => 4];
$new_data   = [ '$set' => ['arrs.$.a' => 3] ];
$res        = $collections->update( $where, $new_data );
var_dump( $res );


更新前:

更新后:


//findandmodify 更新/删除 一个文档 并返回文档
//remove false 表示不删除;new false 表示返回更新前数据,之前没有数据 返回 null;upsert true 如果条件没有找到,则新增
//执行的是新增操作
$where      = [ 'name' => 'joe' ];
$new_data   = [ '$set' => ['pages' => 400] ];
$res        = $collections->findAndModify( $where, $new_data, [], ['new' => false, 'remove' => false,'upsert' => true] );
var_dump( $res );


更新前:

更新后:


//执行的是更新操作
$where      = [ 'name' => 'joe' ];
$new_data   = [ '$set' => ['pages' => 4] ];
$res        = $collections->findAndModify( $where, $new_data, ['name' => 1, 'pages' => 1] );
var_dump( $res );


更新前:

更新后:


//执行的是删除操作
$where      = [ 'name' => 'joe' ];
$res        = $collections->findAndModify( $where, null, null, ['remove' => true] );
var_dump( $res );


更新前:

更新后:


1 //find 查询操作 http://php.net/manual/zh/mongocollection.find.php 2     //根据条件查找所有
3     $where      = ['gender' => 'female'];
4     $res        = $collections->find( $where );
5     var_dump( $res->count() );
6
7 //指定返回的键
8     $where      = ['name' => 'joe'];
9     $fields     = ['name' => 1, '_id' => 0];
10     $res        = $collections->find( $where, $fields );
11     if( $res->hasNext() )
12         var_dump( $res->getNext() );
13
14   //$lt $lte $gt $gte $ne 比较操作符 分别是 < <= > >= !=
15     $where      = ['age' => ['$gt' => 27] ];
16     $res        = $collections->find( $where );
17     var_dump( $res->count() );
18
19     $where      = ['name' => ['$gt' => 'z']];//字母排序
20     $res        = $collections->find( $where );
21     var_dump( iterator_to_array( $res ) );
22
23 //$in $nin   范围查询
24     $where      = [ 'age' => ['$in' => [29,32,27,28] ] ];
25     $res        = $collections->find( $where );
26     var_dump( iterator_to_array( $res ) );


//$or 查询
$where      = ['$or' => [ ['age' => 27],['name' => 'lili'] ]];
$res        = $collections->find( $where );
var_dump( iterator_to_array( $res ) );

//$or 可以包含其他条件语句
$where          = [ '$or' => [ ['age' => ['$in' => [32,33,34] ] ], ['name' => 'joe'] ]];
$res            = $collections->find( $where );
var_dump( iterator_to_array( $res ) );

//$mod 余数 将查询的值跟第一个数求余,等于第二个数的就被帅选处理 例如 age 为1,6,11 的
$where          = ['age' => [ '$mod' => [5,1] ] ];
$res            = $collections->find( $where );
var_dump( iterator_to_array( $res ) );

//$not 是元条件j句,即可以用在其他条件之上
$where          = [ 'age' => ['$not' => ['$mod' => [5,1]]]];
$res            = $collections->find( $where );
var_dump( iterator_to_array( $res ) );


条件句的规则:

  如果比较更新修改器跟查询文档,会发现以“$”开头的键处在不同的位置。在查询中,“$lt”在内层文档,而在更新中"$inc"则是外层文档的键。基本可以肯定:条件句是内层文档的键,而修改器是外层文档的键。

  可对一个键应用多个条件,例如,查找年龄在20-30直接的用户,可以在age上使用$lt,$gt:   $where = [ 'age' => [ '$lt' => 30, '$gt' => 20] ];

  一个键可以有多个条件,但是不能对应多个更新修改器。例如 修改文档不能同时包含 [ '$inc' => ['age' => 1], '$set' => [ 'age' => 40] ]

// null null 不仅仅匹配自身,而且匹配“不存在的”,所以这种匹配会返回缺少这个键的文档;如果仅仅匹配值为null,既要检查该键是否为null,还要通过“$exists”p判断该键是否存在
$where      = [ 'frind' => [ '$in' => [null], '$exists' => true]];
$res        = $collections->find( $where );
var_dump( iterator_to_array( $res ) );

//正则表达式
$where      = [ 'name' => new MongoRegex( '/joe/i' )];
$res        = $collections->find( $where );
var_dump( iterator_to_array( $res ) );

//查询数组 f是数组的键;a 数组的值;同时也查询出有相关条件的信息
$where      = [ 'f' => 'a' ];
$res        = $collections->find( $where );
var_dump( iterator_to_array( $res ) );

//$all 通过多个元素匹配数组 ['f' => ['$all'=>['c']]] = ['f'=>'c']
$where      = [ 'f' => ['$all' => ['c','d'] ] ];
$res        = $collections->find( $where );
var_dump( iterator_to_array( $res ) );

//精准匹配数组的元素
$where      = [ 'f' => ['a','b','c']];
$res        = $collections->find( $where );
var_dump( iterator_to_array( $res ) );

//查询数组指定位置的元素
$where      = [ 'f.2' => 'c'];
$res        = $collections->find( $where );
var_dump( iterator_to_array( $res ) );

//查询数组的个数 size不能跟其他查询子句组合(比如"$gt")但这种查询可以通过文档中增加一个size键来实现
$where      = ['f' => ['$size' => 4]];
$res        = $collections->find( $where );
var_dump( iterator_to_array( $res ) );

//$slice 返回数组的一个子集合; 2 表示 前2个;-2 表示 后两个;[1,2] 表示从第一个开始,取两个
$where      = [ 'name' => 'joe' ];
$res        = $collections->find( $where, ['f' => ['$slice' => [2,2]]] );
var_dump( iterator_to_array( $res ) );


来源:mongodb权威指南
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: