0

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?

7
  • I think in Linq expression you can use ArrayList.Contains((string)bson[“symbol”]) function inside .Where or .Select.. Commented Jul 24, 2018 at 18:21
  • It doesn't change anything. I even used custom function with simple FOR loop inside. Commented Jul 25, 2018 at 1:45
  • Can you please try using db profiler to see the query, not everything in Linq can be translated especially to nosql language.. stackoverflow.com/a/10707634/3254405 Commented Jul 25, 2018 at 2:05
  • Although it would be better to serialize the query into a list and then use Linq stackoverflow.com/a/12076431/3254405 Commented Jul 25, 2018 at 2:16
  • 1
    I misspoke about serializing, may be it’s called desirializing.. Basically try doing what this guy is saying but in a nosql way.. stackoverflow.com/a/16533408/3254405 Commented Jul 26, 2018 at 14:23

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.