-
Type:
Bug
-
Resolution: Works as Designed
-
Priority:
Major - P3
-
None
-
Affects Version/s: 2.9.2
-
Component/s: LINQ
-
None
-
None
-
None
-
None
-
None
-
None
-
None
-
None
Comparing a property to false using the Equality Comparer gives different results to using the Logical Negation Operator on the property.
See the following code snippet...
using Mongo2Go;
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Conventions;
using MongoDB.Driver;
using MongoDB.Driver.Linq;namespace ConsoleApp11
{
class Program
{
static void Main(string[] args)
{
using (MongoDbRunner mongoRunner = MongoDbRunner.Start())
{
// Ignore the default values. So a false boolean will not be stored in Mongo
ConventionRegistry.Register("Ignore default values", new ConventionPack { new IgnoreIfDefaultConvention(true) }, t => true);
var mongoClient = new MongoClient(mongoRunner.ConnectionString);
var mongoDb = mongoClient.GetDatabase("TestDb");
var collection = mongoDb.GetCollection<SimpleDocument>("TestCollection");
var documentTwo = new SimpleDocument
{
Property = false
}; collection.InsertOne(documentTwo);
var query1 = collection.AsQueryable().Where(x => x.Property == false).ToList(); // Doesn't find the record
var query2 = collection.AsQueryable().Where(x => !x.Property).ToList(); // Finds the record
}
}
} public class SimpleDocument
{
public ObjectId Id { get; set; }
public bool Property { get; set; }
}
}
I would have expected both query1 and query2 to find the record.