Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
  • Jan 26 21:54
    agreentejada commented #2140
  • Jan 24 18:31
    kcsombrio closed #2142
  • Jan 24 18:31
    kcsombrio commented #2142
  • Jan 24 06:59
    anseketamen labeled #2144
  • Jan 24 06:59
    anseketamen opened #2144
  • Jan 24 03:50
    Rickedb commented #2017
  • Jan 21 14:36
    eugeniaparvulescu commented #2134
  • Jan 21 13:02
    eugeniaparvulescu commented #2134
  • Jan 21 13:01
    eugeniaparvulescu commented #2134
  • Jan 21 11:28
    eugeniaparvulescu commented #2134
  • Jan 21 08:37
    ivi-cc edited #2138
  • Jan 21 08:30
    ivi-cc edited #2138
  • Jan 20 16:32
    roubachof opened #2143
  • Jan 20 16:32
    roubachof labeled #2143
  • Jan 19 09:31
    brus77 edited #2142
  • Jan 19 09:30
    brus77 edited #2142
  • Jan 19 09:29
    brus77 opened #2142
  • Jan 19 09:29
    brus77 labeled #2142
  • Jan 19 01:17

    kcsombrio on master

    Fix issue #2129 - reload 'last'… Merge branch 'master' of https:… * Fixed index size length calcu… (compare)

  • Jan 18 21:21
    kcsombrio commented #2141
Leonardo Nascimento
@lbnascimento
Are you still calling the NewtonSoft serializer?
weirdyang
@weirdyang
                if (evt.Properties.Any())
                {

                    logMessage.Properties = BsonMapper.Global.Serialize(evt.Properties).AsDocument;

                }
                messages.Add(logMessage);
I'm using a new database too
Leonardo Nascimento
@lbnascimento
In which line is this exception being thrown?
weirdyang
@weirdyang

When the controller action returns the result in

Ok(result)

//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);
Leonardo Nascimento
@lbnascimento
Well, the Newtonsoft serializer is being called somewhere (maybe inside this Ok method), and it is failing to serialize BsonValues (it's trying to cast string to DateTime, string to Boolean etc.)
weirdyang
@weirdyang
yea, the mvc builder uses it. the weird thing is none of the propeties have boolean or datetime
Leonardo Nascimento
@lbnascimento
The class BsonValue has properties for all valid bson types: AsBoolean, AsInt32, AsDateTime...
These properties obviously only work if the internal value of that BsonValue matches the property, it will throw otherwise
Newtonsoft is probably trying to serialize every property in the BsonValue (BsonDocument inherits from BsonValue) and that's why it's throwing
weirdyang
@weirdyang
Thansk for the hint @lbnascimento :)
    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());
        }
    }
Tim Moore
@tjmoore

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.

Leonardo Nascimento
@lbnascimento
@tjmoore While I believe it may be possible to create a context-aware mapping for ObjectId (using the UserVersion pragma or something like that), I think it would be janky.
I believe your best bet is to simply convert the ObjectId properties into strings both in your classes and in your datafiles.
Leonardo Nascimento
@lbnascimento
@tjmoore I came up with this piece of code:
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();
It processes all collections in a datafile, transforming ObjectIds into strings. I ran some tests and it seems to run fine - of course, you should do your own testing, to ensure that it works properly and that it fits your needs.
If you have a distributed aplication (like a mobile app), you can use db.UserVersion to determine whether the file has to be processed or not
Tim Moore
@tjmoore
@lbnascimento Cool, thanks for that. I'll give it a go. It does seem like the simplest way to do it.
Nate Komodo
@NateKomodo
How does one use UpdateMany? the example is to use an anonymous type, but i get a type conversion error when i do this
db.GetCollection<Setting>("settings").UpdateMany(s => new { setting.Name, setting.Value }, s => s.Name == setting.Name);
The example is 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)
weirdyang
@weirdyang
col.UpdateMany(x => new Customer { Name = x.Name.ToUpper(), Salary: 100 }, x => x.Name == "John")
image.png
Leonardo Nascimento
@lbnascimento
@NateKomodo I don't know which version you're running, but the example is probably wrong. In the latest version, it is correct: col.UpdateMany(x => new Customer { Name = x.Name.ToUpper(), Salary: 100 }, x => x.Name == "John")
Mark Gerrits
@skinny
Hi all! I have been using v5 of LiteDB with pleasure but I'm running into an issue with enums. By default enum values are serialized as strings but that won't work with Linq so I added 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; }
Saving is working, but when retrieving an item from the DB I get : : 'Object cannot be stored in an array of this type.' . Contents of the DB field at the point is : [0,1]
any help would be much appreciated
Leonardo Nascimento
@lbnascimento
@skinny Apparently there's a bug when deserializing enum arrays, I'll fix it shortly
Leonardo Nascimento
@lbnascimento
@skinny It is fixed in the master branch, check it out
Mark Gerrits
@skinny
oh nice! building :)
@lbnascimento works like a charm! Using my local build for now, any ETA on a new nuget release ?
Leonardo Nascimento
@lbnascimento
@skinny Usually we wait a few weeks between minor releases (v5.0.9 was a fluke because we were really busy with features for the next major release)
Mark Gerrits
@skinny
no problem, but just to get my expectations straight :)
thanks again!
Chris Hill
@robotron2084
Hello! I'm wondering if i've found a bug or I'm doing something wrong:
In BsonValue:
public BsonDocument AsDocument => this as BsonDocument;
Is there some kind of override that will convert a BsonValue into a BsonDocument that I'm not seeing?
In the code I've written I've created a BsonValue by doing new BsonValue(myBsonDocument) and it is throwing a null exception on case BsonType.Document: return this.AsDocument.GetBytesCount(recalc); during the upsert.
Leonardo Nascimento
@lbnascimento
@robotron2084 Could you provide more info? Like what is the value of myBsonDocument?
Chris Hill
@robotron2084

To be clear, the issue I'm seeing is that the above code is trying to cast the BsonValue as a BsonDocument....which I don't see how that would ever work. To answer your question:

The BsonDocument passed to the BsonValue is created like so:

BsonDocument ToBsonDocument()
{
  BsonDocument retVal = new BsonDocument();
  retVal["x"] = x;
  retVal["y"] = y;
  return retVal;
}
Floowey
@Floowey
hey, I have an issue where my database becomes unreadable with a System.NotSupportedException: BSON type not supported
It happened twice on the same of my users entries; after the first time I thought I had found the issue but a few hours later if happened again. Is there any way to see what the offending string is?
Leonardo Nascimento
@lbnascimento
@robotron2084 BsonDocument inherits from BsonValue, so it should work whenever it is actually a document, returning null otherwise
4 replies
@Floowey Could you send me your data file?
Floowey
@Floowey
@lbnascimento yes, but I'm new to gitter. Let me try figuring out how to dm you
that didn't work out as I thought it would
let me know if there's anything else you need, you could also dm me on discord (Floowey#0205)
ASPNIC
@ASPNIC
I'm trying to store Serilog's LogEvent in LiteDB, but there is an issue with fetching the objects back as the LogEvent class contains another class that has no public constructors with no parameters. So it doesn't know how to deserialize it. I solved the json Serializing/Deserializing with newtonsoft by telling newtonsoft how to deserialize that specific class, but that doesn't work for LiteDB. Any ideas on how to solve this?
I guess I could use newtonsoft to serialize it and then store it as a string, and then deserialize the string on fetch, but I'd much prefer to skip that step if at all possible somehow