1

I'm new to understanding NoSQL databases.

Suppose I have a hierarchy like

public interface MyInterface
{
    [JsonProperty("id")]
    Guid Id { get; }
}

public class Foo : MyInterface
{
    public Guid Id { get; set; }

    [JsonProperty("intVal")]
    public int IntVal { get; set; }
}

public class Bar : MyInterface
{
    public Guid Id { get; set; }

    [JsonProperty("stringVal")]
    public string StringVal { get; set; }
}

Is it possible to create a collection that holds MyInterfaces? Or can it only hold fixed Json structure and I have to do something like

public class MyClass
{
    [JsonProperty("id")]
    public Guid Id { get; set; }

    [JsonProperty("type")]
    [JsonConverter(typeof(StringEnumConverter))]
    public MyClassType Discriminator { get; set; }

    [JsonProperty("fooIntVal")]
    public int? FooIntVal { get; set; }

    [JsonProperty("barStringVal")]
    public string BarStringVal { get; set; }
}

public Enum MyClassType
{
   [JsonProperty("foo")]
   Foo,

   [JsonProperty("bar")]
   Bar
};

???

See Sharp
  • 379
  • 1
  • 4
  • 11
  • 2
    Well, Cosmos DB collections have no schema, so you can put in any documents you like. – juunas Aug 20 '18 at 18:33
  • 1
    Cosmos DB has no restrictions on what you store in a collection. You'd need to create some type of "type" property to differentiate between document types (for your queries) but... store whatever you want. I'd suggest spending a bit of time studying partitioning with Cosmos DB though, to fully understand how queries work within a partition vs cross-partition. Also, please note that "NoSql" is just an umbrella term for non-relational databases. It doesn't equate to "document database." And it doesn't have a definition for what can be stored where, regardless of database type or brand. – David Makogon Aug 20 '18 at 19:32

1 Answers1

-1

Is it possible to create a collection that holds MyInterfaces?

Of course you can. As mentioned in the comments,cosmos db collections have no schema so that you could design it into any structure you want to design.

Such as:

{
    "id": "1",
    "Discriminator": "foo",
    "FooIntVal": 5
},
{
    "id": "2",
    "Discriminator": "bar",
    "BarStringVal": "aaa"
}

Or even:

{ 
    "id": "5",
    "Discriminator": {
        "foo": {
            "FooIntVal": 5
        },
        "bar": {
            "BarStringVal": "aaa"
        }
     }
}

You can adjust the data structure flexibly.I suggest you refer to this doc to get start. Any concern,please let me know.

Jay Gong
  • 23,163
  • 2
  • 27
  • 32