CRUD Operations
Now let's see how the create, read, update and delete operations work in
the addon. For better understanding let's first look at how the read
works.For this experiment, let's consider a MongoDB collection named families and the document schema is as below.
{ father: { name: ** // string age: ** // number }, mother: { name: ** // string age: ** // number }, phone: ** // number kids: [ { name: ** // string dob: ** // date height: ** // number weight: ** // number } ] }
In the above schema please note that most keys are the type of string
or number, except kids dob is a date
Reading from database
Let's first read the father and
mother from the database. To read, just
add keys to row 1 of the spreadsheet. And then click on read from the
sidebar. The data will be populated in the spreadsheet.
But you can see that both father and mother are objects. You can get father.name and father.age in a separate column easily by populating headers as shown below.
But now you can see that age is not correctly populated. This can be easily solved by type conversion. Please see the picture below. age is a number, so we can convert the populated data to number by giving the header as 'father.age:number'. The other type conversion are oid, date, string and number. There is also array and object. But its used for a different purpose. Details about it are below.
Dot notation method is great for reading the data. But don't use it
while creating or updating a document. Because mongoDB doesn't
supports dot notation. The prefered method is to Map the data to
objects or arrays from sheet. Details about it is coming next.
Since father and mother are objects, kids, are arrays we can separate those keys into different sheets. To do that first create three sheets for father, mother, and kids respectively.
father and mother is of type object we need to give the type information in the header as 'father:object' and 'mother:object'. And for kids, since its an array we need to give the type information in the header as 'kids:array'. Please refer the picture below.
If you look at the $ keys of
father,
mother, and
kids you
will see that in sheets
father and
mother the
$ keys are unique, it's not repeating. But in the
kids sheet
the $ keys are repeating. Its because
kids is an
array and other two are just an object. When we link these items back
to our original document then mongoSheet uses this key for linking.
To make the populate possible, select the entire column father and then click on Populate from the sidebar. from the sidebar. And do the same for mother and kids. Since we have given the datatype of $father and $mother as object and kids as array mongoSheet will populate the data in the required format before calling the update.
We have now looked at how the Read and Update works. Create works very similar to Update. In Create you don't have to give _id in column A, the same will be populated once the when the In Create operation is completed. To Delete, specify the document id in column A and click on Delete from the sidebar. All documents specified in column A will be removed from the collection.
This was all about CRUD operation and now all you have to do is to try yourself, fail and learn.
If you are facing issues, then please send us an email to
hello@mongosheet.com