Mongodb_基础实例

2016/03/28 12:00
阅读数 63

mongodb

<?php
/**
 * Mongodb Test
 * 
 * Tools:
 * Zend Studio 8.x
 * Eclipse Pulg :Toggle Varpper
 * 
 * @author Wu Bai Qing<wbqyyicx@gmail.com>
 * @version $Id: Mongodb.php 17 2011-09-17 06:04:15Z wbq $ 
 */

$mongo_server_name = 'admin';
$mongo_server_pwd  = 'admin';

// 连接Mongo数据库   数据库地址:端口/账号:密码;
$mongo = new Mongo('mongodb://localhost:27017/admin:admin');

// 选择一个数据库和要操作的集(如果没有数据库默认创建)
$collection = $mongo->selectDB('rrs_result')->selectCollection('content');


/*
// 添加
$content = array(
    'title'=>'叶子-吴佰清',
    'author'=>'吴佰清',
    'url'=>'http://www.cnblogs.com/wubaiqing/archive/2011/09/17/2179870.html',
);
$collection->insert($content);
*/

/*
// 查询
$colle = $collection->find(array('title'=>'叶子-吴佰清'));

foreach ($colle as $key => $val)
{
    var_dump($val);
}
*/

/*
// 修改
$where = array('title'=>'叶子-吴佰清');
$set = array('title'=>'叶子');

$collection->update($where,array(
    '$set'=>$set,
));
*/

/*
//删除
$collection->remove(array(
    'title'=>'叶子',
));
*/


// End 2011-09-17 23:39
?>


mongodb与mysql语法对照

SQL Statement Mongo Query Language Statement
CREATE TABLE USERS (a Number, b Number) Implicit or use MongoDB::createCollection().
INSERT INTO USERS VALUES(1,1) $db->users->insert(array("a" => 1, "b" => 1));
SELECT a,b FROM users $db->users->find(array(), array("a" => 1, "b" => 1));
SELECT * FROM users WHERE age=33 $db->users->find(array("age" => 33));
SELECT a,b FROM users WHERE age=33 $db->users->find(array("age" => 33), array("a" => 1, "b" => 1));
SELECT a,b FROM users WHERE age=33 $db->users->find(array("age" => 33), array("a" => 1, "b" => 1));
SELECT a,b FROM users WHERE age=33 ORDER BY name $db->users->find(array("age" => 33), array("a" => 1, "b" => 1))->sort(array("name" => 1));
SELECT * FROM users WHERE age>33 $db->users->find(array("age" => array('$gt' => 33)));
SELECT * FROM users WHERE age<33 $db->users->find(array("age" => array('$lt' => 33)));
SELECT * FROM users WHERE name LIKE "%Joe%" $db->users->find(array("name" => new MongoRegex("/Joe/")));
SELECT * FROM users WHERE name LIKE "Joe%" $db->users->find(array("name" => new MongoRegex("/^Joe/")));
SELECT * FROM users WHERE age>33 AND age<=40 $db->users->find(array("age" => array('$gt' => 33, '$lte' => 40)));
SELECT * FROM users ORDER BY name DESC $db->users->find()->sort(array("name" => -1));
CREATE INDEX myindexname ON users(name) $db->users->ensureIndex(array("name" => 1));
CREATE INDEX myindexname ON users(name,ts DESC) $db->users->ensureIndex(array("name" => 1, "ts" => -1));
SELECT * FROM users WHERE a=1 and b='q' $db->users->find(array("a" => 1, "b" => "q"));
SELECT * FROM users LIMIT 10 SKIP 20 $db->users->find()->limit(10)->skip(20);
SELECT * FROM users WHERE a=1 or b=2 $db->users->find(array('$or' => array(array("a" => 1), array("b" => 2))));
SELECT * FROM users LIMIT 1 $db->users->find()->limit(1);
EXPLAIN SELECT * FROM users WHERE z=3 $db->users->find(array("z" => 3))->explain()
SELECT DISTINCT last_name FROM users $db->command(array("distinct" => "users", "key" => "last_name"));
SELECT COUNT(*y) FROM users $db->users->count();
SELECT COUNT(*y) FROM users where AGE > 30 $db->users->find(array("age" => array('$gt' => 30)))->count();
SELECT COUNT(AGE) from users $db->users->find(array("age" => array('$exists' => true)))->count();
UPDATE users SET a=1 WHERE b='q' $db->users->update(array("b" => "q"), array('$set' => array("a" => 1)));
UPDATE users SET a=a+2 WHERE b='q' $db->users->update(array("b" => "q"), array('$inc => array("a" => 2)));
DELETE FROM users WHERE z="abc" $db->users->remove(array("z" => "abc"));

归类

    查询colls所有数据

        db.colls.find() //select * from colls 

    通过指定条件查询

        db.colls.find({‘last_name': ‘Smith'});//select * from colls where last_name='Smith' 

    指定多条件查询

        db.colls.find( { x : 3, y : “foo” } );//select * from colls where x=3 and y='foo'

    指定条件范围查询

        db.colls.find({j: {$ne: 3}, k: {$gt: 10} });//select * from colls where j!=3 and k>10

    查询不包括某内容

        db.colls.find({}, {a:0});//查询除a为0外的所有数据


    支持<, <=, >, >=查询,需用符号替代分别为$lt,$lte,$gt,$gte

        db.colls.find({ “field” : { $gt: value } } ); 

        db.colls.find({ “field” : { $lt: value } } ); 

        db.colls.find({ “field” : { $gte: value } } );

        db.colls.find({ “field” : { $lte: value } } );


    也可对某一字段做范围查询

        db.colls.find({ “field” : { $gt: value1, $lt: value2 } } );


    不等于查询用字符$ne

        db.colls.find( { x : { $ne : 3 } } );


    in查询用字符$in

        db.colls.find( { “field” : { $in : array } } );

        db.colls.find({j:{$in: [2,4,6]}});


    not in查询用字符$nin

        db.colls.find({j:{$nin: [2,4,6]}});


    取模查询用字符$mod

        db.colls.find( { a : { $mod : [ 10 , 1 ] } } )// where a % 10 == 1


    $all查询

        db.colls.find( { a: { $all: [ 2, 3 ] } } );//指定a满足数组中任意值时


    $size查询

        db.colls.find( { a : { $size: 1 } } );//对对象的数量查询,此查询查询a的子对象数目为1的记录


    $exists查询

        db.colls.find( { a : { $exists : true } } ); // 存在a对象的数据

        db.colls.find( { a : { $exists : false } } ); // 不存在a对象的数据


    $type查询$type值为bsonhttp://bsonspec.org/数 据的类型值

        db.colls.find( { a : { $type : 2 } } ); // 匹配a为string类型数据

        db.colls.find( { a : { $type : 16 } } ); // 匹配a为int类型数据


    使用正则表达式匹配

        db.colls.find( { name : /acme.*corp/i } );//类似于SQL中like


    内嵌对象查询

        db.colls.find( { “author.name” : “joe” } );


    1.3.3版本及更高版本包含$not查询

        db.colls.find( { name : { $not : /acme.*corp/i } } );

        db.colls.find( { a : { $not : { $mod : [ 10 , 1 ] } } } );


    sort()排序

        db.colls.find().sort( { ts : -1 } );//1为升序2为降序


    limit()对限制查询数据返回个数

        db.colls.find().limit(10)


    skip()跳过某些数据

        db.colls.find().skip(10)


    snapshot()快照保证没有重复数据返回或对象丢失


    count()统计查询对象个数

        db.students.find({‘address.state' : ‘CA'}).count();//效率较高


展开阅读全文
打赏
1
1 收藏
分享
加载中
更多评论
打赏
0 评论
1 收藏
1
分享
OSCHINA
登录后可查看更多优质内容
返回顶部
顶部