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.

When insert document causes LiteException.

See original GitHub issue

When i try to insert document on collection cause LiteException:

LiteDB.LiteException: Invalid BSON data type ‘Null’ on field ‘_id’. at LiteDB.LiteEngine.InsertDocument(CollectionPage col, BsonDocument doc, BsonType autoId) at LiteDB.LiteEngine.<>cDisplayClass22_0.<Insert>b0(CollectionPage col) at LiteDB.LiteEngine.Transaction[T](String collection, Boolean addIfNotExists, Func2 action) at LiteDB.LiteEngine.Insert(String collection, BsonDocument doc, BsonType autoId) at LiteDB.LiteCollection1.Insert(T document) at ForGer.Modules.DeveloperModule.CadastraTodosAsync(CommandContext ctx) in C:\Users\Nathan Ferreira\Desktop\ForGer\ForGer\Modules\DeveloperModule.cs:line 48 at DSharpPlus.CommandsNext.Command.ExecuteAsync(CommandContext ctx)

Class:

	public class Usuario
	{
		[BsonId]
		protected ObjectId _id;

		[BsonField("user_id")]
		private string _user_id;

		[BsonIgnore]
		public ulong UserId {
			get { return ulong.Parse(_user_id); }
			set { _user_id = value.ToString(); }
		}

		[BsonField("experience")]
		public long Experience { get; set; }
	}

Insert method:

var user = new User();
user.UserId  = 235088799074484224;
user.Experience = 62754;

var mapper = new BsonMapper();
mapper.IncludeNonPublic = true; // accepts protected, internal and private fields.

var db = new LiteDatabase("Users.db", mapper);
var col = db.GetCollection<Usuario>();
col.Insert(user); // Causes LiteException Invalid BSON data type 'Null' on field '_id'.

Same exception in this class: (Using no protected fields)

public class Usuario
{
	[BsonId]
	public ObjectId _id;

	[BsonField("user_id")]
	public ulong UserId { get; set; }

	[BsonField("experience")]
	public long Experience { get; set; }
}

No way to auto create an object id?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
lilasquaredcommented, Nov 14, 2018

@nathan130200 make your _id field a property and it should work, or use the BsonMapper.Global.IncludeFields = true option

0reactions
nathan130200commented, Nov 16, 2018

Fixed! Thanks for help.

Read more comments on GitHub >

github_iconTop Results From Across the Web

When insert document causes LiteException.
When i try to insert document on collection cause LiteException: LiteDB.LiteException: Invalid BSON data type 'Null' on field '_id'.
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 >
LiteDB Studio: Unexpected token
It seems that LiteDB expects an array of document, so your file should start by [ and end by ] and the documents...
Read more >
LiteDB - A .NET NoSQL Document Store in a Single Data File
Simple, fast and free embedded .NET NoSQL Document Store in single data file.
Read more >
C# – LiteDB: Invalid BSON data type 'Null' on field '_id'
When the object is returned from the collection, it does not contain this '_Id' field, and thus fails to update the record in...
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