This repository has a comparison between searching the database using and not using streams
container
script
cpu
mem
delay (s)
mongo
paginated
± 49%
± 37%
16.481s
node
paginated
± 50%
± 16.8%
16.481s
mongo
stream
± 5%
± 37%
3.497s
node
stream
± 50%
± 15.5%
3.497s
Hardware: i5 11400F (6 cores, 12 threads) 16GBs of RAM
I've added a mongo.zip file inside mongo folder in the root of the project. Just extract the content in mongo folder and run the scripts.
Running Without Docker Compose
First step is run your MongoDB locally.
Is necessary to change the domain to localhost in src/database/conn.mjs
docker run -d \
--name mongodb \
-v $PWD /mongo:/data/db \
-e MONGO_INITDB_ROOT_USERNAME=root \
-e MONGO_INITDB_ROOT_PASSWORD=root \
-p 27017:27017 \
mongo
If failed to connect to the database, add the user to the database which we will use in the mongo shell
use example_db
db . createUser (
{
user : "root" ,
pwd : "root" ,
roles : [
{ role : "readWrite" , db : "example_db" }
]
}
)
The second step is add test mass in your database
# Each execution adds 50.000 new users
npm run populate
The last step is run the queries and compare the execution time
# Paginated query
npm run paginated
# Stream query
npm run stream
Running With Docker Compose
docker build -t node-example .
docker compose up -d --build
Enter in "node-example" container
docker exec -it node-example bash
Go to application and run the execution commands
cd /app
# Paginated query
npm run paginated
# Stream query
npm run stream