Retrieving document structure based on previous records

42 Views Asked by At

Writing this question I'm working with .Net's LiteDB but I think the question applies to entire NoSQL matters.

One of collection in my db contains documents that don't have fixed structure. I want to let user add his own values, of whatever name and value he wants.

So, for example, document at first would have following structure:

{
   "_id": 1,
   "creatorId": 10
}

But user would be able to specify new value and choose whether it will be int or boolean.

{
   "_id": 1,
   "creatorId": 10,
   "customValue": false
}

Next time user open my app, he maybe will want to use values of the same kind as he used before, so I need to show him some kind of form with inputs named based on his previous activity. So if he previously added value named "customValue", I want to show him TextView named "customValue" next time he opens page with form.

Is there a way of retrieving structure of such document based on every record from collection? Or do I need to somehow track names of added values and save them in separate collection?

1

There are 1 best solutions below

0
On

In LiteDB you can use BsonDocument class to read collection documents. BsonDocument is a generic way to implement document in BSON format (with all BSON data type available).

If you use:

var col = db.GetCollection("mycol");
var doc = col.FindById(1);

foreach(var key in doc.Keys)
{
    var value = doc[key];
    var valueDataType = value.Type; // return an enumerable with all data types
}