索引
- 查看索引
db.Ttest.getIndexes();
#或
db.getCollection('Ttest').getIndexes();
- 新建索引
#给时间字段新建索引
db.Ttest.ensureIndex({"LTimeB":1})
查询
db.getCollection('Ttest').aggregate([{$match: {"_id": ObjectId("5a53136ae138237acab5c841")}},{$unwind:"$Z118005"},{$limit:10},{$skip:3}]).pretty();
db.getCollection('call_pool').aggregate([{$match: {"calls.category": "system"}},{$unwind:"$calls"}]).pretty()
$command = [['$unwind'=>'$calls'],['$match'=>['_id'=>$calls_id,'calls.category'=>$type]],['$limit'=>(int)$page]];
$data = $collection->aggregate($command);
db.getCollection('Ttest').find({"Z118005.100":{$exists:1}},{'_id':1});
db.Ttest.find({},{"Z118005":{"$slice":20}});
db.Ttest.find({"total":271},{"Z118005":{"$slice":5},'total':1});
db.Ttest.find({total: {"$in":[null],$exists: true}});
#not in 不等于 array
db.Ttest.find({total: {"$nin":[null],$exists: true}});
#不等于 one
db.Ttest.find( { total: { $ne : 3 } } );
- $mod 取模运算 查询 total取模 6 等于 1 的数据
db.ToperaSystem.find({total: {"$mod":[6,1]}});
db.ToperaSystem.find({total:{"$gte":100,"$lte":287}});
$where = [
['time'=>['$gte'=>$time_start, '$lte'=>$time_end]],
['_id'=>new MongoId($id)])]
];
$where = [
"$or"=>[
['time'=>['$gte'=>$time_start, '$lte'=>$time_end]],
['_id'=>new MongoId($id)])]
]
];
#$in必须满足[]中的任意一值则可。
db.Ttest.find({total: {"$in":[14,143]}});
#本人亲自实践 $in 和 $all的不同
# $all则必须满足[]中的所有值,简称完全匹配。
db.Ttest.find({total: {"$all":[14,143]}});
db.Ttest.find().sort({total: -1});
db.Ttest.find({"title":/^F5\\(v/})
$where = ['title' => ['$in' => [new Regex("$keyword", 'i')]]];
新增数据库用户
#新加一个superman的用户操作admin超级库的,拥有全部权限
db.createUser(
{
user: "superman",
pwd: "superman",
roles: [ { role: "readWriteAnyDatabase", db: "admin" }, { role: "dbAdminAnyDatabase", db: "admin" }]
}
)
#新建一个testuser用管理test数据库,拥有读写权限
db.createUser(
{
user: "testuser",
pwd: "testuser",
roles: [ { role: "readWrite", db: "test" }]
}
)
修改mongodb支持远程连接
#不知道配置文件在哪里的同学
find / -name "mongod.conf"
#cd到对应目录
vi mongod.conf
#找到net模块
修改bindIp=192.168.1.6 #修改127.0.0.1这个本地地址为网络ip
#这里简单提一下 万一同学忘记了登录db的用户名等
#这个文件的下面security模块 就有作用了
authorization: "disabled"
#修改完记得重启
service mongod restart
#应用
启动方式
./bin/mongod -f mongod.conf