// 票数统计 private Dictionary <string, long> getCurrentVoteCount(string projId, string proposalIndex) { var match = new JObject { { "$match", new JObject { { "projId", projId }, { "proposalIndex", proposalIndex } } } }.ToString(); var group = new JObject { { "$group", new JObject { { "_id", "$type" }, { "sum", new JObject { { "$sum", "$balance" } } } } } }.ToString(); var list = new List <string> { match, group }; var queryRes = mh.Aggregate(lConn.connStr, lConn.connDB, moloProjBalanceInfoCol, list); if (queryRes.Count == 0) { return(new Dictionary <string, long>()); } return(queryRes.ToDictionary(k => k["_id"].ToString(), v => long.Parse(v["sum"].ToString()))); }
private void GetCountAndUpdateByProjId(JObject findJo, string rColl, string lColl, string rKey) { var updateDict = new Dictionary <string, JObject>(); var list = new List <string>(); list.Add(new JObject { { "$match", findJo } }.ToString()); list.Add(new JObject { { "$group", new JObject { { "_id", "$projId" }, { "sum", new JObject { { "$sum", 1 } } } } } }.ToString()); var queryRes = mh.Aggregate(lConn.connStr, lConn.connDB, rColl, list); if (queryRes.Count > 0) { updateDict = queryRes.ToDictionary(k => k["_id"].ToString(), v => new JObject { { rKey, long.Parse(v["sum"].ToString()) } }); } if (updateDict.Count > 0) { var updateJa = updateDict.ToDictionary( k => new JObject { { "projId", k.Key } }.ToString(), v => new JObject { { "$set", v.Value } }.ToString() ); foreach (var item in updateJa) { mh.UpdateData(lConn.connStr, lConn.connDB, lColl, item.Value, item.Key); } } }
/// <summary> /// Run Aggregate /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void cmdRun_Click(object sender, EventArgs e) { if (_AggrArray.Count > 0) { CommandResult mCommandResult = MongoDBHelper.Aggregate(_AggrArray); if (mCommandResult.Ok) { MongoDBHelper.FillDataToTreeView("Aggregate Result", trvResult, mCommandResult.Response); trvResult.DatatreeView.BeginUpdate(); trvResult.DatatreeView.ExpandAll(); trvResult.DatatreeView.EndUpdate(); } else { MyMessageBox.ShowMessage("Aggregate Result", mCommandResult.ErrorMessage); } } }
private string[] getProjId() { var list = new List <string>(); list.Add(new JObject { { "$group", new JObject { { "_id", "$projId" }, { "sum", new JObject { { "$sum", 1 } } } } } }.ToString()); var res = mh.Aggregate(daoConn.connStr, daoConn.connDB, projFinanceHashCol, list); if (res.Count == 0) { return(null); } return(res.Select(p => p["_id"].ToString()).ToArray()); }
private void processProjTeam() { var match = new JObject { { "$match", new JObject { { "emailVerifyState", EmailState.sendBeforeStateAtInvited } } } }.ToString(); var lookup = new JObject { { "$lookup", new JObject { { "from", userInfoCol }, { "localField", "userId" }, { "foreignField", "userId" }, { "as", "us" } } } }.ToString(); var project = new JObject { { "$project", MongoFieldHelper.toReturn(new string[] { "projId", "userId", "emailVerifyState", "us.username", "us.email" }) } }.ToString(); var list = new List <string>(); var sort = new JObject { { "$sort", new JObject { { "time", 1 } } } }.ToString(); var limit = new JObject { { "$limit", 100 } }.ToString(); list.Add(match); list.Add(lookup); list.Add(sort); list.Add(limit); list.Add(project); var queryRes = mh.Aggregate(daoConn.connStr, daoConn.connDB, projTeamInfoCol, list); if (queryRes.Count > 0) { foreach (var item in queryRes) { string projId = item["projId"].ToString(); string userId = item["userId"].ToString(); string username = ((JArray)item["us"])[0]["username"].ToString(); string email = ((JArray)item["us"])[0]["email"].ToString(); string emailVerifyState = item["emailVerifyState"].ToString(); string vcode = UIDHelper.generateVerifyCode(); if (toMessage(emailVerifyState, username, email, projId, vcode, out string message, out string hasSendState)) { eh.send(message, email, true); var findStr = new JObject { { "projId", projId }, { "userId", userId } }.ToString(); var updateStr = new JObject { { "$set", new JObject { { "emailVerifyCode", vcode }, { "emailVerifyState", hasSendState }, { "lastUpdateTime", TimeHelper.GetTimeStamp() } } } }.ToString(); mh.UpdateData(daoConn.connStr, daoConn.connDB, projTeamInfoCol, updateStr, findStr); } } } }
private void handleProjStarCount() { // supprotCount + starCount + discussCount + updateCount string key = "projStarCount"; long lt = GetLTime(key); // string findStr = new JObject { { "lastUpdateTime", new JObject { { "$gt", lt } } } }.ToString(); string fieldStr = "{'projId':1,'lastUpdateTime':1}"; var queryRes = mh.GetData(daoConn.connStr, daoConn.connDB, projStarInfoCol, findStr, fieldStr); if (queryRes.Count == 0) { return; } long rt = queryRes.Max(p => long.Parse(p["lastUpdateTime"].ToString())); var ids = queryRes.Select(p => p["projId"].ToString()).Distinct().ToArray(); // int size = 100; int cnt = ids.Length; for (int skip = 0; skip < cnt; skip += size) { var idArr = ids.Skip(skip).Take(size).ToArray(); var findJo = idArr.toFilter("projId"); findJo.Add("lastUpdateTime", new JObject { { "$lte", rt } }); findJo.Add("starState", StarState.StarYes); var updateDict = new Dictionary <string, JObject>(); var list = new List <string>(); list.Add(new JObject { { "$match", findJo } }.ToString()); list.Add(new JObject { { "$group", new JObject { { "_id", "$projId" }, { "sum", new JObject { { "$sum", 1 } } } } } }.ToString()); queryRes = mh.Aggregate(daoConn.connStr, daoConn.connDB, projStarInfoCol, list); if (queryRes.Count > 0) { updateDict = queryRes.ToDictionary(k => k["_id"].ToString(), v => new JObject { { "starCount", long.Parse(v["sum"].ToString()) } }); } findJo.Remove("starState"); findJo.Add("supportState", StarState.SupportYes); list = new List <string>(); list.Add(new JObject { { "$match", findJo } }.ToString()); list.Add(new JObject { { "$group", new JObject { { "_id", "$projId" }, { "sum", new JObject { { "$sum", 1 } } } } } }.ToString()); queryRes = mh.Aggregate(daoConn.connStr, daoConn.connDB, projStarInfoCol, list); if (queryRes.Count > 0) { foreach (var item in queryRes) { var skey = item["_id"].ToString(); var sval = long.Parse(item["sum"].ToString()); if (updateDict.ContainsKey(skey)) { updateDict.GetValueOrDefault(skey)["supportCount"] = sval; } else { updateDict.Add(skey, new JObject { { "supportCount", sval } }); } } } if (updateDict.Count > 0) { var updateJa = updateDict.ToDictionary( k => new JObject { { "projId", k.Key } }.ToString(), v => new JObject { { "$set", v.Value } }.ToString() ); foreach (var item in updateJa) { mh.UpdateData(daoConn.connStr, daoConn.connDB, projInfoCol, item.Value, item.Key); } } } // UpdateLTime(key, rt); }