'lowdb - how to query multiple records?
I'm using https://github.com/typicode/lowdb for a small project. I need a query which searches multiple records and returns each record it finds as an array.
For example, for a db such as:
"records": [
{
"id": "sadohfdsf",
"username": "user1",
"data": "abc"
},
{
"id": "tiyuykuy",
"username": "user1",
"data": "xyz"
},
{
"id": "tryehhrt",
"username": "user2",
"data": "tyu"
}
]
I'd like to query all the records for username: "user1" and get all the records for that user in an array.
I've tried the default:
const user = await db.get('records')
.find({ username: "user1" })
.value();
return user;
but it only finds the first record in the db.
What's the correct way to find multiple records in lowdb?
Solution 1:[1]
Use filter instead of find .
const users = await db('records').filter({ username: "user1" });
Ref: https://github.com/typicode/lowdb/issues/185 Left join with lowdb
Multipe filters means use chain() as well
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Senthil |
