[CSHARP-814] String IsNullOrEmpty does not test for fields ignored if null Created: 05/Sep/13  Updated: 16/Jul/15  Resolved: 22/Apr/15

Status: Closed
Project: C# Driver
Component/s: Linq
Affects Version/s: 1.7.1
Fix Version/s: 2.0.1, 2.1

Type: Bug Priority: Minor - P4
Reporter: Andy Clapham Assignee: Craig Wilson
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Epic Link: Rewrite Linq

 Description   

String.IsNullOrEmpty(x.S) expands to
"$or" : [{ "S" :

{ "$type" : 10 }

},

{ "S" : "" }

]

If a field has BsonIgnoreIfNull, it should also test

{ "S" : null }

Here's a test illustrating this:

        public class WithString
        {
            [BsonIgnoreIfNull]
            public string S { get; set; }
        }
 
        [Test]
        public void StringIsNullOrEmptyShouldTestIgnoredNulls()
        {
        
            var a = new WithString() {S = "ooo"};
            var b = new WithString() {S = ""};
            var c = new WithString() { };
 
            collection.Insert(a);
            collection.Insert(b);
            collection.Insert(c);
 
            var query = from t in collection.AsQueryable()
                        where string.IsNullOrEmpty(t.S)
                        select t;
 
            var translated = MongoQueryTranslator.Translate(query);
            var mongoQuery = ((SelectQuery)translated).BuildQuery();
            Debug.WriteLine(mongoQuery.ToString());
 
            var results = query.ToList();
            Assert.That(results.Count, Is.EqualTo(2));
        }



 Comments   
Comment by Githook User [ 22/Apr/15 ]

Author:

{u'username': u'craiggwilson', u'name': u'Craig Wilson', u'email': u'craiggwilson@gmail.com'}

Message: CSHARP-814: changed the behavior of IsNullOrEmpty to allow matching
documents where a field doesn't exist in addition to where that field is
null. Also added support into the aggregation framework language
translator.
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/a65b7a584d96bab1fa92ed1c0ec66f01541a0b99

Comment by Andy Clapham [ 05/Sep/13 ]

Yes we're doing just that, however it would be nice to have this built in.

Comment by Craig Wilson [ 05/Sep/13 ]

It does appear that we have enough information to do this. There seems to be a simple workaround for now, simply use (s == null || s == string.Empty).

Generated at Wed Feb 07 21:37:54 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.