mbdavid / LiteDB

LiteDB - A .NET NoSQL Document Store in a single data file
http://www.litedb.org
MIT License
8.62k stars 1.25k forks source link

[QUESTION] Difficulty Querying with Composite Index #2392

Closed mirror222 closed 11 months ago

mirror222 commented 12 months ago

hi, I'm facing an issue with querying using a composite index. I have a User class defined as follows:

    public class User
    {
        [BsonId]
        public int UID { get; set; }

        public string UserId { get; set; } = "";

        public long Amount { get; set; } = 0;
    }

I created a composite index in LiteDB like this:

db.EnsureIndex("uIndex", x => x.UserName + x.Amount.ToString(), true);

Here is some sample data in the database:

1   "Ben"   64
2   "Jemmy" 193

Now, when I try to query the data using the following statement, I'm unable to retrieve any results:

var u = db.FindOne(x => (x.UserName + x.Amount.ToString()) == "Ben64");

I would appreciate any insights or suggestions on why the query might not be working as expected.

Thanks in advance for your help!