I am using RestHeart to expose CRUD operations from MongoBD. And trying to invoke the Rest API from AngularJS and get the JSON result as in below JSON string. But I am interested only in the name, age & city fields which I stored in the MongoDB.
I am not sure how to get these values.
Javascript code:-
crudApp.controller('listController', function($scope, $http, $location,
crudService) {
$http.get('http://localhost:8081/jaydb/employees').success(
function(response) {
console.log('response : ' +JSON.stringify(response));
$scope.employees = response;
});
})
JSON Result from REST API
{
_embedded: {
rh: doc: [
{
_embedded: {
},
_links: {
self: {
href: "/jaydb/employees/55c1e7c41c49a8cd78818bc7"
},
rh: coll: {
href: "/jaydb"
},
curies: [
{
href: "http://www.restheart.org/docs/v0.10/#api-doc-{rel}",
name: "rh"
}
]
},
_type: "DOCUMENT",
_id: {
$oid: "55c1e7c41c49a8cd78818bc7"
},
name: "Anupama",
city: "Trichy",
age: 25,
_etag: {
$oid: "55c1e7c41c49a8cd78818bc8"
},
_lastupdated_on: "2015-08-05T10:39:00Z",
_created_on: "2015-08-05T10:39:00Z"
},
{
_embedded: {
},
_links: {
self: {
href: "/jaydb/employees/55c1e7ae1c49a8cd78818bc5"
},
rh: coll: {
href: "/jaydb"
},
curies: [
{
href: "http://www.restheart.org/docs/v0.10/#api-doc-{rel}",
name: "rh"
}
]
},
_type: "DOCUMENT",
_id: {
$oid: "55c1e7ae1c49a8cd78818bc5"
},
name: "Sujatha",
city: "Chennai",
age: 24,
_etag: {
$oid: "55c1e7ae1c49a8cd78818bc6"
},
_lastupdated_on: "2015-08-05T10:38:38Z",
_created_on: "2015-08-05T10:38:38Z"
},
{
_embedded: {
},
_links: {
self: {
href: "/jaydb/employees/55c1e7981c49a8cd78818bc3"
},
rh: coll: {
href: "/jaydb"
},
curies: [
{
href: "http://www.restheart.org/docs/v0.10/#api-doc-{rel}",
name: "rh"
}
]
},
_type: "DOCUMENT",
_id: {
$oid: "55c1e7981c49a8cd78818bc3"
},
name: "Soniya",
city: "Ernakulam",
age: 22,
_etag: {
$oid: "55c1e7981c49a8cd78818bc4"
},
_lastupdated_on: "2015-08-05T10:38:16Z",
_created_on: "2015-08-05T10:38:16Z"
},
{
_embedded: {
},
_links: {
self: {
href: "/jaydb/employees/55c1e7711c49a8cd78818bc1"
},
rh: coll: {
href: "/jaydb"
},
curies: [
{
href: "http://www.restheart.org/docs/v0.10/#api-doc-{rel}",
name: "rh"
}
]
},
_type: "DOCUMENT",
_id: {
$oid: "55c1e7711c49a8cd78818bc1"
},
name: "Reshma",
city: "Trivandrum",
age: 21,
_etag: {
$oid: "55c1e7711c49a8cd78818bc2"
},
_lastupdated_on: "2015-08-05T10:37:37Z",
_created_on: "2015-08-05T10:37:37Z"
},
{
_embedded: {
},
_links: {
self: {
href: "/jaydb/employees/55c1d3a8b216e0710f8ee0ab"
},
rh: coll: {
href: "/jaydb"
},
curies: [
{
href: "http://www.restheart.org/docs/v0.10/#api-doc-{rel}",
name: "rh"
}
]
},
_type: "DOCUMENT",
_id: {
$oid: "55c1d3a8b216e0710f8ee0ab"
},
name: "Michael",
city: "Tokyo",
age: 23,
_created_on: "2015-08-05T09:13:12Z"
}
]
},
_links: {
},
_type: "COLLECTION",
_id: "employees",
_created_on: "2015-08-05T09:38:36Z",
_etag: {
$oid: "55c1d99c1c49a8cd78818bb6"
},
_lastupdated_on: "2015-08-05T09:38:36Z",
_collection-props-cached: false,
_returned: 5
}
Ref: http://restheart.org/docs/walkthrough.html
RESTHeart uses the HAL format, have a look at the resource representation section of the restheart documentation for more information.
In summary, your request is a GET to a collection resource: the response includes the collection's own properties at the first level and its documents as embedded resources.
You access the collection's documents via the _embedded property that includes the array rh:doc; the element of this array are the your documents.
Also note that the documents are paginated: by default RESTHeart returns the first (up to) 1000 documents. You can control the pagination via the page and pagesize query parameters.
If you also pass the count query parameter, you'll get the _size and _total_pages properties.
The _links parameter includes the next and eventually the previous links that point to the next and previous page.
Related
I have a array of objects and then inside each of the object I have another array, I want to group the data inside the object array according to their category name. I tried doing it with a reduce function but it is basically giving me the same data(it is not even transformed), It's like it is not even calling.
My Data
data = [
{
name: "listName",
id:434343,
data: [
{
id:434343,
categoryName: school,
},
{
id:234343,
categoryName: house,
},
{
id:2000,
categoryName: school,
},
{
id:2333,
categoryName: house,
}
]
},
{
name: "anotherListName",
id:434343,
data: [
{
id:434343,
categoryName: school,
},
{
id:234343,
categoryName: house,
},
{
id:2000,
categoryName: school,
},
{
id:2333,
categoryName: house,
}
]
}
]
my code
list.forEach(d =>
d.data.reduce((acc, currrent) => {
(acc[currrent.categoryName] = acc[currrent.categoryName] || []).push(
currrent
)
return acc
}, {})
)
How I want the data to be transformed
transformed = [
{
name: "listName",
id:43453,
data: {
school: [
{
id:434343,
categoryName:school
},
{
id:2000,
categoryName:school
},
],
house: [
{
id:234343,
categoryName:house
},
{
id:2333,
categoryName:house
},
],
}
},
{
name: "listName",
id:43453,
data: {
school: [
{
id:434343,
categoryName:school
},
{
id:2000,
categoryName:school
},
],
house: [
{
id:234343,
categoryName:house
},
{
id:2333,
categoryName:house
},
],
}
},
]
you can do something like this using map and reduce
const transform = data => data.map(({name, id, data}) => {
return {
name,
id,
data: data.reduce((res, {id, categoryName}) => {
return {
...res,
[categoryName]: [...(res[categoryName] || []), {id, categoryName }]
}
}, {})
}
})
const data = [{
name: "listName",
id: 434343,
data: [{
id: 434343,
categoryName: 'school',
},
{
id: 234343,
categoryName: 'house',
},
{
id: 2000,
categoryName: 'school',
},
{
id: 2333,
categoryName: 'house',
}
]
},
{
name: "anotherListName",
id: 434343,
data: [{
id: 434343,
categoryName: 'school',
},
{
id: 234343,
categoryName: 'house',
},
{
id: 2000,
categoryName: 'school',
},
{
id: 2333,
categoryName: 'house',
}
]
}
]
const result = transform(data)
console.log(result)
So I am looking through the Prisma docs and I come across an example to create a relational query. This query inserts a new post and assigns it an author with an existing category.
const assignCategories = await prisma.post.create({
data: {
title: 'How to be Bob',
categories: {
create: [
{
assignedBy: 'Bob',
assignedAt: new Date(),
category: {
connect: {
id: 9,
},
},
},
{
assignedBy: 'Bob',
assignedAt: new Date(),
category: {
connect: {
id: 22,
},
},
},
],
},
},
})
I can understand what this query does but I don't understand how to implement this on a backend with the incoming request body.
Suppose I have this request body
{
"title": "how to be bob",
"categories": [
{
"assignedby": "bob",
"category": {
"id": 9
}
},
{
"assignedby": "bob",
"category": {
"id": 22
}
}
]
}
How do I transform this request body to the data object in the first codeblock?
I got it. It was in my face all along. Just use .map to map through the categories
const data = {
title: 'how to be bob',
categories: [
{
assignedby: 'bob',
category: {
id: 9,
},
},
{
assignedby: 'bob',
category: {
id: 22,
},
},
],
};
const mappedData = {
title: data.title,
categories: {
create: data.categories.map((i) => ({
assignedBy: i.assignedby,
assignedAt: new Date(),
category: {
connect: {
id: i.category.id,
},
},
})),
},
};
console.log(mappedData);
which logs this
"title": "how to be bob",
"categories": {
"create": [
{
"assignedBy": "bob",
"assignedAt": "2021-10-24T12:10:00.397Z",
"category": {
"connect": {
"id": 9
}
}
},
{
"assignedBy": "bob",
"assignedAt": "2021-10-24T12:10:00.397Z",
"category": {
"connect": {
"id": 22
}
}
}
]
}
}
Just what we exactly need.
i want group by value and my problem is screen_size and screen_resulation is changeable
is there is something like wildcard in nested document in mongodb ?
i have tried this but its not working
[
{
filters: {
screen_size: {
name: "حجم الشاشة",
value: "50",
},
screen_resulation: {
name: "دقة الشاشة",
value: "4k",
},
},
},
{
filters: {
screen_resulation: {
name: "دقة الشاشة",
value: "UHD",
},
screen_size: {
name: "حجم الشاشة",
value: "55",
},
},
},
{
filters: {
screen_resulation: {
name: "دقة الشاشة",
value: "4k",
},
screen_size: {
name: "حجم الشاشة",
value: "55",
},
},
},
];
{
$group : {
_id: "$filters.*.value", counter: { $sum: 1 }
}
}
You can get the desired result with the below approach as well:
[{$project: {
_id: '$_id',
filters: { $objectToArray: '$filters' }
}}, {$unwind: {
path: '$filters'
}}, {$group: {
_id: '$filters.v.value',
count: {$sum: 1}
}}]
I am trying to compare two objects using lodash like below. The problem is that it always returns false. I think that the issue is that the objects have different order of keys and values. I however couldn't find a solution on how to compare it regardless on the order.
How to ignore the order and compare the two objects correctly?
var obj1 = {
event: 'pageInformation',
page: { type: 'event', category: 'sportsbook' },
username: 'anonymous',
pagePath: '/',
item: { name: 'Barcelona - Leganes', id: '123' },
contest: { name: '1.Španielsko', id: 'MSK70' },
category: { name: 'Futbal', id: 'MSK3' },
teams: [
{ id: 'barcelona', name: 'Barcelona' },
{ id: 'leganes', name: 'Leganes' }
]
}
var obj2 = {
event: 'pageInformation',
page: { type: 'event', category: 'sportsbook' },
username: 'anonymous',
pagePath: '/',
category: { id: 'MSK3', name: 'Futbal' },
contest: { name: '1.Španielsko', id: 'MSK70' },
item: { id: '123', name: 'Barcelona - Leganes' },
teams: [
{ name: 'Barcelona', id: 'barcelona' },
{ name: 'Leganes', id: 'leganes' }
]
}
function compareObjects(obj1, obj2){
return _.isMatch(obj1, obj2);
}
You can use the isEqual function which does a deep equal check (regardless of key order):
_.isEqual(obj1, obj2)
See more here: https://lodash.com/docs/2.4.2#isEqual
I'm a beginner meteor. I create a blog with many posts(article) by each categories. This is my Collection:
if (Category.find().count() === 0) {
[
{
name: 'IT',
sub_categories: [
{ name: 'Java' },
{ name: 'PHP' },
{ name: '.NET' },
{ name: 'Android/iOS' },
{ name: 'HTML/CSS' },
{ name: 'Javascript and Framworks' },
{ name: 'Ruby on Rails' },
],
},
{
name: 'ENGLISH',
sub_categories: [
{ name: 'Listening' },
{ name: 'Speaking' },
{ name: 'Writing' },
{ name: 'Reading' },
{ name: 'Topic' },
],
},
{
name: 'SoftSkill',
sub_categories: [
{ name: 'CV and CL' },
{ name: 'Presentation' },
{ name: 'Teamwork' },
],
},
{
name: 'ENTERTAINMENT',
sub_categories: [
{ name: 'Sports' },
{ name: 'Games' },
{ name: 'Music & Videos' },
{ name: 'Fashion' },
{ name: 'Talk show' },
],
},
].forEach(doc => {
Category.insert(doc);
});
}
After I save all posts by each categories, Now I want to show it throught select option
Details:
When I click on "IT" button -> app show all sub_categories. Then I choose one option (example "Java") it will show all posts have sub_categories = "Java". Can you help me to do it?
You can use navbar for your requirement.
In that navbar you can have dropdown as navbar element
Please Follow this steps if it is useful for you