Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
  • Aug 19 23:15
    janekos edited #2225
  • Aug 19 02:06
    janekos edited #2225
  • Aug 19 02:06
    janekos edited #2225
  • Aug 19 02:03
    janekos edited #2225
  • Aug 19 02:00
    janekos edited #2225
  • Aug 19 01:59
    janekos opened #2225
  • Aug 19 01:59
    janekos labeled #2225
  • Aug 15 14:39
    dlidstrom commented #1546
  • Aug 11 08:14
    marinusbeck opened #2224
  • Aug 11 06:40
    eyal7773 commented #1940
  • Aug 11 06:33
    marinusbeck opened #2223
  • Aug 11 06:33
    marinusbeck labeled #2223
  • Aug 10 17:45
    k-ahmedhm edited #2222
  • Aug 10 17:45
    k-ahmedhm edited #2222
  • Aug 10 17:43
    k-ahmedhm opened #2222
  • Aug 10 17:43
    k-ahmedhm labeled #2222
  • Aug 09 09:03
    gjvanvuuren labeled #2221
  • Aug 09 09:03
    gjvanvuuren opened #2221
  • Aug 08 17:42
    thomasagarza labeled #2220
  • Aug 08 17:42
    thomasagarza opened #2220
weirdyang
@weirdyang
@lbnascimento thanks!
WTTSoftwareSolutions
@WTTSoftwareSolutions
Hey Guys,
Is there a way in v5 to load a collection from json? I know that there is the ability to load from json file using the sql syntax:
db.Execute("select $ into mycollection from $file('myfile.json')")

however my json is not in a file on disc, it is in memory. This used to work:

db.Engine.Insert("mycol", JsonSerializer.Deserialize(json).AsArray.ToArray());

There does not seem to be accessibility to the Engine from LiteDatabase anymore.

Leonardo Nascimento
@lbnascimento
@WTTSoftwareSolutions If you open your collection as var col = db.GetCollection("mycollection"), you could do something like col.Insert(JsonSerializer.DeserializeArray(json).Cast<BsonDocument>())
weirdyang
@weirdyang

is there anyway to save message

@WTTSoftwareSolutions If you open your collection as var col = db.GetCollection("mycollection"), you could do something like col.Insert(JsonSerializer.DeserializeArray(json).Cast<BsonDocument>())

thanks! been trying to figure this out too

WTTSoftwareSolutions
@WTTSoftwareSolutions
@lbnascimento Thanks for the help! For some reason it didn't work. It kept throwing exceptions about the json formatting. The json was from me using Newtonsoft.Json to serialize a table from an OleDb DataSet. I changed the way I'm doing the conversion and am now using NPoco Query to get an IEnumerable of my Poco's and passing that into the Collection Insert() method. Success!! We have now officially dumped using Access and will be completely LiteDB in the January release of our product!
weirdyang
@weirdyang
Can I store a class with a bsondocument property? or should i just store it as a string and do a query e.g "Select JSON($.MyProperty) From Collection"
iSunOfficial
@iSunOfficial
Did I make a mistake, or is this not possible to use regex?
ILiteQueryable<Class> h = class.Query().Where(x => x.Query.Count(s => Regex.IsMatch(text, @$"^(.*)({s})(net)$")) > 0);
Shady3cho
@Shady3cho
I joined just to say, yo I love this db thanks to who ever made it
weirdyang
@weirdyang
Recommendation
Single instance (second option) is much faster than multi instances. In multi instances environment, each instance must do expensive data file operations: open, lock, unlock, read, close. Also, each instance has its own cache control and, if used only for a single operation, will discard all cached pages on close of datafile. In single instance, all pages in cache are shared between all read threads.
Does this mean for a web app I should be using the litedbrepo as a singleton?
weirdyang
@weirdyang
What is the best way to implement async methods?
Can I return a Task.FromResult or do I need to use lock?
Leonardo Nascimento
@lbnascimento
@iSunOfficial Regex is currently not supported, I'll see if it's possible to add support for it in a future version
@captmomo Yes, you can have a BsonProperty if you want
@captmomo Having a single instance of LiteDatabase is the recommended way to use LiteDB.
@captmomo There is currently no support for async methods in LiteDB, but someone made an async wrapper for LiteDB, you should check it out: https://github.com/mlockett42/litedb-async
weirdyang
@weirdyang
Thanks! sorry for all the questions!

btw,

            BsonMapper.Global.RegisterType<LogEventLevel>(
                serialize: (lvl) => (int)lvl,
                deserialize: (bson) => (LogEventLevel)bson.AsInt32);

I am trying to store an enum as an int and getting back the enum when deserializing.

this is my mapping method, however, the string representation is stored and I get a null object reference when I try to fetch it:
var results = context.LiteRepository.Fetch<LogMessage>(Query.EQ("Level", "Error"));

image.png
Leonardo Nascimento
@lbnascimento
@captmomo If you want to store enums as int, you can just configure the mapper with: BsonMapper.Global.EnumAsInteger = true;
weirdyang
@weirdyang
ahh, thanks.
weirdyang
@weirdyang

thanks that works! but now I get a null object reference when I try to fetch the items, it seems my mapping for the DbRef is causing the error:
+ TargetSite {System.Object Deserialize(System.Type, LiteDB.BsonValue)} System.Reflection.MethodBase {System.Reflection.RuntimeMethodInfo}

            BsonMapper.Global.Entity<LogMessage>()
                .Id(x => x.Id)
                .DbRef(x => x.User, nameof(User));
var results = context.LiteRepository.Fetch<LogMessage>(Query.EQ("Level", (int)LogEventLevel.Error));

Do I need to do an .include?

Leonardo Nascimento
@lbnascimento
@captmomo You shouldn't use DbRef with LiteRepository. LiteRepository is meant to be used as a single "container" for all your data types. If you want distinct collections for different types (that's what DbRef does), you should use LiteCollection<T>
weirdyang
@weirdyang
Oh, so LiteRepository is only to be used for a single collection
Leonardo Nascimento
@lbnascimento
@captmomo Internally it still uses separate collections in the data file, but it's meant to be accessed as a single container.
weirdyang
@weirdyang
ok, sorry I'm a bit confused as to what's the difference. do you mind explaining what you mean by a single container?
isn't dbref for referencing collections within the same db?
Leonardo Nascimento
@lbnascimento
@captmomo The most common way to manipulate data in LiteDB is using LiteCollection<T>. For example, if you have two classes Customer and Product, you would open two distinct collections: var customerCol = db.GetCollection<Customer>(); and var productCol = db.GetCollection<Product>();
@captmomo If you want a single "entry point" for all your data, that's when you use LiteRepository. Internally, it still uses distinct collections, but you can simply throw all your objects at it and it will handle the collections itself.
Leonardo Nascimento
@lbnascimento
@captmomo Now, imagine you have a property of type Product inside Customer. LiteDB will store Product as a sub-document inside Customer, so they'll both be stored in the Customer collection. By using DbRef, you can store instances of Product in its own collection and store only a reference to it in every customer. This, however, should not be used with LiteRepository.
weirdyang
@weirdyang
hmm ok, I'm still not sure why LiteRepository can't handle the the dbref. does it have issues loading the referenced entities?
weirdyang
@weirdyang
image.png
I switched to using collection and it's still throwing the error.
weirdyang
@weirdyang
It seems that I must call .Include, if not the query will fail with the null object reference error. Is this the expected behavior?
Leonardo Nascimento
@lbnascimento
@captmomo Yeah, my explanation wasn't particularly clear, sorry about that. The default behavior of LiteDB is to store sub-objects as embedded documents. DbRef allows you to override this behavior, but it changes the "expected" collection structure. If you want to use LiteRepository, you should let it decide on the correct way to store things.
Leonardo Nascimento
@lbnascimento
@captmomo Regarding the Include, I responded to the issue you opened.
weirdyang
@weirdyang
@lbnascimento Appreciate the help!
i swtiched to just storing the id of the user instead of a ref, everything seems to be working now
weirdyang
@weirdyang

How does the LiteDb IQueryable Skip work with Limit?

If I have 50 entries. and I have a query like:

            UserGenerator userGenerator = new UserGenerator();
            while (count <= 10)
            {
                User newUser = userGenerator.Generate();
                newUser.Name = "test";
                context.LiteRepository.Insert(newUser);
                count++;
                testing.Add(newUser);

            }

            var user = context.LiteRepository.FirstOrDefault<User>(x => x.Name == "test");
            var users = context.LiteRepository.Query<User>().Where(x => x.Name == "test").Skip(1).Limit(5).ToList();

The first query gives me a user with Id = 9, the second query gives me users with ids from 6 to 11.

Is my query syntax incorrect?

weirdyang
@weirdyang
image.png

Newtonsoft.Json.JsonSerializationException: Error getting value from 'AsDateTime' on 'LiteDB.BsonValue'. ---> System.InvalidCastException: Unable to cast object of type 'System.String' to type 'System.DateTime'. at lambda_method(Closure , Object )

Getting this issue with this class when serializing:

weirdyang
@weirdyang
mbdavid/LiteDB#169 <<- DateTimeOffSet has builtin support, so I'm not sure what is the issue
Leonardo Nascimento
@lbnascimento
@captmomo Are you using any custom deserializer?
weirdyang
@weirdyang
No.
The JsonConvert is from NewtonSoft, could that be an issue?
Leonardo Nascimento
@lbnascimento
I'm not sure, could you show me how you're using objects of this class? How you're inserting, how they're being serialized/deserialized etc. Where is this error happening?
weirdyang
@weirdyang
   internal async Task<IEnumerable<LogMessage>> ProcessLogEvents(IEnumerable<LogEvent> logEvents, int clientId)
        {
            List<LogMessage> messages = new List<LogMessage>();
            foreach (var evt in logEvents)
            {
                var logMessage = new LogMessage()
                {
                    Level = evt.Level,
                    Message = evt.RenderMessage(),
                    TimeStamp = evt.Timestamp,
                    UserId = clientId
                };
                if (evt.Exception != null)
                {
                    logMessage.Exception = evt.Exception.ToString();
                }

                if (evt.Properties.Any())
                {

                    string complete = await Task.Run(() => Newtonsoft.Json.JsonConvert.SerializeObject(evt.Properties));
                    logMessage.Properties = LiteDB.JsonSerializer.Deserialize(complete).AsDocument;

                }
                messages.Add(logMessage);
            }
            return messages;
        }
Converting the log events into the model for storage
//retrieval

                return _repository.Query<T>()
                .OrderBy(x => x.Id, order: Query.Descending)
                .Where(predicate)
                .Skip(skip)
                .Limit(take)
                .ToList();
            return Ok(results);
the error happens when I return the ActionResult
the last step