Mistre83
Mistre83

Reputation: 2827

MongoDB - Document with different type of value

I'm very new to MongoDB, i tell you sorry for this question but i have a problem to understand how to create a document that can contain a value with different "type:

My document can contain data like this:

// Example ONE
{
    "customer" : "aCustomer",
    "type": "TYPE_ONE",
    "value":    "Value here"
}

// Example TWO
{
    "customer": "aCustomer",
    "type": "TYPE_TWO",
    "value": {
        "parameter1": "value for parameter one",
        "parameter2": "value for parameter two"
    }
}

// Example THREE
{
    "customer": "aCustomer",
    "type": "TYPE_THREE",
    "value": {
        "anotherParameter": "another value",
        {
            "someParameter": "value for some parameter",
            ...
        }
    }
}

Customer field will be even present, the type can be different (TYPE_ONE, TYPE_TWO and so on), based on the TYPE the value can be a string, an object, an array etc.

Looking this example, i should create three kind of collections (one for type) or the same collection (for example, a collection named "measurements") can contain differend kind of value on the field "value" ?

Trying some insert in my DB instance i dont get any error (i'm able to insert object, string and array on property value), but i would like to know if is the correct way...

I come from RDBMS, i'm a bit confused right now.. thanks a lot for your support.

Upvotes: 3

Views: 3208

Answers (1)

Leandro Ferreira
Leandro Ferreira

Reputation: 21

You can find the answer here https://docs.mongodb.com/drivers/use-cases/product-catalog

MongoDB's dynamic schema means that each need not conform to the same schema.

Upvotes: 2

Related Questions