1连接
db.getCollection('test').find({})
// 连接Mongo数据库 数据库地址:端口/账号:密码;
$mongo = new Mongo('mongodb://localhost:27017/admin:admin');
$this->link = new \MongoClient('mongodb://user:password@host:port/dbname);
// 选择一个数据库和要操作的集(如果没有数据库默认创建)
$collection = $mongo->selectDB('rrs_result')->selectCollection('content');
2添加
<?php
$content = array(
'title'=>'叶子-吴佰清',
'author'=>'吴佰清',
'url'=>'http://www.cnblogs.com/wubaiqing/archive/2011/09/17/2179870.html',
);
$collection->insert($ );
2查询
查询所有数据:
$result = $col->findOne($condition, $fields);
<?php
$rows = $collection->find();
foreach ($rows as $key => $val)
{
var_dump($val);
}
?>
{
"_id" : ObjectId("5a163546b845c5d0888269bf”),对象
"title" : "叶子-吴佰清",
"author" : "authoaar"
}
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));
and
SELECT * FROM users WHERE a=1 and b='q' || $db->users->find(array("a" => 1, "b" => "q"));
order
SELECT * FROM users WHERE a=1 or b=2 ||$db->users->find(array('$or' => array(array("a" => 1), array("b" => 2))));
limit
SELECT * FROM users LIMIT 1 ||$db->users->find()->limit(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 ORDER BY name DESC || $db->users->find()->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 age>33 AND age<=40 $db->users->find(array("age" => array('$gt' => 33, '$lte' => 40)));
like
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/")));
distinct
SELECT DISTINCT last_name FROM users $db->command(array("distinct" => "users", "key" => "last_name"));
count
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();
3
更改数据:
<?php
$where = array('title'=>'叶子-吴佰清');
$set = array('title'=>'叶子');
$collection->update($where,array(
'$set'=>$set,
));
?>
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)));
4
<?php
$collection->remove(array(
'title'=>'叶子',
));
?>
DELETE FROM users WHERE z="abc" $db->users->remove(array("z" => "abc"));
4创建索引
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));
4explain显示了mysql如何使用索引来处理select语句以及连接表。可以帮助选择更好的索引和写出更优化的查询语句。
EXPLAIN SELECT * FROM users WHERE z=3 $db->users->find(array("z" => 3))->explain()
待解决
1update
$result = $col->update($condition,$data,$option);第三个参数是什么
$options = array( 'upsert' => false, 'multiple' => true,);
4
$result = $col->aggregate($pipeline);