Key Value pairs and how they are listed in the Database

I am new to mongodb and I am unsure if I am even posting in the right place.

I have the following schema for a collection of products

On looking at this I am concerned about the dimensions (Length, width, height, and weight). I think it would be better to store them as key value pairs under a Dimensions heading. Should I add the Dimensions field as an object or array? And how is the key value entered in to the record?

Hello @David_Thompson, you can store the “dimension” data as an object or an array depending upon the use case (that is what kind of data you are storing and how you are querying it).

In case the dimension’s properties are the same for all the products, you can store like this as an object to group the related data:

dimension: {
    length: 10.2,
    width: 18.0,
    height: 5.77,
    weight: 8.0
}

In case of having different dimension properties for the products then you can apply the Attribute Pattern to store the data in an array field of objects. Each object is a key-value pair as below:

dimensions: [
    { k: "length", v: 12.7 },
    { k: "width", v: 15.0 },
    // other k-v pairs
]
1 Like

@Prasad_Saya , Thanks for your response… Sorry it took me so long to reply, I am doing this project as a side project to my current job.

I understand your examples, however, am using atlas to establish my schema and I cannot edit the array values… when I make the field dimensions and assign it the array datatype, I am immediately confronted with the 0,1,2 field values in the array (item at place 0 is length) but I am unsure whether to make length another array with the value (basically making it a 2 dimensional array) or not.

I made it an object because I can define the data fields with it being an object… Am I on the right thinking?

On another note… I have been thinking about how I am doing my product collection and I have a question for you.

I am building a program which will allow businesses to sell products online… I currently have the product collection and I am embedding objects into when needed… For example I have embedded the dimension object for the length, height, width, and weight.

Where I am getting stuck is I need to allow for the differentiation between textiles and non textile products… Textiles will have the attributes colour size and quantity. I currently have the size as an object with the attributes of the sizes (large, medium, small) and the quantity. I believe I should redo this to be colour with the attribute of size and quantity.

below is my current way of doing this. Am I on the right track?

Am I on the right track or am I making this harder than it needs to be?

Hello @David_Thompson. Most of the document design depends upon the usage - how you want to query (includes CRUD) it. In other words how you want to use in your application and access it efficiently.

Creating some data samples and important queries you might use in your app - can help.

For example:

I currently have the size as an object with the attributes of the sizes (large, medium, small) and the quantity.
I believe I should redo this to be colour with the attribute of size and quantity.

One way to store this data is as shown below. Will it suit your application needs, I cannot tell for sure as I don’t know how the data is to be used.

[
  { color: "red", size: "L", qty: 4 },
  { color: "red", size: "M", qty: 1 },
  { color: "white", size: "XL", qty: 3 }
]

@Prasad_Saya ,

Thanks… My question is what does this look like in Atlas? If I were to look at the record in atlas, would it be nested arrays?

[ color [red [size [qty] ] ] ]

or would this be nested objects?

I am trying to see what this would look like at the record level.

The issue I run into is if I create an array I can’t rename the array index… If I make it an object, I can. So this would have to be stored as an object right?

In atlas, I can make an array for colour, but then I only have 0, 1, 2, etc to place the values in… If I make 0 another array, I cannot name that nested array to size.

colour [
0: red ( if I use the string red, I cannot say it is another array. Or I don’t know how to stipulate that it is an array “red” or the array “colour”.

Am I explaining this right?

@Prasad_Saya ,
I apologise. I struggle with schema with mongodb. I understand what I want to do… I just get confused with the embedded document idea. To embed a document, it is an array or is it an object… This is where I get confused and in searching for this definition or distinction, there isn’t any real good explanations in mongodb documentation.

I looked for embedded nested arrays… The mongodb documentation talks about querying them, but not in defining them.

Hi @David_Thompson welcome to the community!

In this case, I would recommend you to browse the free MongoDB University courses. We have all sorts of courses from beginner to advanced that may help clear up your confusion. Specifically, please have a look at M001 MongoDB basics and M100 MongoDB for SQL pros to start with. Then M320 data modeling might be a good next step.

Best regards
Kevin

Thank you Kevin… I have taken the Developer courses… But I will look at retaking the M001 MongoDB Basics Again (Although it will only teach me compass and the basics of querying). To my knowledge (from the last time I took it (It has been updated since I took it last) it doesn’t go into how the data is stored. Nesting arrays within arrays (basically a 2d array)

M100 MongoDB for SQL pros is new to me… I will look at it and hopefully it will help me to gain a better understanding where I have questions.

I will also look at the data modeling course again… I believe I have already taken this one, but I may need to revisit it.

Thanks

Dave

1 Like

@David_Thompson, MongoDB data can be of various types and includes arrays and objects. These can be combined and nested. Here is some additional info:

The data stored in the MongoDB Atlas cluster or on your local computer is similar. You can work with the data using various tools like mongosh, Compass, etc.

2 Likes

@Prasad_Saya ,
THANKS!!! The link to the mongoDB manual helped a TON!!

Regards,

Dave