免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 14613 | 回复: 2

[MongoDB] asp.net的mongodb实例 [复制链接]

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

论坛徽章:
0
发表于 2015-06-26 00:13 |显示全部楼层
有没有想到过原文作者会出现在这里?

论坛徽章:
59
2015七夕节徽章
日期:2015-08-24 11:17:25ChinaUnix专家徽章
日期:2015-07-20 09:19:30每周论坛发贴之星
日期:2015-07-20 09:19:42ChinaUnix元老
日期:2015-07-20 11:04:38荣誉版主
日期:2015-07-20 11:05:19巳蛇
日期:2015-07-20 11:05:26CU十二周年纪念徽章
日期:2015-07-20 11:05:27IT运维版块每日发帖之星
日期:2015-07-20 11:05:34操作系统版块每日发帖之星
日期:2015-07-20 11:05:36程序设计版块每日发帖之星
日期:2015-07-20 11:05:40数据库技术版块每日发帖之星
日期:2015-07-20 11:05:432015年辞旧岁徽章
日期:2015-07-20 11:05:44
发表于 2015-07-08 21:30 |显示全部楼层
这个似乎是通用的类。和ASP。NET关系不大吧。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP