Automated reply with Whatsapp Web JS + Express JS Part 2

Ady Bagus
5 min readJul 22, 2023

--

Today's story, we are going to talk about MongoDB to replace our JSON files to save data. for you guys who haven't followed my last story, you can check it out here :)

Why MongoDB??

MongoDB is a document-oriented database that can store non-structural information in it. so basically when you're making a table on the structural database as an example is going to be like this.

CREATE TABLE parents (
id NUMBER,
name VARCHAR2(100),
address VARCHAR(100)
)

and your data when selected, inserted, and updated will have the same column (id, name, address).

when using MongoDB, you can save data with different columns in each row. please note that in implementation, it's better to make each row have the same column (recommendation) but if you want to make it slightly different it's okay. so we will implement MongoDB in our project as source data. oh right, here is the example of data stored in MongoDB

MongoDB Sample Data

It is the special thing about MongoDB, you can store nested data inside data. according to MongoDB documentation, it can do 100 Levels of nested data. To install MongoDB on your local server you can use MongoCompass. you can see the instructions on the MongoDB page or on this article by London App Brewery

Creating a New Database and Collection

The first step you need to do to make a database is connecting to the MongoDB. to do this, first open the MongoDB Compass to start a local connection to the local database.

You can simply press “Connect” button to start local database server. then you will be redirected to the databases page. in here you can create a new database. on start you will see 3 databases, this database is defaulted by the app.

Database

Here you can see, there is “whatsapp” database. that's my database, you can make a new database with another name as you need. The next step is creating collections, collection is the same as table on a regular database.

Here the example collection, I named it “parents” like the old JSON file that I used to store parent numbers. after you make your collection, try to click your collection. Here you will see a blank page, which means you don't have any data inside your collection. To create new data, you can see the cmd line on the bottom side of the MongoDB Compass.

MongoDB Compass CMD

To make it larger so you can see more lines simply pull the CMD line upwards then it will look like this.

here you can see, that your current database used is test. To change it to your database you can type “use whatsapp”, as an example my database name is whatsapp.

to add new data, type command

db.parents.insert([{ name: "new name", phone: 123123 }])

code descriptions:
db.[collection name].insert({ your data })

to see your inserted data, use find() command. to do this you can use

db.parents.find()

find() basically showing all of your data. what if you want to make it more specific? you can add parameters inside the find() function. To do this, here's my data example.

{
_id: ObjectId("64bbb3672b4b6fb59d8bbed9"),
name: 'hehe',
age: '12'
}
{
_id: ObjectId("64bbb37f2b4b6fb59d8bbeda"),
lastname: 'hoho',
address: 'ansdkwl'
}
{
_id: ObjectId("64bbc2f3339f712a2eff681a"),
name: 'John Doe',
address: '19 Quai de la Tournelle, 75005 Paris, France',
phone: '6282345678900',
children: {
name: 'Laura Doe',
class: '3rd Grade'
}
}
{
_id: ObjectId("64bbc2f3339f712a2eff681b"),
name: 'Loid Forger',
address: '128 Park Avenue, West Berlint ',
age: '30',
phone: '6282345678900',
children: {
name: 'Anya Forger',
class: '3rd Grade'
}
}
{
_id: ObjectId("64bbd20cafcae4e3ba0f2c5e"),
name: 'new name',
phone: 123123
}

as example “I want parent data with age 30”, then your query will be like this

the code above means you're finding data with field name “age” and the field value is “30”. but what if you want to find data on the nested one? this case we want data with children name is Laura Doe, so we need to put a parameter like this.

db.parents.find({ "children.name": "Laura Doe" })

to find nested object, you need to locate the object first. here as you can see name is under children object, to pull the name inside the children object you need to call the parent too which mean “children.name” so you can specify your target parameter.

Now we know the MongoDB basic, next story I will talk about how we connect MongoDB with Node JS. stay tune guys 👋

--

--

Ady Bagus

Software Quality Assurance | Backend Developer | PHP Developer