0

On a similar vein to this question I am trying the example in the LiteDB documentation, and at runtime get the error:

Invalid BSON data type 'Null' on field '_id'.

My code is below, including my attempts to solve the problem, which was to add the _id and Id declaration lifted from the comments on the github, here

Public Class Customer
   Public _id As LiteDB.BsonValue
   Public Id As Integer
   Public Name As String
   Public Phones As String
   Public IsActive As Boolean
End Class

Public Class ThisAddIn


  Shared Sub testSub()
    Dim db As New LiteDB.LiteDatabase(Directory.GetCurrentDirectory() & "\DEPDB.db")
    Dim col As LiteDB.LiteCollection(Of Customer)

    col = db.GetCollection(Of Customer)("customer")

    Dim tCustomer As New Customer

    tCustomer.Id = 1
    tCustomer.Name = "John Doe"
    tCustomer.Phones = "12354534"
    tCustomer.IsActive = True

    col.Insert(tCustomer)
  end sub
end class
Lauren Rutledge
  • 1,195
  • 5
  • 18
  • 27
Martin KS
  • 481
  • 7
  • 26

1 Answers1

1

Changing the class declaration slightly to:

Public Class Customer
  Public Property Id As Integer
  Public Property Name As String
  Public Phones As String
  Public IsActive As Boolean
End Class

Allowed the code to compile and run, and for the "Name" field to be searchable using the below:

    col.EnsureIndex(Function(x) x.Name)

    Dim tresult = col.FindOne(Function(x) x.Name.Contains("Jo"))

    MsgBox(tresult.Name)

I came across this solution wholly by accident though, adding words to the declaration... any explanation would be greatly appreciated.

Martin KS
  • 481
  • 7
  • 26
  • It's probably also worth noting that only items declared as `Property` will be saved to the database. Anything that's not got the keyword `Property` keyword is discarded. – Martin KS Aug 13 '18 at 19:43