2

I have two objects.

 [DataContract]
 public class Record
 {
    [DataMember]
    public string Id { get; set; }
 }

And this class:

public class BatteryStatus : Record
{
    [DataMember]
    public DateTime RetrieveTime { get; set; }

}

I'm using Lite DB as a local NoSQL option to query and save the data. I'm needing to find and delete the values based after some time. Here's my code doing so:

            var col = db.GetCollection<BatteryStatus>(CollectionName);
            var test = col.FindAll()
                .Where(x => x.Id == status.Id).ToList();
            var result = col.Find(Query.EQ("Id", status.Id.ToString())).ToList();

Test returns with the with the object, but the result value doesn't. Lite DB only uses the Query or the BSONId as a way to delete an object. I don't have a BSON id attached to it (it's a referenced definition so I can't change it).

How can I use the "Query" function in order to get a nested value so I can delete it?

Community
  • 1
  • 1
Kat
  • 2,460
  • 2
  • 36
  • 70

2 Answers2

2

Classes has properties, BSON documents has fields. By default, LiteDB convert all property names to same name in BSON document except _id field which is document identifier.

If you want query using Linq, you will use properties expressions. If you are using Query object class, you must use field name.

var result = col.FindById(123);
// or
var result = col.FindOne(x => x.Id == 123);
// or
var result = col.FindOne(Query.EQ("_id", 123));

Find using _id always returns 1 (or zero) document.

mbdavid
  • 1,076
  • 7
  • 8
  • Hey David, I've observed that for a property of an enum type, the only way I can query is using the query syntax. I'm using LiteDB version 1.0.4 - am I right in this observation or am I missing something? – Sudhanshu Mishra May 23 '16 at 07:08
  • Maybe is missing enum mapping in Linq visitor class. Please, open an issue on my github repo. – mbdavid May 23 '16 at 14:23
  • Issue opened: https://github.com/mbdavid/LiteDB/issues/173, also contains link to a gist that demos the issue. Thanks for your response and awesome work! – Sudhanshu Mishra May 24 '16 at 04:19
1

I figured out the problem with LiteDB, since I was using the property name of "Id", the BSON interpreted that as the "_id" of the JSON object, and merging their two values. I solve the issue by renaming the "Id" property to something else.

Kat
  • 2,460
  • 2
  • 36
  • 70