I am currently working on a NodeJS project using Mongoose queries.
A new requirement has come up which I am unable to understand how to tackle. I want to take a query string and search for this in all the collections of my database to return the objects that match this value.
This is how my collection looks:
Books:
{
"book_id": "ab12nld”,
"book_version": "0”,
"author": “Sam”,
“name”: “Sample Book”,
“comments”: “Done”
},
{
"book_id": "a5g2nld”,
"book_version": "1",
"author": "Martin",
"name": "Sample Book",
“comments”: “In Progress”
}
Cars:
{
"car_id": "nlp240n”,
"car_model": "3”,
"brand": “Hyundai”,
“name”: “Verna”,
“color”: “Blue”
},
{
"car_id": "cak201n”,
"car_model": "1”,
"brand": “Tata”,
“name”: “Indica”,
“color”: “Black”
}
Phones:
{
"phone_id": "n0830bo”,
"brand": “OnePlus”,
“name”: “7 Pro”,
“color”: “Red”
},
{
"phone_id": "bh97b01”,
"brand": “IPhone”,
“name”: “11 Pro”,
“color”: “Black”
}
I will get a query_string
which will be searched for in this collection and if there is a matching result, I should get all the related values.
Example:
If the query_string
is Martin
, I should get the corresponding result from the Books collection as Martin
is present only there:
{
"book_id": "a5g2nld”,
"book_version": "1",
"author": "Martin",
"name": "Sample Book",
“comments”: “In Progress”
}
If the query_string
is Black
, I should get the corresponding result from both Cars and Phones collections, as both these have the value Black
:
{
"car_id": "cak201n”,
"car_model": "1”,
"brand": “Tata”,
“name”: “Indica”,
“color”: “Black”
},
{
"phone_id": "bh97b01”,
"brand": “IPhone”,
“name”: “11 Pro”,
“color”: “Black”
}
This seems tricky and I don't know how to proceed forward. Any help will be appreciated.