public async Task <LockResponse> RetrieveLatestInCategoryAsync( IEnumerable <string> categoryNames, bool isReadonly) { var fb = new FilterDefinitionBuilder <Lockable>(); var filter = fb.And( fb.All(l => l.Categories, categoryNames), fb.Or( $"{{ \"Locks.IsShared\": {isReadonly.ToString().ToLower()} }}", "{ Locks: { $size: 0 } }")); var lockable = await collection .Find(filter) .FirstOrDefaultAsync(); if (lockable == null) { return(null); } var @lock = new Lock { IsShared = isReadonly, MaxLeaseDate = DateTime.Now.AddHours(1), OwnerToken = null, UnlockedAt = default(DateTime) }; var response = await LockAsync(lockable, @lock); return(response); }
public object All <TItem>(string arrayProperty, IEnumerable <TItem> items) { return(InternalBuilder.All(arrayProperty, items)); }
// // 摘要: // Tests that the named array element contains all of the values (see $all). // // 参数: // name: // The name of the element to test. // // values: // The values to compare to. // // 返回结果: // The builder (so method calls can be chained). public static FilterDefinition <BsonDocument> All(string name, params BsonValue[] values) { var condition = filter.All(name, values); return(condition); }
public void BuildQuerySimple() { var client = new MongoClient("mongodb://localhost:27017"); var db = client.GetDatabase("hra"); var items = db.GetCollection<BsonDocument>("incumbent"); var fd = new FilterDefinitionBuilder<BsonDocument>(); var filter1 = fd.All("Job_Family", new List<string> {"Health Care"}); IAggregateFluent<BsonDocument> dd = items.Aggregate(); IAggregateFluent<BsonDocument> ts = dd.Match(filter1); var ll = ts.Group(x => new {Year = x["Year"], Org = x["org_type"]}, g => new {Key=g.Key, Average=g.Average(x => (double) x["Base_Pay"])}); ll.Project(x => new BsonDocument {new BsonElement("Key",x.Key.Year), new BsonElement("Average",x.Average)}); //ts.Group() //IAggregateFluent<BsonDocument> gd = ts.Group(x => new {s1 = x["Year"], s2 = x["org_type"]}, // g => new {Key = g.Key, Value = g.Average(x => (double) x["Base_Pay"])}).Project(x => new {Key = new List<string>{x.Key.s1.ToString()}, Value = x.Value}); //dd.Match(filter1) //.Group(x => new {s1 = x["Year"], s2 = x["org_type"]}, // g => new {Key = g.Key, Value = g.Average(x => (double) x["Base_Pay"])}) //.Project(x => new {Key = new List<string>{x.Key.s1.ToString()}, Value = x.Value}); //var dc = new List<IPipelineStageDefinition>(); //dc.AddRange(dd.Stages); //var pl = dc.ToArray(); //var zito = pl.ToJson(); //var docs = items.AggregateAsync<BsonDocument>(dc); var zz = ll.ToListAsync().Result; Assert.IsTrue(zz.Count > 0); var pp = "cc"; }
public void BuildQuery() { var source = GetQueryBuilder(); var jobFamily = source.AvailableFilters.Single(x => x.Column.ColumnName == "Job_Family"); var jobTrack = source.AvailableFilters.Single(x => x.Column.ColumnName == "Job_Track"); var matchBuilder = new FilterDefinitionBuilder<BsonDocument>(); jobFamily.AvailableFilterValues.Add(new FilterValue {Key = "Executive", Value = "Executive", Active=true}); var jobFamilyFilter = matchBuilder.All(jobFamily.Column.ColumnName, jobFamily.AvailableFilterValues.Where(x=>x.Active).Select(x=>x.Value).ToList()); //take care of grouping key var groupDoc = new BsonDocument(); var slicers = new List<BsonElement>(); slicers.Add(new BsonElement("s1", new BsonString("$Year"))); slicers.Add(new BsonElement("s2", new BsonString("$orgType"))); var idDoc = new BsonDocument(slicers); var idElm = new BsonElement("_id", idDoc); var idDocument = new BsonDocument(); idDocument.Add(idElm); //take care of grouping val var factDoc = new BsonDocument(); var factEl = new BsonElement("$avg", new BsonString("$Base_Pay")); factDoc.Add(factEl); var factElementDoc = new BsonDocument(); factElementDoc.Add(new BsonElement("f1", factDoc)); var factDocResult = new BsonDocument(); factDocResult.AddRange(factElementDoc); groupDoc.AddRange(idDocument); groupDoc.AddRange(factDocResult); var groupElement = new BsonElement("$group", groupDoc); var matchDoc = new BsonDocument(); var filter1Element = new BsonElement("Job_Family", new BsonString("Executive")); var filter1Doc = new BsonDocument(); filter1Doc.Add(filter1Element); var filterFinalElement = new BsonElement("$match", filter1Doc); var finalDoc = new BsonDocument(); finalDoc.Add(filterFinalElement); finalDoc.Add(groupElement); var dd = finalDoc.ToString(); Console.Out.Write(dd); var xx = "Y"; // jobTrack. }