[CSHARP-4625] Find Method on collection object is not working. Saying 'MongoDB.Driver.MongoCollectionBase<MongoDB.Bson.BsonDocument>' does not contain a definition for 'Find' Created: 22/Apr/23  Updated: 27/Oct/23  Resolved: 01/May/23

Status: Closed
Project: C# Driver
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Question Priority: Trivial - P5
Reporter: manikanta punnam Assignee: Robert Stam
Resolution: Works as Designed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Documentation Changes Summary:

1. What would you like to communicate to the user about this feature?
2. Would you like the user to see examples of the syntax and/or executable code and its output?
3. Which versions of the driver/connector does this apply to?


 Description   

Summary

Find Method on collection object is not working. Saying 'MongoDB.Driver.MongoCollectionBase<MongoDB.Bson.BsonDocument>' does not contain a definition for 'Find'

Please provide the version of the driver. If applicable, please provide the MongoDB server version and topology (standalone, replica set, or sharded cluster).

Driver version --->  2.19.1
we are using Azure Cosmos DB for MongoDB account(RU) and server version 4.2

How to Reproduce

1. create Azure cosmost DB for MongoDB account(RU). version should be 4.2
2. create a sample db and collection name.
3. do write the following similar code,
 __ 
using MongoDB.Driver;
using MongoDB.Bson;
var client = new MongoClient(connectionString);
var database = client.GetDatabase("teacher_profile");
var collection = database.GetCollection<BsonDocument>("chapters");
var filter = Builders<BsonDocument>.Filter.Eq("_id", ObjectId.Parse("64439214f877fcad42b31c38"));
 var result = collection.Find(filter).FirstOrDefault();
 Console.WriteLine(result);

Additional Background

Please provide any additional background information that may be helpful in diagnosing the bug.



 Comments   
Comment by Robert Stam [ 01/May/23 ]

If you choose not to use var and instead choose to (or need to) declare the type explicitly I recommend you use `IMongoColection<TDocument>` as the type of the variable.

Comment by manikanta punnam [ 25/Apr/23 ]

I found what was the problem.

In the above code, I have not declared the collection object before assigning the result of database.GetCollection<BsonDocument>("chapters"); 
If i follow this approach it assigning the "CollectionBase<BsonDocument>" type to "collection" object. As a result getting the above mentioned error.

But when i declared the collection variable with "IMongoCollection<BsonDocument>" type before doing assignment like below, it starts working.

private static IMongoCollection<BsonDocument> collection;
collection = database.GetCollection<BsonDocument>("chapters");

I am new to statically typed languages like C#. I am not sure if this is an issue or not in this case.

Please rectify if that is the case.

Thanks.

Comment by Robert Stam [ 23/Apr/23 ]

I tried to reproduce the error you described and did not get an error.

Note that the MongoCollectionBase class mentioned in your error message is an internal implementation detail. None of our public APIs return a value of type MongoCollectionBase. The call to database.GetCollection<BsonDocument> returns a value of type IMongoCollection<BsonDocument>, and the Find method is an extension method on IMongoCollection.

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