Dynamic collection

42 views
Skip to first unread message

Raymond Dazo

unread,
Jan 5, 2022, 12:21:48 PM1/5/22
to RavenDB - an awesome database
Hi RavenDB Team,

Need help. I've been trying to figure this out using c#, RavenDB v4.2. But no luck.

I need to split each products based of company name. Example "product1" belongs to company "A", which should be stored to collection "ProductA",  "product2" belongs to company "B" should be stored to "ProductB", and so on. 

I need to have a model (c# class) that represents the document, as I would still like to have type-checking without using Dynamic or ExpandoObject.

Currently I am using BulkInsert to transfer a huge amount of products from another system to our RavenDB database.

Here is a snippet of my code:

foreach (var item in data.Results)
{
        var product = new Product()
        {
                Name = item.Name,
                Company = item.Company
        }
       
        bulkInsert.Store(cashflow);
}

I still want to be able to instantiate a model, but I've been trying to figure out how to change the Id prefix and collection at the same time.
All available conventions either deals with only one and not both, or only one has the data for the current document to check.

BulkInsert has a way to pass metadata, but I can't figure out how to get/create an instance of a metadata for a collection that may not exists yet.

In summary, is there a way to dynamically specify a prefix Id and collection name and still able to use a model class?

Example of desired output:

ProductsA/1
{
        "Name": "Product1",
        "@metadata": {
        "@collection": "ProductsA",
        "Raven-Clr-Type": "MyProject.Models.Products, MyProject"
    }
}

ProductsB/1
{
        "Name": "Product2",
        "@metadata": {
        "@collection": "ProductsB",
        "Raven-Clr-Type": "MyProject.Models.Products, MyProject"
    }
}

Notice that @collection is different but " Raven-Clr-Type" type is the same.

Thank you,

Raymond Dazo

Ryan Heath

unread,
Jan 5, 2022, 12:34:10 PM1/5/22
to rav...@googlegroups.com
If your class has an Id you should be able to prefix it like:


foreach (var item in data.Results)
{
        var product = new Product()
        {
                Id = $"Products{item.Company}/"
                Name = item.Name,
                Company = item.Company
        }
       
        bulkInsert.Store(cashflow);
}

HTH
// Ryan 

--
You received this message because you are subscribed to the Google Groups "RavenDB - an awesome database" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ravendb+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ravendb/d33e50ec-5638-4896-ab86-425152fb1cd5n%40googlegroups.com.

Oren Eini (Ayende Rahien)

unread,
Jan 6, 2022, 4:38:25 AM1/6/22
to rav...@googlegroups.com
This is probably what you want to customize the id generation:

Also look at this:


That said, note that a collection per client is probably not a good idea.
RavenDB assumes that you have a stable number of collections.

Also note that id prefixes are usually better for this sort of behavior

--
You received this message because you are subscribed to the Google Groups "RavenDB - an awesome database" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ravendb+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ravendb/d33e50ec-5638-4896-ab86-425152fb1cd5n%40googlegroups.com.


--
Oren Eini
CEO   /   Hibernating Rhinos LTD
Skype:  ayenderahien
Support:  sup...@ravendb.net
  
Reply all
Reply to author
Forward
0 new messages