1

I'm implementing the UPDATE part of a CRUD interface, and using LiteDB to store my collection of objects.

The objects I am storing are Schedules and Schedule.ScheduleName is the index on the collection. Schedule.ScheduleName is the [BsonId] of the type.

My Update method is pretty darned simple:

    public bool UpdateSchedule(Schedule schedule, string scheduleID)
    {
        return scheduleCollection.Update(scheduleID, schedule);
    }

Will that do what I'm expecting, however? If the schedule object being provided has the new name in it, and schjeduleID is the old name, I expect it will go find the old entry under scheduleID and update it with whatever is in the new Schedule object. But will it?

And if so, is that the correct way to implment the U part of CRUD? Do I have the semantics right? I can't think of any other way to rename or change and object's ID otherwise!

Akira Yamamoto
  • 4,685
  • 4
  • 42
  • 43
Dave
  • 1,521
  • 17
  • 31

1 Answers1

0

In LiteDB is not possible change _id value because this _id is used to find you document inside database.

This update signature Update(BsonValue id, T entity) can be used only if you entity (POCO class) has no _id value after serialize.

public bool Update(BsonValue id, T document)
{
    if (document == null) throw new ArgumentNullException("document");
    if (id == null || id.IsNull) throw new ArgumentNullException("id");

    // get BsonDocument from object
    var doc = _mapper.ToDocument(document);

    // set document _id using id parameter
    doc["_id"] = id;

    return _engine.Value.Update(_name, new BsonDocument[] { doc }) > 0;
}

And the reason for this is because _id index (PK) are not re-build in an update command. If you compare with tradicional databases (like SQL Server/Oracle) they have Row_Id that cannot be changed too.

mbdavid
  • 1,076
  • 7
  • 8
  • So a rename of _id can only be done via delete-reinsert with the new name? That's pretty painful. I'd expect this should have worked so long as the key meets the uniqueness constraint. – Dave Nov 10 '17 at 17:21
  • There is no index key change because index are inserted in asc order. And primary key is main index document. Even if there was an option, it would be necessary to delete and re-include the document. I recommend you to create another key to you logic and keeps `_id` as an internal identification. – mbdavid Nov 10 '17 at 17:59