//主键控制
$config = C("MONGO_CONN"); //取链接配置
$mongo = new MongoClient($config);//链接
$db_name= C("MONGO_DB"); //取数据库配置
$db = $mongo->$db_name;//选定数据库
$ids = $db->ids;//选定ids 自增控制表 ids
//生成ids表 其他表自增主键控制表
$data = array();
$data['collection'] = 'locations';
$data['id'] = 0;
$ids->insert($data);
//自动累加
$where=array('collection'=>'locations'); //查询 collection=locations的表记录
//修改表中id字段自增并返回刚新增的值,做另一个表的主键 array('new'=>true)在这里控制返回更新后的值,如果不加array('new'=>true) 返回的是自增前的值
$locations=$ids->findAndModify($where,array('$inc'=>array('id'=>1)),array(),array('new'=>true));
if($locations['id']){
//id自增成功
$collection = $db->locations;
//当前点
$point = array();
$point['longitude'] = $post['lng'];
$point['latitude'] = $post['lat'];
$data = array();
$data['_id'] = $locations['id'];
$data['route_id'] = $post['route_id'];
$data['position'] = $point;
$data['time'] = date('Y-m-d H:i:s');
//生成一条新的坐标记录值 _id 用ids表刚生成的自增id
$result = $collection->insert($data);
}
调用mongoDB存储过程
//测试数据
$post['lng'] = 120.323066;
$post['lat'] = 40.989956;
$post['distance'] = 50000;
$post['limit'] = 50;
$db = $mongo->$db_name;
//查询条件
$query = 'return homeSearch('.$post['lng'].','.$post['lat'].','.$post['distance'].','.$post['limit'].')';
//相当于在命令行 调用
//db.eval("return homeSearch(120.323066, 40.989956, 50000, 50);");$list = $db->execute($query);
2个存储过程例子
/*
输入:经度,纬度,范围(米),返回结果总数
longitude, latitude, scope, count
返回:数组,包含起点经度和纬度,终点名称
results, query_start_point,end_point_name
*/
function (longitude, latitude, scope, count) {
var results;
if (longitude < 0) {
longitude = 0;
}
if (latitude < 0) {
latitude = 0;
}
if (scope < 0) {
scope = 0;
}
if (count <= 0) {
count = 1;
}
if (count > 100) {
count = 100;
}
results = db.routes.find({
"deleted": 0,
"query_start_point": {
$near: [longitude, latitude],
$maxDistance: scope
}
}, {
"query_start_point": true,
"end_point_name": true
}).limit(count).toArray();
return results;
}
//shell中执行db.eval("return homeSearch(120.323066, 40.989956, 50000, 50);");
/*
输入:查询起点经度和纬度、查询终点经度和纬度、范围(米)、查询出发时间前n个钟头、查询出发时间后n个钟头、当前页码、每页条数
queryStartPointLongitude、queryStartPointLatitude、queryEndPointLongitude、queryEndPointLatitude、
scope、queryStartTimeFront、queryStartTimeBack、pageNowNo、pageSize
返回:数组,包含与目标点距离和线路所有信息,总条数
query_start_point,end_point_name,count
*/
function (queryStartPointLongitude, queryStartPointLatitude, queryEndPointLongitude, queryEndPointLatitude, scope, queryStartTimeFront, queryStartTimeBack, pageNowNo, pageSize)
{
if (queryStartPointLongitude < 0) {
queryStartPointLongitude = 0;
}
if (queryStartPointLatitude < 0) {
queryStartPointLatitude = 0;
}
if (queryEndPointLongitude < 0) {
queryEndPointLongitude = 0;
}
if (queryEndPointLatitude <= 0) {
queryEndPointLatitude = 0;
}
if (scope < 0) {
scope = 0;
}
if (pageNowNo <= 0) {
pageNowNo = 1;
}
var distinctRouteIds = db.runCommand({
"distinct" : "routes",
"key" : "route_id",
"query" : {
"deleted" : 0,
"status" : {
$in : [1, 2]
},
"start_time" : {
$gte : ISODate(queryStartTimeFront),
$lte : ISODate(queryStartTimeBack)
},
"query_start_point" : {
$near : [queryStartPointLongitude, queryStartPointLatitude],
$maxDistance : scope
},
"query_end_point" : {
$near : [queryEndPointLongitude, queryEndPointLatitude],
$maxDistance : scope
}
},
"sort" : {
"seats_available" : -1,
"start_time" : 1,
"route_id" : 1
}
}).values;
var distanceRoutes = db.runCommand({
geoNear : "routes",
near : [queryStartPointLongitude, queryStartPointLatitude],
maxDistance : scope,
"query" : {
"route_id" : {
$in : distinctRouteIds.slice((pageNowNo-1)*pageSize,pageSize-1 )
}
}
}).results;
var results = new Array(distanceRoutes.length);
var distanceRoute;
var j;
for (var i = 0; i < distanceRoutes.length; i++) {
distanceRoute = distanceRoutes[i];
j = distinctRouteIds.indexOf(distanceRoute.obj.route_id);
if (results[j] != null) {
continue;
}
distanceRoute.count = distinctRouteIds.length;
results[j] = distanceRoute;
}
return results;
}
//db.eval("return routeSearch(120.323066, 40.989956, 120.323066, 40.989956,5, '2014-01-11T17:30:00Z', '2014-01-11T19:30:00Z',1, 3);");