Mongo命令,PHP操作mongo
一、查询出一条数据
1、无条件
db.order_list.findOne();
sql: select * from order_list limit 1;
2、单条件
db.order_list.findOne(price:4900);
sql: select * from order_list where price = 4900 limit 1;
3、多条件
db.order_list.findOne(price:4900,app_id:343);
sql: select * from order_list where price = 4900 and app_id = 343 limit 1;
4、范围区间
db.order_list.findOne(price:{$gt:4900},app_id:343);
sql: select * from order_list where price > 4900 and app_id = 343 limit 1;
5、查询出指定字段
db.order_list.find({
price: {$gte: 4900},app_id: 343
}, {
app_id: 1,openid: 1, register_time: 1, pay_time: 1, price: 1, order_no: 1, agent_price: 1
}).limit(1);
-----------------------------------------------------------------------
sql:
SELECT
app_id,openid,register_time,pay_time,price,order_no,agent_price
FROM
order_list
WHERE
price >= 4900 AND app_id = 343
LIMIT 1;
二、查询出所有数据
1、无条件
db.order_list.find();
sql: select * from order_list;
2、单条件
db.order_list.find({price:4900});
sql: select * from order_list where price = 4900;
3、多条件
db.order_list.find({price:4900,app_id:343});
sql: select * from order_list where price = 4900 and app_id = 343;
4、范围区间
db.order_list.find({
price: { $gte: 4900 }, app_id: 343
});
-----------------------------------------------------------------------
sql:
SELECT * FROM order_list WHERE price >= 4900 AND app_id = 343;
5、查询指定字段
db.order_list.find({
price: {
$gte: 4900
},
app_id: 343
}, {
app_id: 1, openid: 1, register_time: 1, pay_time: 1, price: 1, order_no: 1, agent_price: 1
});
-----------------------------------------------------------------------
sql:
SELECT
app_id, openid, register_time, pay_time, price, order_no, agent_price
FROM
order_list
WHERE
price >= 4900 AND app_id = 343;
三、聚合查询
1、求总条数
db.order_list.aggregate({
$match: {
price: {
$gte: 4900
},
app_id: 343
}
}, {
$count: "total_num"
});
-----------------------------------------------------------------------
sql:
SELECT
count(id) AS total_num
FROM
order_list
WHERE
price >= 4900 AND app_id = 343;
2、求某个字段的和
db.order_list.aggregate({
$match: {
price: {
$gte: 4900
},
app_id: 343
}
}, {
$group: {
_id: null,
price_sum: {
$sum: "$price"
},
agent_price_sum: {
$sum: "$agent_price"
}
}
});
-----------------------------------------------------------------------
sql:
SELECT
sum(price) AS price_num,
sum(agent_price) AS agent_price_sum
FROM
order_list
WHERE
price >= 4900 AND app_id = 343;
3、排序
聚合模式:
db.order_list.aggregate({
$match: {
price: {
$gte: 4900
},
app_id: 343
}
}, {
$sort: {
app_id: - 1,
price: 1
}
});
-----------------------------------------------------------------------
sql:
SELECT * FROM order_list WHERE app_id = 343 ORDER BY app_id DESC, price ASC;
普通模式:
db.order_list.find({
price: {
$gte: 4900
}
}, {
app_id: 1, openid: 1, register_time: 1, pay_time: 1, price: 1, order_no: 1, agent_price: 1
}).sort({
app_id: 1
});//1表示升序,-1表示降序
-----------------------------------------------------------------------
sql:
SELECT
app_id, openid, register_time, pay_time, price, order_no, agent_price
FROM
order_list
WHERE
price >= 4900
ORDER BY
app_id ASC;
4、分组
db.order_list.aggregate({
$match: {
price: {
$gte: 4900
}
}
}, {
$group: {
_id: "$app_id",
price_sum: {
$sum: "$price"
},
agent_sum: {
$sum: "$agent_price"
},
num: {
$sum: 1
}
}
}, {
$sort: {
_id: - 1,
price_sum: - 1
}
});
-----------------------------------------------------------------------
sql:
SELECT
app_id AS _id,sum(price) AS price_sum, sum(agent_price) AS agent_sum,count(id) AS num
FROM
order_list
WHERE
price >= 4900
GROUP BY
app_id
ORDER BY
app_id DESC,price_sum DESC;
5、分页
db.order_list.aggregate({
$match: {
price: {
$gte: 4900
}
}
}, {
$group: {
_id: "$app_id",
price_sum: {
$sum: "$price"
},
agent_sum: {
$sum: "$agent_price"
},
num: {
$sum: 1
}
}
}, {
$sort: {
_id: - 1, price_sum: - 1
}
}, {
$project: {
app_id: 1, price_sum: 1, agent_sum: 1, num: 1
}
}, {
$skip: 1
}, {
$limit: 5
});
------------------------------------------------------------
sql:
SELECT
app_id AS _id, sum(price) AS price_sum, sum(agent_price) AS agent_sum, count(id) AS num
FROM
order_list
WHERE
price >= 4900
GROUP BY
app_id
ORDER BY
app_id DESC, price_sum DESC
LIMIT 1,5;
四、PHP操作mongo (以mongo-php-adapter为例)
1、安装扩展包
composer require alcaeus/mongo-php-adapter
2、调用方式
//实例化mongo链接客户端
$db = new \MongoClient("localhost");
//选择数据库,集合
$c = $db->selectDB("catch_admin")->selectCollection("order_list");
//或者
$c = $db -> catch_admin -> order_list;
3、添加数据
//插入数据
$add_data = [
'app_id' => 123,
'openid' => 'asdgasgasdfasdfasgasdf',
'register_time' => time(),
'pay_time' => time(),
'price' => 12300,
'order_no' => '46435165464315465431546545',
'agent_bili' => 900,
'agent_price' => 1521.1500,
'create_time' => time(),
];
//插入一条数据
$res = $c->insert($add_data);
//批量插入
$data = [];
for ($i=0;$i<5;$i++){
$add_data['app_id'] = $i + 5555;
$data[] = $add_data;
}
$res = $c->batchInsert($data);
4、更新数据
//更新数据
$add_data = [
'app_id' => 12300,
'openid' => 'asdgasgasdfasdfasgasdf',
'register_time' => time(),
'pay_time' => time(),
'price' => 12300,
'order_no' => '46435165464315465431546545',
'agent_bili' => 900,
'agent_price' => 1521.1500,
'create_time' => time(),
];//数据源
//更新一条文档
$c -> update(['app_id'=>12300],['$set'=>$add_data]);
//批量更新
$res = $c -> update(
['app_id'=>['$gte'=>22222222,'$lt'=>222222222]],
['$set'=>$add_data],
['multiple' => true]
);
5、查询数据
//查询一条数据
$res = $c -> findOne(
['app_id'=>12300]
);
//查询一条数据中的某些字段
$res = $c -> findOne(
['app_id'=>['$gte'=>123,'$lt'=>500]],
['app_id'=>1,'openid'=>1,'pay_time'=>1]
);
//查询多条数据
$res = $c -> find(
['app_id'=>['$gte'=>123,'$lt'=>500],'price'=>['$in'=>[5500,3000]]],
['app_id'=>1,'openid'=>1,'pay_time'=>1,'price'=>1]
);
$res = iterator_to_array($res);//对象转数组
6、聚合查询
//聚合
$res = $c->aggregate(
[
['$match'=>['app_id'=>['$gte'=>343],'price'=>['$in'=>[5500,3900]]]], //条件限制
['$group' => ['_id' => '$app_id','price_sum'=>['$sum'=>'$price'], 'app_num' => ['$sum' => 1]]], //分组,求和
['$project'=>['_id'=>1,'app_num'=>1,'price_sum'=>1]], //显示的字段
['$sort' => ['_id' => 1]], //排序
['$skip'=>2], //过滤条数
['$limit'=>5] //结果数量限制
] ,
['cursor'=>['batchSize'=>0]] //必填,游标参数,
);