当前位置:Gxlcms > 数据库问题 > asp.net的mongodb实例

asp.net的mongodb实例

时间:2021-07-01 10:21:17 帮助过:2人阅读

public class MongoNearby 2 { 3 private static object objLock = new object(); 4 private static MongoDatabase mongo ; 5 6 private static MongoDatabase Instance 7 { 8 get 9 { 10 if (mongo == null) 11 { 12 lock (objLock)//防止多线程构造 13 { 14 string connctionString = "mongodb://192.168.1.x:27017"; 15 MongoClient client = new MongoClient(connctionString); 16 MongoServer server = client.GetServer(); 17 mongo = server.GetDatabase("nearby");//数据库 18 } 19 } 20 return mongo; 21 } 22 } 23 24 /// <summary> 25 /// 保存记录 26 /// </summary> 27 /// <param name="info"></param> 28 /// <returns></returns> 29 public static bool SaveNearby(NearInfo info) 30 { 31 if (info == null) 32 return false; 33 try 34 { 35 MongoCollection collection = Instance.GetCollection<NearInfo>("userinfo"); 36 QueryDocument query = new QueryDocument(); 37 query.Add("userid", info.userid); 38 collection.Remove(query); 39 40 WriteConcernResult result = collection.Insert(info); 41 CLog.WriteLog("write: "+info.ToJson()); 42 return result.Ok; 43 } 44 catch(Exception ex) 45 { 46 CLog.WriteLog(ex.Message+info.ToJson()); 47 return false; 48 } 49 } 50 51 /// <summary> 52 /// 附近用户 53 /// </summary> 54 /// <param name="sex"></param> 55 /// <param name="longitude"></param> 56 /// <param name="latitude"></param> 57 /// <param name="total"></param> 58 /// <param name="limit"></param> 59 /// <param name="maxDistance"></param> 60 /// <returns></returns> 61 public static List<UserInfo> GetNearbyUser(int myuid,int sex, double longitude, double latitude, ref int total, int limit = 1000, int maxDistance = 50*1000) 62 { 63 List<UserInfo> list = null; 64 try 65 { 66 list = new List<UserInfo>(); 67 double radius = 6378000; 68 MongoCollection<NearInfo> collection = Instance.GetCollection<NearInfo>("userinfo"); 69 70 #region 方法2 计算出了距离 71 IMongoQuery query = Query.NE("userid",myuid); 72 if (sex ==0 || sex == 1) { 73 query =Query.And( Query.NE("sex", 1-sex),Query.NE("userid",myuid)); 74 } 75 GeoNearOptionsBuilder options = GeoNearOptions.SetMaxDistance(maxDistance * 1.0 / radius).SetSpherical(true).SetDistanceMultiplier(radius); 76 var result = collection.GeoNear(query, longitude, latitude, limit, options); 77 78 var response = result.Response; 79 foreach (var item in response["results"].AsBsonArray) 80 { 81 BsonDocument bson = item.AsBsonDocument; 82 var dis = bson["dis"].AsDouble;//间隔的距离 83 var obj = bson["obj"].AsBsonDocument; 84 NearInfo info = BsonSerializer.Deserialize<NearInfo>(obj); 85 if (info == null) 86 continue; 87 88 UserInfo tmp = new UserInfo { 89 userid = info.userid, 90 dis = dis, 91 unick = CUser.GetUserNick(info.userid), 92 headimg = CUser.UserHeadImg(info.userid).Facemiddle, 93 usign = Getmemo(info.userid),sex=(info.sex ==1?"":"") 94 }; 95 list.Add(tmp); 96 total++; 97 //CLog.WriteLog(info.sex.ToString()); 98 } 99 CLog.WriteLog(myuid+"Read: "+result.Response.ToJson()); 100 #endregion 101 //CLog.WriteLog(list.ToJson()); 102 } 103 catch (Exception ex){ 104 CLog.WriteLog(ex.Message); 105 } 106 return list; 107 } 108 109 public static string Getmemo(int userid) 110 { 111 string memo = string.Empty; 112 string _tmp = string.Empty; 113 int _tmp1 = 0; 114 CUser.GetUserDetail(userid, out _tmp1, out _tmp, out _tmp1, out _tmp, out _tmp, out memo); 115 return memo; 116 } 117 }

 博客园刚开始用,不知道怎么保留文件...

asp.net的mongodb实例

标签:

人气教程排行