LogEvent.Properties
with NewtonSoft.Json and serializing it right after with LiteDB.JsonSerializer, there's probably some form of incompatibility (I believe Newtonsoft serializes dates as strings, while LiteDB serializes them to a custom document)
{{"custom":{"TypeTag":null,"Properties":[{"Name":"message","Value":{"Value":"Yesenia-orange"}}]},"SourceContext":{"Value":"Sample.Program"},"Application":{"Value":"Sample"}}}
{{"SourceContext":{"Value":"Sample.Program"},"Application":{"Value":"Sample"}}}
//retrieval
return _repository.Query<T>()
.OrderBy(x => x.Id, order: Query.Descending)
.Where(predicate)
.Skip(skip)
.Limit(take)
.ToList();
var results = getLogMessages();
return Ok(results);
public class BsonValueConverter : Newtonsoft.Json.JsonConverter<BsonValue>
{
public override BsonValue ReadJson(Newtonsoft.Json.JsonReader reader, Type objectType, [AllowNull] BsonValue existingValue, bool hasExistingValue, Newtonsoft.Json.JsonSerializer serializer)
{
throw new NotImplementedException();
}
public override void WriteJson(Newtonsoft.Json.JsonWriter writer, [AllowNull] BsonValue value, Newtonsoft.Json.JsonSerializer serializer)
{
writer.WriteValue(value.ToString());
}
}
Hi, I'm working on a big project refactor and part of that involves abstracting the data model POCO classes from the data access layer. This allows for multiple database options, possibly MongoDB at least, and removing dependencies on the database assemblies in the rest of the project when using the data model classes.
I've been changing ObjectId properties to string and using Guids for ID instead, although these may not be as efficient as an ObjectId. I have a problem though with this approach on deserialising existing databases as ObjectId doesn't cast to string.
Is there a recommended way around this? I suppose I could register a mapping of ObjectId to string, but would that prevent me serialising an ObjectId if I really wanted to?
Alternatively a generic solution that still uses ObjectId, good for LiteDB and MongoDB (as they have virtually the same thing I believe), but I need that abstraction with no dependency on LiteDB in the model and dependent non-DB code needs access to an ID as a string.
If any of that makes sense.
var collectionNames = db.GetCollectionNames().ToList();
db.CheckpointSize = 0;
foreach (var collectionName in collectionNames)
{
db.BeginTrans();
var tmpColName = collectionName + "_tmp";
var col = db.GetCollection(collectionName);
var tmpCol = db.GetCollection(tmpColName);
var indexes = db.Execute($"select $ from $indexes where collection = '{collectionName}'").ToList();
foreach(var index in indexes)
{
tmpCol.EnsureIndex(index["name"].AsString, index["expression"].AsString, index["unique"].AsBoolean);
}
foreach(var doc in col.FindAll())
{
var newDoc = new BsonDocument();
foreach(var kvp in doc)
{
if(kvp.Value.IsObjectId)
{
newDoc[kvp.Key] = kvp.Value.AsObjectId.ToString();
}
else
{
newDoc[kvp.Key] = kvp.Value;
}
}
tmpCol.Insert(newDoc);
}
db.Commit();
db.DropCollection(collectionName);
db.RenameCollection(tmpColName, collectionName);
db.Checkpoint();
}
db.CheckpointSize = 1000;
db.Rebuild();
db.UserVersion
to determine whether the file has to be processed or not
db.GetCollection<Setting>("settings").UpdateMany(s => new { setting.Name, setting.Value }, s => s.Name == setting.Name);
Extend expression must return an anonymous class to be merge with entities. Eg: col.UpdateMany(x => new { Name = x.Name.ToUpper() }, x => x.Age > 10)
BsonMapper.Global.EnumAsInteger = true;
to my application. That solved the problem with my "first" enum but introduced one for another enum I use. This enum is defined on my class as public DocumentType[] SupportedDocumentTypes { get; set; }
: 'Object cannot be stored in an array of this type.'
. Contents of the DB field at the point is : [0,1]