[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? |
| Description |
| 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"); But when i declared the collection variable with "IMongoCollection<BsonDocument>" type before doing assignment like below, it starts working. private static IMongoCollection<BsonDocument> collection; 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. |