I need to request some data from a database using C# mongodb driver and filter it. I create a filter and do request like so
public async Task GetForexFeedForTimeSpan(long fromUTCUnixtime, long toUTCUnixtime, Action<List<SymbolData>> onComplete, string[] forSymbols, int limit = 0) {
List<SymbolData> list = new List<SymbolData>();
FilterDefinition<BsonDocument> filter
= Builders<BsonDocument>.Filter.Where(bson =>
forSymbols.Any(symbolName => bson["symbol"].AsString == symbolName) &&
bson["timestamp"] >= fromUTCUnixtime &&
bson["timestamp"] <= toUTCUnixtime
);
await GetCustomForexData(onComplete, filter, limit);
}
private async Task GetCustomForexData(Action<List<SymbolData>> onComplete, FilterDefinition<BsonDocument> filter, int limit = 0) {
List<SymbolData> list = new List<SymbolData>();
await m_SymbolsCollection
.Find(filter)
.Limit(limit)
.SortBy(bson => bson["timestamp"]).ForEachAsync(
bson => {
list.Add(SymbolData.FromBSON(bson));
});
onComplete?.Invoke(list);
}
Then I call it somewhere like this:
GetForexFeedForTimeSpan(fromUnixtime, toUnixtime, OnCompleteDelegate, new[]{ "EURUSD" });
The database itself really contains "symbol" keys. And if I change the filter to this
FilterDefinition<BsonDocument> filter
= Builders<BsonDocument>.Filter.Where(bson =>
(string)bson["symbol"]== "EURUSD" &&
bson["timestamp"] >= fromUTCUnixtime &&
bson["timestamp"] <= toUTCUnixtime
);
It will work and find all documents with a symbol equal to "EURUSD". But I need to pass a bunch of symbol names and select any coincidences
Why doesn't it work with an array? Or maybe somebody can advise a better solution?