What are the recommended ways to handle references to embedded documents in MongoDB? - mongodb

I'm new to MongoDB. Here's my problem: a user can have multiple avatars, but one and only one is active. Here's what a user document looks like for the moment:
{
"_id": ObjectId("515c99f7e4d8094a87e13757"),
"avatars": [{
"url": "http://example.com/img/photo1.jpg",
"width": 50,
"height": 50,
}, {
"active": true,
"url": "http://example.com/img/photo2.jpg",
"width": 50,
"height": 50,
}]
}
This solution is simple enough, but there are a few things I don't like:
changing the active avatar means updating two embedded documents
I'm not sure it will behave nicely in case of concurrent access (read_avatar+change_active_avatar or change_active+change_active) (will it?)
looking for the active avatar requires a sequential search
Another solution would be this:
{
"_id": ObjectId("515c99f7e4d8094a87e13757"),
"active_avatar_id": 2,
"avatars": [{
"_id": 1,
"url": "http://example.com/img/photo1.jpg",
"width": 50,
"height": 50,
}, {
"_id": 2,
"url": "http://example.com/img/photo2.jpg",
"width": 50,
"height": 50,
}]
}
This fixes problems 1&2, but not problem 3. And it adds an extra _id field in every embedded document. Plus when I insert a new avatar I now need to know what's the next _id to use (unless I use an objectId, but then it's a 12 bytes id for just a few hundred avatars (max).
So yet another solution could be this:
{
"_id": ObjectId("515c99f7e4d8094a87e13757"),
"active_avatar": {
"url": "http://example.com/img/photo2.jpg",
"width": 50,
"height": 50,
},
"extra_avatars": [{
"url": "http://example.com/img/photo1.jpg",
"width": 50,
"height": 50,
}]
}
Not much better than the first solution (it just fixes problem #3, that's it, and it's uglier).
All these solutions work, but I'm looking for "the right way" to do it. Any ideas? And what if I allow users to have multiple active avatars (whatever that would mean)?
Thanks.

Have you considered the document inside a document model?
{
"_id" : ObjectId("515c99f7e4d8094a87e13758"),
"active_avatar_id" : 2,
"avatars" : {
"1" : {
"url" : "http://example.com/img/photo1.jpg",
"width" : 50,
"height" : 50
},
"2" : {
"url" : "http://example.com/img/photo2.jpg",
"width" : 50,
"height" : 50
}
}
}

Related

Paggination on api data in flutter

i get data from API like demo:-
{draw: 0, records Total: 210, records Filtered: 210, data: [,…], input: []}
data: [,…]
draw: 0
input: []
records Filtered: 210
records Total: 210
I want to apply paggination on API data (grid view or list view)
How can i do that ?
Your json structure needs to look like this to apply pagination with ease.
Use current page, next page and last page to determine the next, current and remaining pages.
You can directly use the next link to get the data of the next page.
{
"meta": {
"page": {
"current-page": 2,
"per-page": 15,
"from": 16,
"to": 30,
"total": 50,
"last-page": 4
}
},
"links": {
"first": "http://localhost/api/v1/posts?page[number]=1&page[size]=15",
"prev": "http://localhost/api/v1/posts?page[number]=1&page[size]=15",
"next": "http://localhost/api/v1/posts?page[number]=3&page[size]=15",
"last": "http://localhost/api/v1/posts?page[number]=4&page[size]=15"
},
"data": [...]
}

Better way to display images in Magento's default API response?

I am using Magento2 default API: /V1/carts/mine/payment-information.
The response from this API is:
{
"payment_methods": [
{
"code": "payu",
"title": "PayUMoney"
},
{
"code": "checkmo",
"title": "Check / Money order"
},
{
"code": "paytm",
"title": "Paytm PG"
}
],
"totals": {
"grand_total": 195,
"base_grand_total": 195,
"subtotal": 45,
"base_subtotal": 45,
"discount_amount": 0,
"base_discount_amount": 0,
"subtotal_with_discount": 45,
"base_subtotal_with_discount": 45,
"shipping_amount": 150,
"base_shipping_amount": 150,
"shipping_discount_amount": 0,
"base_shipping_discount_amount": 0,
"tax_amount": 0,
"base_tax_amount": 0,
"weee_tax_applied_amount": null,
"shipping_tax_amount": 0,
"base_shipping_tax_amount": 0,
"subtotal_incl_tax": 45,
"shipping_incl_tax": 150,
"base_shipping_incl_tax": 150,
"base_currency_code": "INR",
"quote_currency_code": "INR",
"items_qty": 1,
"items": [
{
"item_id": 41,
"price": 45,
"base_price": 45,
"qty": 1,
"row_total": 45,
"base_row_total": 45,
"row_total_with_discount": 0,
"tax_amount": 0,
"base_tax_amount": 0,
"tax_percent": 0,
"discount_amount": 0,
"base_discount_amount": 0,
"discount_percent": 0,
"price_incl_tax": 45,
"base_price_incl_tax": 45,
"row_total_incl_tax": 45,
"base_row_total_incl_tax": 45,
"options": "[{\"value\":\"Green\",\"label\":\"Color\"},{\"value\":\"29\",\"label\":\"Size\"}]",
"weee_tax_applied_amount": null,
"weee_tax_applied": null,
"name": "Erika Running Short"
}
],
"total_segments": [
{
"code": "subtotal",
"title": "Subtotal",
"value": 45
},
{
"code": "shipping",
"title": "Shipping & Handling (Fixed)",
"value": 150
},
{
"code": "tax",
"title": "Tax",
"value": 0,
"extension_attributes": {
"tax_grandtotal_details": []
}
},
{
"code": "grand_total",
"title": "Grand Total",
"value": 195,
"area": "footer"
}
]
}
}
I want to add the images tag inside items to display images of the items/products. But this tag is not defined in the interface of items, i.e
TotalsItemInterface.php
I replicated TotalsItemInterface in my custom module and added all getters and setters from Totalsinterface along with setImages and getImages Tag. Thus internally I call the method to use and show it by my custom apiInterfaces.
Is there a better or a proper "Magento 2 way" if we want to change the data displayed in the APIs?
You may use extension attributes for it. You can find more information about them in the devdocs there:
https://devdocs.magento.com/guides/v2.3/extension-dev-guide/extension_attributes/adding-attributes.html

Bigger picture for facebook news feed

I tried the Facebook Graph API Explorer and looked into the news feed of a person with /[userid]/feed.
In this case, the person added a new cover photo which is at least 720px wide.
But in the news feed, all I found was a picture attribute containing a very small 130px wide thumbnail only.
Is there a way to access the full - or at least a bigger - picture within the API?
Thank you.
If you check the data coming back from the /[userid]/feed API call, you'll notice "object_id": "xxx" in the response.
If you make a second API call to this, e.g. /xxx/, you will get an array of images back with different sizes you can use. E.g.:
{
"id": "000",
"created_time": "...",
"from": {
...
},
"height": 223,
"icon": "https://fbstatic-a.akamaihd.net/rsrc.php/v2/yz/r/StEh3RhPvjk.gif",
"images": [
{
"height": 480,
"source": "https://fbcdn-sphotos-b-a.akamaihd.net/hphotos-ak-xfp1/t31.0-8/1912152_448929725237759_238021965_o.jpg",
"width": 1547
},
{
"height": 320,
"source": "https://fbcdn-sphotos-b-a.akamaihd.net/hphotos-ak-xfp1/t31.0-8/p320x320/1912152_448929725237759_238021965_o.jpg",
"width": 1031
},
{
"height": 130,
"source": "https://fbcdn-sphotos-b-a.akamaihd.net/hphotos-ak-xpa1/t1.0-9/p130x130/1013838_448929725237759_238021965_n.jpg",
"width": 420
},
{
"height": 225,
"source": "https://fbcdn-sphotos-b-a.akamaihd.net/hphotos-ak-xfp1/t31.0-8/p75x225/1912152_448929725237759_238021965_o.jpg",
"width": 725
}
],
"link": "https://www.facebook.com/147587828705285/photos/a.448909598573105.1073741827.147587828705285/448929725237759/?type=1",
"picture": "https://fbcdn-sphotos-b-a.akamaihd.net/hphotos-ak-xpa1/t1.0-9/s130x130/1013838_448929725237759_238021965_n.jpg",
"source": "https://fbcdn-sphotos-b-a.akamaihd.net/hphotos-ak-xfp1/t31.0-8/q71/s720x720/1912152_448929725237759_238021965_o.jpg",
"updated_time": "2014-03-12T19:58:08+0000",
"width": 720
}

Getting a High Resolution picture from Facebook API

Facebook FQL API for Photos states the maximum resolution for a photo returned from the API is 960x960:
The Photo object has an equivalent src connection. Using photo_src
FQL, you can retrieve images with the following dimensions: 960, 720,
480, 320, 180, 130, 75 pixels.
However, some images are uploaded at a higher resolution. Sometimes even much higher.
When browsing Facebook regularly, you can see these pictures and view their full size. However, I can't seem to find any way to get the original resolution in the API.
Is this possible and I have missed something? And if it's not - why?
Getting the max size of a picture
/USER_ID?fields=images
images gives back "an array of objects containing width, height, source each representing the various photo sizes". The result looks like this:
{
"data": [
{
"images": [
{
"height": 1536,
"width": 2048,
"source": "https://fbcdn-sphotos-c-a.akamaihd.net/hphotos-ak-prn1/s2048x2048/65169_XXXXXX_n.jpg"
},
{
"height": 720,
"width": 960,
"source": "https://fbcdn-sphotos-c-a.akamaihd.net/hphotos-ak-prn1/65169_44590146XXXXXXXXn.jpg"
},
{
"height": 540,
"width": 720,
"source": "https://fbcdn-sphotos-c-a.akamaihd.net/hphotos-ak-prn1/s720x720/65169_44XXXXXXX0984540_n.jpg"
},
{
...
},
{
"height": 97,
"width": 130,
"source": "https://fbcdn-photos-a.akamaihd.net/hphotos-ak-prn1/s75x225/65169_44XXXXX_s.jpg"
}
],
}
]
}
Getting the max size of a profile picture
Try with more than 960, i.e 961. You'll get the maximum size of the picture, if available!
/USER_ID?fields=picture.height(961)
Result:
{
"id": "PROFILE_ID",
"picture": {
"data": {
"url": "https://fbcdn-profile-a.akamaihd.net/hprofile-ak-prn1/615962_4486XXXXXXXXX3_601495975_o.jpg",
"width": 1536,
"height": 2048,
"is_silhouette": false
}
}
}

Items in the images array of the facebook photo object does not contain a source anymore

We were using photo's from facebook in our application until we noticed today that there was no longer a source field available for the different sizes of the image.
I saw in the facebook documentation that there should be a field "source" for every item in the images array.
images | The 4 different stored representations of the photo |
Requires access_token | array of objects, containing height, width,
and source fields
Here's the json data i get from facebook:
{
"id": "10150916781197589",
"from": {
"name": "Thomas Dekiere",
"id": "641457588"
},
"name": "antwerpen",
"picture": "http://photos-b.ak.fbcdn.net/hphotos-ak-snc7/383569_10150916781197589_641457588_12988491_2098011643_s.jpg",
"source": "http://a2.sphotos.ak.fbcdn.net/hphotos-ak-snc7/s720x720/383569_10150916781197589_641457588_12988491_2098011643_n.jpg",
"height": 720,
"width": 478,
"images": [
{
"width": 1360,
"height": 2048
},
{
"width": 637,
"height": 960
},
{
"width": 478,
"height": 720
},
{
"width": 318,
"height": 480
},
{
"width": 212,
"height": 320
},
{
"width": 180,
"height": 271
},
{
"width": 86,
"height": 130
},
{
"width": 86,
"height": 130
}
],
"link": "http://www.facebook.com/photo.php?fbid=10150916781197589&set=a.10150916780582589.528576.641457588&type=1",
"icon": "http://static.ak.fbcdn.net/rsrc.php/v1/yz/r/StEh3RhPvjk.gif",
"created_time": "2012-04-04T13:42:09+0000",
"position": 1,
"updated_time": "2012-04-04T13:42:19+0000"
}
as you can see, there's no source field in the images array items.
Is this normal behavior now and is the documentation out dated or is this a facebook bug?
There was a bug in the facebook service:
http://developers.facebook.com/bugs/328741343854109
It has been solved :)