question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unhandled Exception: ... invalid name ... at LiteDB.BsonDocument.set_Item in 'mutable' field

See original GitHub issue

The mutable attribute avoids insert in LiteDB, or passing through Json’s encoder, something happens that the name of the field is modified.

open System
open LiteDB
open LiteDB.FSharp

[<StructuredFormatDisplay("a{SizeGb}GB")>]
[<CLIMutable>]
type Disk = 
    { SizeGb : int }

[<StructuredFormatDisplay("Computer #{Id}: {Manufacturer}/{DiskCount}/{Disks}")>]
[<CLIMutable>]
type Computer =
    { Id: int
      mutable Manufacturer: string
      mutable Disks: Disk list }
    [<BsonIgnore>] 
    member __.DiskCount = __.Disks.Length  // property calculated

[<EntryPoint>]
let main argv =

    let myPc =
        { Id = 0
          Manufacturer = "Computers Inc."
          Disks =
            [ { SizeGb = 100 }
              { SizeGb = 250 }
              { SizeGb = 500 } ] }
    printfn "%A" myPc

    let mapper = FSharpBsonMapper()
    use db = new LiteDatabase("data.db", mapper)
    let computers = db.GetCollection<Computer>("computers")

    // All right , but if I remove the comment on next line I get the error
    // computers.Insert(myPc) |> ignore
    myPc.Manufacturer <- "Dell"
    printfn "%A" myPc
    ...

The field you are trying to modify Manufacturer seems to change its name Manufacturer@' The full error is:

Unhandled Exception: System.ArgumentException: Field 'Manufacturer@' has an invalid name.
   at LiteDB.BsonDocument.set_Item(String name, BsonValue value)
   at LiteDB.JsonReader.ReadObject()
   at LiteDB.JsonReader.ReadValue(JsonToken token)
   at LiteDB.JsonSerializer.Deserialize(String json)
   at LiteDB.FSharp.Bson.serialize[t](t entity)
   at LiteDB.FSharp.FSharpBsonMapper.ToDocument[t](t entity)
   at LiteDB.LiteCollection`1.Insert(T document)
   at Program.main(String[] argv) in /app/Program.fs:line ..

Thanks

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
Zaid-Ajajcommented, May 4, 2019

Fixed and released in version 2.8.0 to nuget, should be available really soon when nuget does it’s magic. Now I decided to take control of how the fields are marshalled, so now these mutable records will work just fine 😄

1reaction
Zaid-Ajajcommented, May 4, 2019

Hmm even though it is not recommended, I don’t see an issue in supporting mutable constructs in the library, I will work on it…

Read more comments on GitHub >

github_iconTop Results From Across the Web

"Field 'ExampleName$0$' has an invalid name" after ...
After upgrading to LiteDB 4.0 my field names are not valid anymore. The reason for this is a change in commit 0c69c84. From...
Read more >
LiteDB: Invalid BSON data type 'Null' on field '_id'
When you have a object without an identification, LiteDB convert your object to BsonDocument and create a new "_id" on insert.
Read more >
BsonDocument - LiteDB :: A .NET embedded NoSQL ...
The BsonDocument class is LiteDB's implementation of documents. ... The only exception is for _id field, which will always be the first field....
Read more >
Object Mapping - LiteDB :: A .NET embedded NoSQL ...
A property can be decorated with [BsonField("fieldName")] to customize the name of the document field; No circular references are allowed; By default, max...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found