总结一下当前thinkphp开发的项目中需要操作mongo:
以下是三种tp中操作mongo数据的方法:
使用tp中的扩展,方法一
Db::connect('dataname')->table('dbname')->insertAll($list); $info = $connection->getTableInfo('collection');
后面接着就可以任意使用db下的增删改查
db.users.find(); //查询某个集合的所有记录
db.users.distinct("name"); //滤掉 name 中的相同数据
db.users.find({"age": 22}); //查询 age = 22 的记录
db.users.find({age: {$gt: 22}}); //查询 age 大于 22 的记录
db.users.find({age: {$gte: 22}}); //查询 age 大于等于 22 的记录
db.users.find({age: {$lt: 22}}); //查询 age 小于 22 的记录
db.users.find({age: {$lte: 22}}); //查询 age 小于等于 22 的记录
db.users.find({age: {$gte: 23, $lte: 25}}); //查询 age >= 23 并且 age <= 25 注意书写格式
db.users.find({age: {$ne: 25}}); //查询 age != 25 的记录
db.users.find({name: /mongo/}); //查询 name 中包含 mongo 的数据 模糊查询用于搜索
相当于 select * from userswhere name like '%mongo%';
db.users.find({name: /^mongo/}); //查询 name 中以 mongo 开头的
相当于 select * from users where name like 'mongo%';
db.users.find({name: /mongo$/}); //查询 name 中以 mongo 结尾的
相当于 select * from users where name like ‘%mongo’;
db.users.find({}, {name: 1, age: 1}); //查询指定列 name、age 数据
相当于 select name, age from users;
db.users.find({age: {$gt: 25}}, {name: 1, age: 1});
相当于 select name, age from users where age > 25;
db.users.find().sort({age: 1}); //按照年龄排序 1 升序 -1 降序
db.users.find().sort({age: -1});
db.users.find().limit(5); //查询前 5 条数据
db.users.find().skip(10); //查询 10 条以后的数据
db.users.find().limit(10).skip(5); //查询在 6-10条 之间的数据,可用于分页,limit 是
pageSize,第n页时 skip 是 (n-1)*pageSize
相当于 select * from userslimit 5,5;
db.users.find({name: 'zhangsan', age: 22}); //and 查询 name = zhangsan, age = 22 的数据
db.users.find({$or: [{age: 22}, {age: 25}]}); //db.userInfo.find({$or: [{age: 22}, {age: 25}]}); // or 查询
db.users.find({age :{$in:[22,25]}}); //in 查询
db.users.find({age: {$gte: 25}}).count(); //查询某个结果集的记录条数 统计数量
skip(), limilt(), sort()三个放在一起执行的时候,执行的顺序是先 sort(), 然后是 skip(),最后是显示的 limit()。
db.users.find({createTime:{$gt:ISODate("2020-11-09T00:00:00Z")}});
//查询某个时间段的数据(时间为日期类型,非字符串类型)
db.userInfo.aggregate({$group:{_id:null,score:{$sum:"$score"}}})
//对表中一字段进行统计求和
db.userInfo.aggregate({$group:{_id:null,score:{$avg:"$score"}}})
//对表中一字段进行统计求平均值
db.userInfo.aggregate({$match:{createTime:{$gte:ISODate("2020-11-10T00:00:00Z"),$lt:ISODate("2020-11-11T00:00:00Z")}}},{$group:{_id:null,score:{$sum:"$score"}}})
//对表中指定条件记录中的某字段求和
关联查询
假设 有 用户集合, 存储的测试数据 如下:
db.userInfo.insert([
{ "_id" : 1, "userId" : "xxxx", "username" : "ruink", "website" : "www.51ste.com" },
{ "_id" : 2, "userId" : "yyyy", "username" : "foosingy", "website" : "www.scgossip.com" }
假设 有 地址集合, 存储的测试数据 如下:
db.userAdress.insert([
{ "_id" : 1, "userId" : "xxxx", address: "测试地址1"},
{ "_id" : 2, "userId" : "yyyy", address: "测试地址2"},
{ "_id" : 3, "userId" : "xxxx", address: "测试地址3"},
])
db.userInfo.aggregate([
{
$lookup:
{
from: "userAdress",
localField: "userId",
foreignField: "userId",
as: "address_detail"
}
},
{ $match : {"userId" :"xxxx"} }
])
上表为找出userId="xxxx"的所有地址的集合,查询结果如下:
[
{
_id: 1,
userId: 'xxxx',
username: 'ruink',
website:'www.51ste.com',
address_docs: [
{
_id: 1,
userId: 'xxxx',
address: '测试地址1'
},
{
_id: 3,
userId: 'xxxx',
address: '测试地址3'
}
]
}
]
根据A表,匹配B表所有满足条件的集合,如根据用户表userInfo表中的userId字段找出userAdress表中所有地址的集合,其中userId也为userAdress中的字段。
])
使用tp中的db类,方法二
use think\mongo\Connection;
$connection = new Connection([
'hostname' => '10.10.10.10', // MongoDB服务器地址
'hostport' => 2017, // MongoDB服务器端口
'database' => 'chatname', // 数据库名称
'username' => "username",
'password' => "password",
]);
后面接着就可以任意使用Connection各类方法
使用MongoDB PHP驱动程序,方法三
$mongo = new Manager("mongodb://username:password@10.10.10.10:2017");
$database = "chatname";
$collection = new Collection($mongo, $database,"cname");
$indexes = $collection->listIndexes();//查看添加索引情况
后面接着就可以任意使用Collection各类方法