CRUD operations in MongoDB, i.e. Create, Read, Update, Delete.

The code is for Pymongo.

Preparation

1
2
3
4
5
6
7
8
9
10
11
import datetime
from pymongo import MongoClient

client = MongoClient()
db = client.test_database
collection = db.test_collection

post = {'author': 'Mike',
'text': 'My first blog post!',
'tags': ['mongodb', 'python', 'pymongo'],
'date': datetime.datetime.utcnow()}

CREATE:

1
2
3
4
def create_lines():
collection.insert_one(post)
# if we have multiple posts (an array of dictionaries)
collection.insert_many(posts)

READ:

1
2
3
4
def read_lines():
collection.find_one({'author': 'Mike'})
# if we want to find multiple records at the same time
collection.find_many({'author': 'Mike'})

UPDATE:

1
2
3
4
5
6
def update_lines():
collection.update_one({'author': 'Mike'},
{'$set': {'text': 'My second blog post!'}})
# if we want to update all the relevant records at the same time
collection.update_many({'author': 'Mike'},
{'$set': {'text': 'My second blog post!'}})

DELETE:

1
2
3
4
def delete_lines():
collection.delete_one({'author': 'Mike'})
# if we want to delete the matched records all at once
collection.delete_many({'author': 'Mike'})

I would say MongoDB is designed in a way that is very user-friendly.

Check out the pymongo website for more detailed instructions.
https://pymongo.readthedocs.io/en/stable/tutorial.html

Make the database in a simpler way and make it fast instead of designing complicated architecture of tables. MongoDB can do this with its document model to store things more naturally which can be later fit into your applications. Remember, it’s fast. I think it can make your life easier.

Sharding is basically to break up your data across servers to improve the performance and/or infrastructure.

A replica set of MongoDB for example is replicating your data on any of your servers while sharding is breaking up the data across your servers. The process is called MongoS using Shard Key. When you connect and send queries to MongoDB, MongoS will look up the data and get it from the right place and also merge data if necessary because they can be on different servers.

You can google it for more details, maybe you can find the wikipedia page if you are curious enough. Anyway, put it in a simple way, the paradox is talking about the contradiction between the reality that we have not met any aliens and the high probability there should be so many aliens in the cosmos because the universe is so big.

It occurs to me that when a society becomes more civilized, it kind of loses the impulse to invade as one way of exploration. You need to treat all those living things equally because you can see the tendency that we treat with each other more fairly no matter who you are, where you come from, or what are you. In the end, everyone lives happily in their chambers and forgets the big universe exists.

Or maybe, there could be a pandemic out there in the universe, everyone during this period of time needs to stay low and keep quiet. It is equivalent to the dark forest theory I know. But the twist here is that the pandemic can end, and it will.

No, I am not talking about Shannon’s Noisy-Channel Coding Theorem.

By information, I mean news in general. Because we are exposed to too much information on a daily basis, most of them become noise in our life. For example, it won’t hurt if you miss all the news for a week or a month. Your life goes on. And you know what, the most important news to you will find you anyway.

The world is watching Oprah’s interview with Harry and Meghan. And the media is waiting for the royal family to respond.

Based on what I have witnessed (on television of course), they are being normal people and they are being a normal family with normal family issues. People may judge or gossip the royal family members because they are some kind of celebrity. That’s fine when people don’t have anything meaningful in daily conversasion why not talking about someone else’s life.

But keep in mind they are just humans. At the end of the day, everyone has their own struggles in life, everyone has their own wars to fight.

I feel connected with the young couple, and I am very happy they got the courage to make the change.

I am not sure if I have mentioned this or not, CS50 courses are awesome. Great resources if you are looking for a place to start learning your CS knowledge. It is an entry-level course taught by David J. Malan in Harvard, and it is free. I think CS50 has motivated me in a way and convinced me that CS is something worth learning and doing as a career. Thanks to the internet, we can have the learning experience with such a wonderful teacher David Malan. Please google the course links or watch it on Youtube.

If you really want to be a coder, do it today.

I guess it is interesting to see a 4K video which is upscaled from an original 720p video. We can achieve this using ffmpeg.

1
ffmpeg -i input_720p.ts -vf scale=3840x2560:flags=lanczos -c:v libx264 -preset slow -crf 21 output_4k.ts

Even if you can do this, still, my suggestion will be, DON’T do it.

Something you don’t do in an elevator pitch.

“I can tell you what happended during my university, it was blablabla”

“Hi, which floor you want to go, ok, you have missed your floor already.”

“We are here to talk, forget about pressing the buttons.”

“I have no idea why I am here.”

“How about let me sing a song for you.”

“No, I am waiting for you to introduce yourself.”

“What do you think about the weather?”

“Have you watched the new movie?”

“You look like someone from an interview panel.”

Previously, I listened to the topic of remote working in one of the podcasts. It is something new to me if the work is entirely remote, but I would say to some extent, we need to get used to this concept of remote working with colleagues in a team. In the beginning, you might feel the pain that when you need to talk to someone to get things done, but other people are living in different time zones, which means they are still in their sleep. So you have to wait.

After the pandemic, I think people will realize that most of the office work can be done anyway at home. So finding a better way of collaboration among people in different places is one of the most important things to do. You can write more emails. Maybe.

0%