Complex Query in Mongodb using driver (subtraction of dates and mod)

I want to write a Query like this:

Builders<Test>.Filter.Mod(s=> (s.StartDateTime - requiredDate).Days, s.frequence, 0)

this is not working.

Also a query like this:

s => DateTime.DaysInMonth(s.StartDateTime.Year, s.StartDateTime.Month) - s.LastDays < requiredDate.Day

This is not working too.

Both are giving me “Not supported” error.

Is there any equivalent way of querying such thing?

Hi, @Said_Amir,

Welcome to the MongoDB Community! You appear to be running into issues defining modulo filter definitions. The Mod method cannot accept a field definition for either the modulus or remainder argument, both must be of type long:

public FilterDefinition<TDocument> Mod(
	Expression<Func<TDocument, Object>> field,
	long modulus,
	long remainder
)

Our LINQ provider unfortunately does not support this construction either.

If you need to specify a field definition for the modulus parameter, you will have to write the FilterDefinition using MQL and use the implicit conversion operator from JSON:

FilterDefinition<BsonDocument> filter = "{ x: 1 }"; // <-- Your MQL would go here

You can file a request for this feature to be supported in our builders and/or LINQ.

Sincerely,
James

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.