'Public member 'Find' on type 'MongoCollectionImpl(Of BsonDocument)' not found.'

272 views Asked by At

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
1

There are 1 answers

1
jmcilhinney On BEST ANSWER

Firstly, you must have Option Strict Off for that code to even compile. That's bad. You should immediately turn Option Strict On in the project properties and address all the issues it raises. One of those will be the fact that your GetCollectionByName has no return type declared. That means that here:

Dim list = collection.Find(filter).ToList()

that collection variable is implicitly type Object and you are relying on late binding when calling that Find method because the Object 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 any Find method listed there. There is a FindSync method, so maybe that's what you actually want. If you had Option 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 is On for all future projects.

I had a look at some documentation for the MongoCollectionImpl for Java and there appears to be a find 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 the IMongoCollection so you should only be working with members of that interface. Basically, your code would need to look more like the below with Option Strict On:

Public Function GetCollectionByName(ByVal collectionName As String) As IMongoCollection(Of BsonDocument)
    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) As List(Of BsonDocument)
    Dim filter = Builders(Of BsonDocument).Filter.Eq(Of String)("ID", id)
    Dim collection = GetCollectionByName("Users")
    Dim list = collection.FindSync(filter).ToList()

    Return list
End Function

You may want to declare the GetUser method as type IList(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.