Unhandled Exception: ... invalid name ... at LiteDB.BsonDocument.set_Item in 'mutable' field
See original GitHub issueThe 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:
- Created 4 years ago
- Comments:7 (3 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Fixed and released in version
2.8.0to 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 😄Hmm even though it is not recommended, I don’t see an issue in supporting
mutableconstructs in the library, I will work on it…