博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
mongodb 2php 操作
阅读量:5124 次
发布时间:2019-06-13

本文共 3437 字,大约阅读时间需要 11 分钟。

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);

 

 

转载于:https://www.cnblogs.com/keiweila/p/7884036.html

你可能感兴趣的文章
Discuz3.3精仿小米风格整站模板制作——1、新建模板方案
查看>>
挺好用的Markdown写法
查看>>
C# ADO.NET
查看>>
快速排序算法
查看>>
HDU 1394 Minimum Inversion Number
查看>>
JQuery方法
查看>>
P1049 装箱问题
查看>>
第一百二十六节,JavaScript,XPath操作xml节点
查看>>
LightOJ 1393 Crazy Calendar(博弈)题解
查看>>
第一步:Axure 使用svn多人协作产品开发(提交文件)
查看>>
用IIS配置反向代理
查看>>
sufeinet
查看>>
论算法的实际应用——泡妞论
查看>>
HTTP 错误 404.3 – Not Found 由于扩展配置问题而无法提供您请求的页面。如果该页面是脚本,请添加处理程序。如果应下载文件,请添加 MIME 映射。...
查看>>
js和layerjs配合实现的拖拽表格列
查看>>
Spring MVC集成slf4j-logback
查看>>
java常量池
查看>>
URL类
查看>>
flask(精讲)
查看>>
Java异常处理原则与技巧总结
查看>>