/// <summary> /// /// </summary> /// <param name="tag"></param> /// <returns></returns> public async Task <List <Post> > FindPostsByTag(string tag) { var builder = new FilterDefinitionBuilder <Post>(); // Builders<Post>.Filter.AnyIn(x => x.Tags, new List<string>() { tag }); FilterDefinition <Post> filter = null; if (!string.IsNullOrWhiteSpace(tag)) { filter = builder.AnyIn(x => x.Tags, new List <string>() { tag }); } else { filter = new BsonDocument(); } var listOfRecentPosts = await _postCollection.Find(filter) .Sort(Builders <Post> .Sort.Descending(x => x.CreatedAtUtc)) .ToListAsync(); return(listOfRecentPosts); }
//test public List <InfoBO> FindTargetByTag(Dictionary <string, List <string> > tagGroups) { if (tagGroups.Count() == 0) { return(GetInfos()); } IMongoCollection <InfoBO> collection = _client. GetDatabase(Configuration["MongoSetting:InfoSetting:Database"]) .GetCollection <InfoBO>(Configuration["MongoSetting:InfoSetting:Collection"]); List <InfoBO> ret = new List <InfoBO>(); FilterDefinitionBuilder <InfoBO> builder = new FilterDefinitionBuilder <InfoBO>(); List <FilterDefinition <InfoBO> > filters = new List <FilterDefinition <InfoBO> >(); foreach (var it in tagGroups) { string tagGroupName = it.Key; List <string> tagsInGroup = it.Value; Builders <InfoBO> .Filter.Exists(x => x.tags, true); var filter1 = builder.Exists(x => x.tags[tagGroupName], false); var filter2 = builder.AnyIn(r => r.tags[tagGroupName], it.Value); var filter = filter1 | filter2; filters.Add(filter); } MongoDB.Driver.FilterDefinition <InfoBO> finalFiter = filters.Count == 1 ? filters[0] : builder.And(filters); ret = collection.Find(finalFiter).ToList(); return(ret); }
public object AnyIn <TItem>(string arrayProperty, IEnumerable <TItem> items) { return(InternalBuilder.AnyIn(arrayProperty, items)); }
private IEnumerable <SyncTransactionAddressItem> SelectAddressWithPool(SyncBlockInfo current, string address, bool availableOnly) { FilterDefinitionBuilder <MapTransactionAddress> builder = Builders <MapTransactionAddress> .Filter; var addressFiler = new List <string> { address }; FilterDefinition <MapTransactionAddress> filter = builder.AnyIn(transactionAddress => transactionAddress.Addresses, addressFiler); if (availableOnly) { // we only want spendable transactions filter = filter & builder.Eq(info => info.SpendingTransactionId, null); } watch.Restart(); SortDefinition <MapTransactionAddress> sort = Builders <MapTransactionAddress> .Sort.Descending(info => info.BlockIndex); var addrs = MapTransactionAddress.Find(filter).Sort(sort).ToList(); watch.Stop(); log.LogInformation($"Select: Seconds = {watch.Elapsed.TotalSeconds} - UnspentOnly = {availableOnly} - Addr = {address} - Items = {addrs.Count()}"); // this creates a copy of the collection (to avoid thread issues) ICollection <Transaction> pool = MemoryTransactions.Values; if (pool.Any()) { // mark trx in output as spent if they exist in the pool List <MapTransactionAddress> addrsupdate = addrs; GetPoolOutputs(pool).ForEach(f => { MapTransactionAddress adr = addrsupdate.FirstOrDefault(a => a.TransactionId == f.Item1.PrevOut.Hash.ToString() && a.Index == f.Item1.PrevOut.N); if (adr != null) { adr.SpendingTransactionId = f.Item2; } }); // if only spendable transactions are to be returned we need to remove // any that have been marked as spent by a transaction in the pool if (availableOnly) { addrs = addrs.Where(d => d.SpendingTransactionId == null).ToList(); } // add all pool transactions to main output var paddr = PoolToMapTransactionAddress(pool, address).ToList(); addrs = addrs.OrderByDescending(s => s.BlockIndex).Concat(paddr).ToList(); } // map to return type and calculate confirmations return(addrs.Select(s => new SyncTransactionAddressItem { Address = address, Index = s.Index, TransactionHash = s.TransactionId, BlockIndex = s.BlockIndex == -1 ? default(long?) : s.BlockIndex, Value = s.Value, Confirmations = s.BlockIndex == -1 ? 0 : current.BlockIndex - s.BlockIndex + 1, SpendingTransactionHash = s.SpendingTransactionId, SpendingBlockIndex = s.SpendingBlockIndex, CoinBase = s.CoinBase, CoinStake = s.CoinStake, ScriptHex = new Script(Encoders.Hex.DecodeData(s.ScriptHex)).ToString(), Type = StandardScripts.GetTemplateFromScriptPubKey(new Script(Encoders.Hex.DecodeData(s.ScriptHex)))?.Type.ToString(), Time = s.BlockIndex == -1 ? UnixUtils.DateToUnixTimestamp(DateTime.UtcNow) : current.BlockTime })); }