Litedb: Updating nested entities

Created on 11 Oct 2017  路  2Comments  路  Source: mbdavid/LiteDB

Hi @mbdavid!

Could you please show the most simple and fast way of updating nested entities using LiteDB? For example, what to do, if we have a reference to a NestedNode (not to a TopNode) and would like to inverse it's Value property (and save changes to database)?

c# class TopNode { public Guid Id { get; set; } = Guid.NewGuid(); public List<NestedNode> Children { get; set; } } class NestedNode { public Guid Id { get; set; } = Guid.NewGuid(); public bool Value { get; set; } }

Thanks!

Most helpful comment

Thanks a lot for in-depth explanation @mbdavid!

Just for those who may have misunderstanding on how NoSQL works (like I had a few weeks ago), I'd like to post a very simple solution here that gets things done using expression-based document query.
```c#
public Task Update(NestedNode nested) => Task.Run(() =>
{
var collection = _liteDatabase.GetCollection();
var query = Query.EQ("$.Children[*]._id", nested.Id);
var top = collection.FindOne(query);

top.Children.RemoveAll(i => i.Id == nested.Id);
top.Children.Add(nested);
collection.Update(top);

});

Example usage:
```c#
var nested = // get nested node from database
nested.Value = true;
Update(nested); // changes are saved to disk!

All 2 comments

Hi @Worldbeater, in document database you always need read all document and write all document. Doesnt matter if you change top node or nested nodes. It's a single document and will be serialize and write on disk at all. So, you can use simple FindOne, change your document (top or nested) and then call Update.

In v4 it's possible use Update command similiar with any dbms (it's like FindModify in Mongo). In shell tool you can:

// changing a value on specific array position
db.col.update $.Children[1].Value = true

// changing a value on filter position
db.col.update $.Children[@.Id = 100].Value = false

// adding new 
db.col.update $.Children += { Id: 1, Value: true }

// delete (re-create an array with no Value = true)
db.col.update $.Children = ARRAY($.Children[@.Value = true])

All command will be applied in all documents. If you want update a specific document you can you where clause:

db.col.update $.Children[0].Value = true where _id > 100

This command also can be used with Update command in LiteEngine (there is no LiteCollection strong-typed implementation yet). Take a look in Path/Expressions here

https://github.com/mbdavid/LiteDB/wiki/Expressions

Thanks a lot for in-depth explanation @mbdavid!

Just for those who may have misunderstanding on how NoSQL works (like I had a few weeks ago), I'd like to post a very simple solution here that gets things done using expression-based document query.
```c#
public Task Update(NestedNode nested) => Task.Run(() =>
{
var collection = _liteDatabase.GetCollection();
var query = Query.EQ("$.Children[*]._id", nested.Id);
var top = collection.FindOne(query);

top.Children.RemoveAll(i => i.Id == nested.Id);
top.Children.Add(nested);
collection.Update(top);

});

Example usage:
```c#
var nested = // get nested node from database
nested.Value = true;
Update(nested); // changes are saved to disk!
Was this page helpful?
0 / 5 - 0 ratings

Related issues

nightroman picture nightroman  路  3Comments

muhamad picture muhamad  路  3Comments

kuiperzone picture kuiperzone  路  4Comments

axelgenus picture axelgenus  路  3Comments

rstat1 picture rstat1  路  3Comments