C# collection.Find() method returning null every time

General issue:
-Any searches on documents within a collection seems to return null. Searches for DB’s and/or collections work just fine though.

Context:
-DB version: 3.4.5
-C# Driver version 2.4.2
> I am aware these are ancient but I have little power to update these at this time.
-Programming Language: C#

Problem details:
-Tried the following so far to no avail:
-Attempt 1:

 var documentsTest = collectionOfInterest.Find(new BsonDocument()).FirstOrDefault(); 

-Attempt 2:

           var builder = Builders<BsonDocument>.Filter;
           var newFunFilter = builder.Eq("IsSmoke", true);
           var list = await collectionOfInterest.Find(newFunFilter).ToListAsync();
           var result = list.FirstOrDefault();

-Attempt 3:

        List<TestSet> TestSetObjList = new List<TestSet>();
            foreach(var collection in collectionsList)
            {
                //Traverse
                foreach (var element in collection)
                {
                    funShitString.Add(selectedTestSet.ToString());
                }               
            }

Hey @Jon_S , in case if your first attempt returns null, then you query an empty collection. I see 2 options, you either specify wrong db/collection names or server address. You can validate it if you try connecting to your database from the shell (with the same configuration as your c# application) and call the same db.coll.find({}) method

Thank you for your quick response. I am checking the connection and server address and I am able to get the list of the collections just before trying to select the documents to get from that collection and they all show as expected. Once I try to get document information though I just get null back every time. Here is the code I am using.

            //Get collection selected
            //Works as expected
            var collectionsList = DatabaseDesired.ListCollections().ToList();
            var collectionNameSpace = DatabaseDesired.GetCollection<BsonDocument>(selectedTestSet).CollectionNamespace;          
            var collectionOfInterest = DatabaseDesired.GetCollection<BsonDocument>(selectedTestSet);
            var collectionOfInterestNonBsonDoc = DatabaseDesired.GetCollection<TestSet>(selectedTestSet);

 

            //Attempt to get documents from a given collection
            //Fails every time
            var documentsTest = collectionOfInterest.Find(new BsonDocument()).FirstOrDefault();

            var builder = Builders<BsonDocument>.Filter;

            var newFunFilter = builder.Eq("IsSmoke", true);

            var list = await collectionOfInterest.Find(newFunFilter).ToListAsync();

            var result = list.FirstOrDefault();

 
            //Always caught here
            if (documentsTest.Equals(null))
           {
              
                MessageBox.Show("No documents found");

                System.Threading.Thread.Sleep(10000);

                this.Close();

            }

It doesn’t say much.
if this line returns var collectionsList = DatabaseDesired.ListCollections().ToList(); collections you expect in further steps, then that collections are empty. Instead Find, you can just call:

collectionNameSpace.CountDocuments();

I appreciate you sharing this option. Still new to the MongoDB space so everything i can learn is good! Unfortunately the documents number I am getting back is 0. That makes no sense to me though as I am staring at this collection in Studio3T and it has 50 documents in it.

I have even paused execution and checked the database connection string to compare the server URL, username, and everything else I can and they all match. This code is tied to a UI that has a dropdown of DB’s and collections that even update on new DB selection without issue.

Only thing that still stands out as odd to me is that in the dB client settings there is a field called “isFrozen” that is set to true. Could this have anything to do with it?

if you mean IsFrozen field in MongoClientSettings, then no, this option is indicator that underlying settings are in readonly mode so you can’t modify them

Ok, from the sounds of it that is all it was then…

So any idea how I could be looking at documents in Studio3T and collections all match and are referenceable but I would not be able to get the associated documents then?!