I am trying to find a particular user from a mongodb collection that matches the given id. Folliwng is my VB.Net code. However, I keep getting the error 'Public member 'Find' on type 'MongoCollectionImpl(Of BsonDocument)' not found.'
Public Function GetCollectionByName(ByVal collectionName As String)
Dim db As IMongoDatabase = DBcontext()
Dim collection As IMongoCollection(Of BsonDocument)
collection = db.GetCollection(Of BsonDocument)(collectionName)
Return collection
End Function
Public Function GetUser(ByVal id As String)
Dim filter = Builders(Of BsonDocument).Filter.Eq(Of String)("ID", id)
Dim collection = GetCollectionByName("Users")
Dim list = collection.Find(filter).ToList()`<<<<<<<<<<<<<<<<<<<<<<<<<<<<<ERROR here
Return list
End Function
Firstly, you must have
Option Strict Off
for that code to even compile. That's bad. You should immediately turnOption Strict On
in the project properties and address all the issues it raises. One of those will be the fact that yourGetCollectionByName
has no return type declared. That means that here:that
collection
variable is implicitly typeObject
and you are relying on late binding when calling thatFind
method because theObject
class has no such method. As a result, you get no help from Intellisense and Intellisense would have told you what members were and were not available if you were doing this properly.Regardless, you still could have made it work if you had actually read the documentation for the types you're using to see what members they have. Here is the documentation for the interface you're using in that
GetCollectionByName
method and, I don't know about you but I don't see anyFind
method listed there. There is aFindSync
method, so maybe that's what you actually want. If you hadOption Strict On
and used proper types every where, Intellisense would have shown you that.You should also turn
Option Strict On
in the IDE options, so that it isOn
for all future projects.I had a look at some documentation for the
MongoCollectionImpl
for Java and there appears to be afind
method there but that doesn't necessarily mean that the same method is available in .NET and you aren't working directly with that class anyway. You are working with theIMongoCollection
so you should only be working with members of that interface. Basically, your code would need to look more like the below withOption Strict On
:You may want to declare the
GetUser
method as typeIList(Of BsonDocument)
if you want to work with interfaces. You probably ought to rename that method or change the implementation too. If a method is returning a list then the name should not indicate that it returns a single item.