$person = array( 'name' => 'Cesar Rodas', 'email' => '[email protected]', 'address' => array( array( 'country' => 'PY', 'zip' => '2160', 'address1' => 'foo bar' ), array( 'country' => 'PY', 'zip' => '2161', 'address1' => 'foo bar bar foo' ), ), 'sessions' => 0, ); $safe_insert = true; $collection->insert($person, $safe_insert); $person_identifier = $person['_id'];
$filter = array('email' => '[email protected]'); $new_document = array( '$inc' => array('sessions' => 1), '$set' => array( 'address.0.address2' => 'Some foobar street', ), '$unset' => array('address.1' => 1), ); $options['multiple'] = false; $collection->update( $filter, $new_document, $options);MongoDB也支持批量更新,与关系型数据库类似,可以更新给定条件的所有文档,如果想这么做的话,就需要设置options的multiple的值为true.
$filter = array('email' => '[email protected]'); $cursor = $collection->find($filter); foreach ($cursor as $user) { var_dump($user); }例,获取sessions大于10的信息:
$filter = array('sessions' => array('$gt' => 10)); $cursor = $collection->find($filter);例,获取没有设置sessions属性的信息:
$filter = array( 'sessions' => array('$exists' => false)); $cursor = $collection->find($filter);例,获取地址在PY并且sessions大于15的信息:
$filter = array( 'address.country' => 'PY', 'sessions' => array('$gt' => 10)); $cursor = $collection->find($filter);
$total = $cursor->total(); $cursor->limit(20)->skip(40); foreach($cursor as $user) { }
$countries = $collection->distinct( array("address.country")); $result = $collection->group(array("address.country" => True),array("sum" => 0),"function (obj, prev) { prev.sum += 1; }", array("session" => array('$gt' => 10)) );
$filter = array('field' => 'foo'); $collection->remove($filter);要注意,默认所有符合条件的文档都会被删除,如果只想删除符合条件的第1个文档,那么在给remove函数的第二个参数赋值为true。
$collection->ensureIndex( array('email' => 1), array('unique' => true, 'background' => true));
$collection->ensureIndex( array('address.country' => 1, 'sessions' => 1), array('background' => true));
$filter = array( 'address.country' => 'PY',); $cursor = $collection->find($filter)->order( array('sessions' => -1)); $collection->ensureIndex( array('address.country' => 1, 'sessions' => -1), array('background' => true));
$metadata = array( "filename" => "path.avi", "downloads" => 0, "comment" => "This file is foo bar", "permissions" => array( "crodas" => "write", "everybody" => "read", ) ); $grid = $db->getGridFS(); $grid->storeFile("/file/to/path.avi", $metadata);正如你所看到的,这很简单且容易理解。
$map = new MongoCode("function () { var i; for (i=0; i < this.tags.length; i++) { emit(this.tags[i], {count: 1}); } }"); $reduce = new MongoCode("function (key, values) { var i, total=0; for (i=0; i < values.length; i++) { total = values[i].count; } return {count: total} }");然后执行map-reduce命令:
$map_reduce = array( 'out' => 'tags_info', 'verbose' => true, 'mapreduce' => 'posts', 'map' => $map, 'reduce' => $reduce, ); $information = $db->command($map_reduce); var_dump($information);
$filter = array('username' => new MongoRegex("/.*bar.*/i"),); $collection->find($filter);在使用Regex时要小心,大多时候它不能使用索引,因此它将对整个数据扫描,所以比较好的方法是对文档的数目进行限制。
$filter = array('username' => new MongoRegex("/.*bar.*/i"),'karma' => array('$gt' => 10),); $collection->find($filter);使用Regex可以完成如下这个复杂的查询:
$filter = array('username' => new MongoRegex("/[a-z][a-z0-9\_]+(\_[0-9])?/i"),'karma' => array('$gt' => 10),); $collection->find($filter);