I'm having trouble translating a query (relatively simple) from Mongo Shell to C# (can be linq or filter builder).
I want to query by some field(in my example EntityId and ActivityDate) and by fields that exist on array(in my example UnCommonFields).
Example of data:
{
"_id" : ObjectId("5ded105e90424e1c0e6fda05"),
"EntryId" : NumberLong(1111111),
"EntityId" : "10000007",
"EntityType" : NumberInt(5),
"ActivityItemId" : "1,4,16172532",
"ActivityDate" : ISODate("2015-04-13T03:50:55.000-0400"),
"CreateDate" : ISODate("2017-12-13T19:07:07.757-0500"),
"UpdateDate" : ISODate("2017-12-13T19:07:07.757-0500"),
"ActivityItemStatus" : NumberInt(2),
"ActivityTypeId" : NumberInt(2),
"Currency" : "USD",
"Amount" : "-10.7800",
"ActivityItemDescription" : "Stackoverflow example",
"DisplayLevel" : NumberInt(10),
"UnCommonFields" : [
{
"k" : "CardPresent",
"v" : NumberLong(2)
},
{
"k" : "PreAuthDate",
"v" : ISODate("2015-04-11T08:47:05.000-0400")
},
{
"k" : "TransactionDate",
"v" : ISODate("2015-04-13T03:50:55.000-0400")
},
{
"k" : "ActivityItemStatus",
"v" : NumberLong(2)
},
{
"k" : "MerchantName",
"v" : "StackOverflow POS"
},
{
"k" : "TerminalCity",
"v" : "TLV"
},
{
"k" : "TerminalState",
"v" : "IL"
},
{
"k" : "TerminalCountry",
"v" : "IL"
},
{
"k" : "Fee",
"v" : 0.0
},
{
"k" : "IsDecline",
"v" : false
},
{
"k" : "BillingAmount",
"v" : -10.78
},
{
"k" : "BillingCurrency",
"v" : "USD"
},
{
"k" : "ForeignAmount",
"v" : -10.78
},
{
"k" : "ForeignCurrency",
"v" : "USD"
},
{
"k" : "FxRate",
"v" : 1.0
},
{
"k" : "MaskedCardNumber",
"v" : "530000XXXXXX6699"
}
]
}
The query that I want to write look like this on Mongo shell:
db.getCollection("ActivityWriterTest").find(
{$and:
[
{EntityId:"10000007"},
{ ActivityDate: { $gte: ISODate("2015-04-12T03:50:55.000"), $lte: ISODate("2015-04-14T03:50:55.000") } },
{UnCommonFields : { $elemMatch : {"k" : "ForeignAmount", "v" : {$in: [-10.78, -15.85] } }}}
]
})
This is the part that I wrote on my C# code: It's include two first filter and it's work as expected
_activityContext.ActivityData.Find(x => x.EntityId == accountHolderId && x.ActivityDate >= requestDetails.FromDate && x.ActivityDate <= requestDetails.ToDate)
I try to implement the elemmactch in several way, but not success.. Any idea how I can translate and integrate the elemmatch as part of my query?
I use MongoDB driver 2.10