Json-server query table based on data from another table - json-server

I have json-table like this:
"articles": [
{
"id": "1",
"title": "What is Lorem Ipsum?",
"body": "Lorem Ipsum is simply...",
"userId": "1"
}],
"users": [
{
"id": "1",
"name": "fake user",
"email": "fake#gmail.com",
"password":"$2a$12$yir.7d.1gWsvNcFPIdf8Gu52XWUL4br4a7l7Qbid6QadL1TClXOMa"
}],
"favorites": [
{
"id": "1",
"articleId": "1",
"userId": "1"
}]
Simple query for data that is based only on data from one table works.
For example:
http://localhost:3000/articles?userId=1
Now I would like to make a query that would fetch all articles that the specyfic user has added to its favorites list. Something like this:
http://localhost:3000/articles?id=favorites.articleId&favorites.userId=1
Are there better alternatives to json-server?

Related

Optimise postgres database table design

I am looking to optimise my database. The current design is one table company which is ID and a JSONB payload which can contain the type of company, address, leaders etc.
ID
Payload
6edf43d2-565b-4cad-9419-1bbb61441d7c
JSONB
fb6a649d-3aa6-42f0-a0f5-ea49b0e6dd33
JSONB
The JSONB payload looks something like this for a specific company:
{
"type": "business",
"leaders": [
{
"id": "01f6dcd0-02d4-11eb-b9cb-c7896e45862d",
"name": {
"title": "Mr",
"lastName": "one",
"firstName": "leader"
},
"contactMethods": [
{
"name": "email",
"type": "email",
"email": "leaderone#lead.com"
},
{
"name": "landline",
"type": "phone",
"number": "234234234",
"countryCode": 64
}
]
},
{
"id": "2bd9abe0-02d4-11eb-b9cb-c7896e45862d",
"name": {
"title": "Mrs",
"lastName": "two",
"firstName": "leader"
},
"contactMethods": [
{
"name": "email",
"type": "email",
"email": "leadertwo#lead.com"
},
{
"name": "landline",
"type": "phone",
"number": "234234234",
"countryCode": 64
}
]
},
{
"id": "35a09210-02d4-11eb-b9cb-c7896e45862d",
"name": {
"title": "Mrs",
"lastName": "three",
"firstName": "three"
},
"contactMethods": [
{
"name": "email",
"type": "email",
"email": "leaderthree#lead.com"
},
{
"name": "landline",
"type": "phone",
"number": "234234234",
"countryCode": 64
},
{
"name": "mobile",
"type": "phone",
"number": "123",
"countryCode": 64
}
]
},
],
"addresses": [
{
"id": "01f6dcd1-02d4-11eb-b9cb-c7896e45862d",
"type": "Australia",
"country": "AU",
"postcode": "2025"
},
{
"id": "f6aa5550-2a15-11eb-8914-5fa8f55d3b03",
"type": "NewZealand",
"country": "NZ",
"postcode": "239059",
"streetName": "Martin Road",
"streetNumber": "38A"
}
],
"createdAt": "2020-09-30T04:23:00.335780909Z",
"legalName": "Company A",
"updatedAt": "2021-05-27T06:16:37.733462415Z",
}
A company has many leaders and I am storing the contact method per leader. The company can also have many addresses. Now, the leaders keep chopping and changing and at the moment if I want to edit a leader I am needing to update the whole company payload, same with addresses. I am also trying to performance test my design by adding 10 leaders at at time however timeouts occur which I suspect is due to the same issue.
I am looking to re-design this structure optimally, I was thinking to have a separate table per entity (leader, address etc) which can reference back to the company table by an ID. However, in order to initially create a company it is mandatory to have at least one leader and one address so it doesn't seem to make sense to have a leader or address table to exist on its own.
What is the best performant design?
You are right, you should normalize the data model and avoid JSON in this case. If an address is missing in the data, simply set the corresponding foreign key to NULL to indicate that the company has no known address (yet). Complain to your data source about the lack of data quality :^)

MongoDB lookup with multiple nested levels

In my application, I have a section of comments and replies under some documents.
Here's how my database schema looks like
db.updates.insertOne({
"_id": "62347813d28412ffd82b551d",
"documentID": "17987e64-f848-40f3-817e-98adfd9f4ecd",
"stream": [
{
"id": "623478134c449b218b68f636",
"type": "comment",
"text": "Hey #john, we got a problem",
"authorID": "843df3dbbdfc62ba2d902326",
"taggedUsers": [
"623209d2ab26cfdbbd3fd348"
],
"replies": [
{
"id": "623478284c449b218b68f637",
"type": "reply",
"text": "Not sure, let's involve #jim here",
"authorID": "623209d2ab26cfdbbd3fd348",
"taggedUsers": [
"26cfdbbd3fd349623209d2ab"
]
}
]
}
]
})
db.users.insertMany([
{
"_id": "843df3dbbdfc62ba2d902326",
"name": "Manager"
},
{
"_id": "623209d2ab26cfdbbd3fd348",
"name": "John"
},
{
"_id": "26cfdbbd3fd349623209d2ab",
"name": "Jim"
},
])
I want to join those two collections, and replace user ids with complete user information on all levels. So the final JSON should look like this
{
"_id": "62347813d28412ffd82b551d",
"documentID": "17987e64-f848-40f3-817e-98adfd9f4ecd",
"stream": [
{
"id": "623478134c449b218b68f636",
"type": "comment",
"text": "Hey #john, we got a problem",
"author": {
"_id": "843df3dbbdfc62ba2d902326",
"name": "Manager"
},
"taggedUsers": [
{
"_id": "623209d2ab26cfdbbd3fd348",
"name": "John"
}
],
"replies": [
{
"id": "623478284c449b218b68f637",
"type": "reply",
"text": "Not sure, let's involve #jim here",
"author": {
"_id": "623209d2ab26cfdbbd3fd348",
"name": "John"
},
"taggedUsers": [
{
"_id": "26cfdbbd3fd349623209d2ab",
"name": "Jim"
}
]
}
]
}
]
}
I know how to do the $lookup on the top-level fields, including pipelines, but how can I do with the nested ones?

update multiple documents of specific field values based on _Id

i have the two sample documents as :
[
{
"_id": "605b2fcb526b8b609ef97eaa",
"comments": "",
"name": "",
"user_name": ""
},
{
"_id": "605b3034asubc2bed542f88f",
"comments": "",
"name": "",
"user_name": ""
}
]
and the updated data for these two documents for key comment i have as :
[
{
"_id": "605b2fcb526b8b609ef97eaa",
"comments": "test one",
"name": "",
"user_name": ""
},
{
"_id": "605b3034asubc2bed542f88f",
"comments": "test two",
"name": "",
"user_name": ""
}
]
Here I am currently trying using a loop by ID and update the respective comment value using update
May i know how could i use update_many in this scenario ?
Any guiding links or a solution is much appreciated TIA
There is no way you can update different values to the same field in different documents in a single query to Mongo (update_many or otherwise).
You need to use a loop or you can do a Bulk.

Populate one path of an object nested inside array for specific fileds

My Posts Schema has a "comments" array path, for each "comment", "user" is objectId to User Modal. How could I populate only "name", "_id", "thumbnailURI" for the user.
what the posts db looks like:
[
...
{
"_id": "5ce5b41222fa4937f8a495e5",
"owner": "5ce5b41123456789f8a495e5",
"uri": "uri.jpg",
"comments": [
{
"date": "2019-05-23T23:24:47.554Z",
"_id": "5ce72bbf88781b35fca696ce",
"user": "5cdda3a4f5a8a077a9352f38",
"content": "cute!!!"
},
{
"date": "2019-05-23T23:28:52.941Z",
"_id": "5ce72cb46fba6f1bece267db",
"user": "5cdda3a4f5a8a077a9352f38",
"content": "cute!!!"
}
],
"createAt": "2019-05-22T20:41:54.290Z",
"__v": 0
},
...
]
I want the "comments" looks like this:
"comments": [
{
"date": "2019-05-23T23:24:47.554Z",
"_id": "5ce72bbf88781b35fca696ce",
"user": {
"name": "name",
"_id": "5ce72cb46fb5ce72cb46fb"
"thumbnailURI": "thumbnailURI.jpg"
},
"content": "cute!!!"
},
{
"date": "2019-05-23T23:28:52.941Z",
"_id": "5ce72cb46fba6f1bece267db",
"user": {
"name": "name",
"_id": "5ce72cb46fb5ce72cb46fb"
"thumbnailURI": "thumbnailURI.jpg"
},
"content": "cute!!!"
}
],
Tried
posts
.getByOwners(owners)
.populate('owner', 'username thumbnail _id')
.populate({
path: "comments",
populate: {
path: "user",
model: "user",
}
});
get empty "comments" array
Thank you!
Thanks for the advises.
I have a "user" model with some paths
There is no "comments" model. "comments" is an array embedded in "post" model.
The query looks like this:
posts
.findById(id)
.populate('owner', 'username thumbnail _id')
.populate("comments.user", 'username thumbnail _id')
This is the final result I want:
"comments": [
{
"date": "2019-05-23T23:24:47.554Z",
"_id": "5ce72bbf88781b35fca696ce",
"user": {
"thumbnail": "pic1.jpg",
"_id": "5cdda3a4f5a8a077a9352f38",
"username": "a"
},
"content": "cute!!!"
},
{
"date": "2019-05-23T23:28:52.941Z",
"_id": "5ce72cb46fba6f1bece267db",
"user": {
"thumbnail": "pic2.jpg",
"_id": "5cdda3a4f5a8a077a9352f38",
"username": "a"
},
"content": "cute!!!"
}
],

How to get Friends uid from tagged photo?

I can get names from field "text" of table "photo" but
I cant use those names to get uids.
How can I get uids of tagged people in photo??
all you have to do is query the graph api like this :
https://graph.facebook.com/{photo_id} and you will recieve the data you need...
{
"id": "{photo_id}",
"from": {
"name": "{user_that_uploaded_the_photo}",
"id": "123"
},
"tags": {
"data": [
{
"id": "123",
"name": "Lior",
"x": 48.5099,
"y": 37.5276,
"created_time": "2011-10-11T05:50:35+0000"
},
{
"id": "456",
"name": "Shlain",
"x": 19.0397,
"y": 35.3201,
"created_time": "2011-10-11T05:50:40+0000"
},
{
"id": "789",
"name": "Abigail",
"x": 76.8212,
"y": 47.4614,
"created_time": "2011-10-11T05:50:32+0000"
}
...
You can use the graph api explorer to experiment with these methods...
good luck!