private static FieldsBuilder ParseProjection(string projection) { var fields = new FieldsBuilder(); if (String.IsNullOrEmpty(projection)) return fields; var projectionDocument = BsonSerializer.Deserialize<BsonDocument>(projection); foreach (var element in projectionDocument) { var value = element.Value; if (value.IsBoolean && value.AsBoolean || value.IsInt32 && value.AsInt32 != 0) { fields.Include(element.Name); } else if (value.IsBoolean && !value.AsBoolean || value.IsInt32 && value.AsInt32 == 0) { fields.Exclude(element.Name); } else { throw Errors.InvalidProjectionFormat(); } } return fields; }
/// <summary> /// 更新值 /// </summary> /// <param name="pid"></param> /// <param name="filed"></param> /// <param name="inc"></param> /// <param name="check">不能扣负为true,可以扣负为false</param> /// <returns></returns> public BsonDocument SafeUpdate(int pid, string filed, int inc, bool check = true) { if (inc != 0) { var query = Query.EQ("_id", pid); var update = MongoDB.Driver.Builders.Update.Set("Modified", DateTime.UtcNow).Inc("Ver", 1); update.Inc(filed, inc); if (check && inc < 0) { query = Query.And(query, Query.GTE(filed, -inc)); } FieldsBuilder fb = new FieldsBuilder().Include(filed); var pf = m_collection.FindAndModify(query, null, update, fb, true, false); if (pf != null && pf.Ok) { return pf.ModifiedDocument; } } return null; }
public IEnumerable<AggregatedValue> Find(DateTime startDate, DateTime endDate, Dictionary<string, List<string>> props) { MongoCollection<StoredCounter> items = MongoDb.GetCollection<StoredCounter>("countersData"); IMongoQuery q = Query.And(Query.GTE("date", startDate), Query.LT("date", endDate)); FieldsBuilder fields = new FieldsBuilder(); //fields.Include("date", "data.Max", "data.Min", "data.Count", "data.Avg", "data.Sum"); fields.Include("date", "data"); foreach (KeyValuePair<string, List<string>> prop in props) { string fieldName = "props." + prop.Key; fields.Include(fieldName); if (prop.Value.Count == 0) q = Query.And(q, Query.Exists(fieldName)); else if (prop.Value.Count == 1) q = Query.And(q, Query.EQ(fieldName, prop.Value.First())); else q = Query.And(q, Query.In(fieldName, prop.Value.Cast<BsonString>())); } MongoCursor cursor = items.Find(q); cursor.SetFields(fields); cursor.SetSortOrder(SortBy.Ascending("date")); Stopwatch sw = Stopwatch.StartNew(); var found = cursor.Cast<object>().ToList(); sw.Stop(); Console.WriteLine( found.Count + " entries fetched from db in " + sw.ElapsedMilliseconds + " ms"); sw.Restart(); var parsed = found.Cast<StoredCounter>().Select(ParseAggregatedValue).ToList(); sw.Stop(); Console.WriteLine(parsed.Count + " entries parsed in " + sw.ElapsedMilliseconds + " ms"); sw.Restart(); var result = parsed.Compact().ToList(); sw.Stop(); Console.WriteLine(found.Count + " entries compacted to " + result.Count + " in " + sw.ElapsedMilliseconds + " ms"); return result; }
int GetPlayerID(string tokenID) { var query = Query.EQ("_id", tokenID); var update = Update.Inc("Ver", 1); FieldsBuilder fb = new FieldsBuilder().Include("pid"); var pf = m_collection.FindAndModify(query, null, update, fb, true, false); if (pf != null && pf.Ok && pf.ModifiedDocument != null) { BsonValue vaule; if (pf.ModifiedDocument.TryGetValue("pid", out vaule)) { return (int)vaule; } } return 0; }
/// <summary> /// 补全Order信息 /// (状态为0时才可以补全) /// </summary> /// <param name="log"></param> /// <param name="newState">补全后状态设置新值,默认为11</param> /// <returns>角色ID</returns> public int Replenish(Order log, int newState) { var query = Query.And(Query.EQ("_id", log.token), Query.EQ("state", 0)); var update = Update.Set("amt", log.amt).Set("billno", log.billno) .Set("openid", log.openid).Set("payitem", log.payitem).Set("ppc", log.ppc) .Set("providetype", log.providetype).Set("sig", log.sig).Set("ts", log.ts) .Set("zoneid", log.zoneid).Inc("state", newState); FieldsBuilder fb = new FieldsBuilder().Include("pid"); var pf = m_collection.FindAndModify(query, null, update, fb, true, false); if (pf != null && pf.Ok && pf.ModifiedDocument != null) { BsonValue vaule; if (pf.ModifiedDocument.TryGetValue("pid", out vaule)) { return (Int32)vaule; } } //记录已补全.直接获取 return GetPlayerID(log.token); }
public static IMongoFields ObjectsToFields(IList<object> values) { if (values == null) return null; IMongoFields fields; if (values.Count == 1 && (fields = values[0] as IMongoFields) != null) return fields; var builder = new FieldsBuilder(); foreach (var it in values) { var name = it as string; if (name != null) { builder.Include(name); continue; } throw new ArgumentException("Property: Expected either one IMongoFields or one or more String."); } return builder; }
public QuerySettings( ) { Fields = new FieldsBuilder( ); Limit = 0; }
PlayerExAccess() : base("PlayerEx") { excludeFields = new FieldsBuilder(); excludeFields.Exclude("Ver", "Modified"); }