If I have records for users and also records for orders, it is proper to save them like that:
{
"users": [
{"id": "1", "name": "Allan", "age": "40"},
{"id": "2", "name": "Jack", "age": "50"}
],
"orders": [
{ "id": "1", "item": "item 1", "userId": "1"},
{ "id": "2", "item": "item 3", "userId": "1"},
{ "id": "1", "item": "item 4", "userId": "1"},
{ "id": "1", "item": "item 51", "userId": "1"},
{ "id": "2", "item": "item 4", "userId": "1"}
]
}
as you can see I'm coming from the SQL world so I'm saving the 'orders' data with foreign key, is that the right way to save this data as json and if so how can I get all the users with there orders?
Thanks!
If your json object is in the above mentioned structure you can use this code to filter the orders
var oJson = {
"users": [
{"id": "1", "name": "Allan", "age": "40"},
{"id": "2", "name": "Jack", "age": "50"}
],
"orders": [
{ "id": "1", "item": "item 1", "userId": "1"},
{ "id": "2", "item": "item 3", "userId": "1"},
{ "id": "1", "item": "item 4", "userId": "1"},
{ "id": "1", "i`enter code here`tem": "item 51", "userId": "1"},
{ "id": "2", "item": "item 4", "userId": "1"}
]}
for(var i=0; i<oJson.users.length;i++){
for(var j=0; j<oJson.orders.length;j++){
if (oJson.users[i].id == oJson.orders[j].userId) {
console.log( oJson.orders[j].item, oJson.orders[j].userId)
//your logic here
}
}
}
But it is better to use sql joints to merge the orders and return the objects like this
{
"users": [
{"id": "1", "name": "Allan", "age": "40","orders": [{ "id": "1", "item": "item 1", "userId": "1"},{ "id": "2","item": "item 3", "userId": "1"},{ "id": "1", "item": "item 4", "userId": "1"},{ "id": "1", "item": "item 51", "userId": "1"},{ "id": "2", "item": "item 4", "userId": "1"}
]},
{"id": "2", "name": "Jack", "age": "50","orders":[]}
]}
Fetch the objects for json file into two varible
var a=value.users;
var b=value.orders;
value is nothing but the whole json file
then you can match them against each other like
a[0].id==b.[0].id
Related
I've a little problem, fetch query over react to Graphql work.
But when I ask a complex query, I receive this answer :
How I can read all variable but not the variable in the "operation":
Name
ID
How I can read this 2 Variables ?
[
{
"__typename": "Journal",
"id": "1",
"name": "Journal1",
"state": "ACTIVE",
"operation": {
"__typename": "Operation",
"id": "1",
"name": "Oper1OnlyName"
}
},
{
"__typename": "Journal",
"id": "2",
"name": "Default",
"state": "ACTIVE",
"operation": {
"__typename": "Operation",
"id": "15",
"name": "Oper15ID"
}
},
{
"__typename": "Journal",
"id": "3",
"name": "Nachrichten",
"state": "ACTIVE",
"operation": {
"__typename": "Operation",
"id": "15",
"name": "Oper15ID"
}
},
{
"__typename": "Journal",
"id": "4",
"name": "WEMA",
"state": "ACTIVE",
"operation": null
},
I could have sworn I've seen examples of this in multiple places, but can anyone tell me how I only return items from the JSON object below that match ALL of the tags in a search array - eg:
[
{
"ref": "1",
"title": "Title 1",
"description": "Description 1...",
"tags": ["A2", "B1", "D1", "E3", "E4"]
},
{
"ref": "2",
"brand": "Title 2",
"description": "Description 2...",
"tags": ["A1", "A2", "C1", "D1", "E1", "E2", "E3", "E4"]
},
{
"ref": "3",
"title": "Title 3",
"description": "Description 3...",
"tags": ["A5", "A6", "B1", "D1", "E3"]
},
{
"ref": "4",
"title": "Title 4",
"description": "Description 4...",
"tags": ["A5", "A6", "B1", "E1", "E3"]
}
]
And my search array to match/find within the tags array would be:
["B1", "D1"]
This would return the filtered JSON with only item ref 1 and 2 in this instance...
If you are using java script then this should work..
const array1 = [
{
"ref": "1",
"title": "Title 1",
"description": "Description 1...",
"tags": ["A2", "B1", "D1", "E3", "E4"]
},
{
"ref": "2",
"brand": "Title 2",
"description": "Description 2...",
"tags": ["A1", "A2", "C1", "D1", "E1", "E2", "E3", "E4"]
},
{
"ref": "3",
"title": "Title 3",
"description": "Description 3...",
"tags": ["A5", "A6", "B1", "D1", "E3"]
},
{
"ref": "4",
"title": "Title 4",
"description": "Description 4...",
"tags": ["A5", "A6", "B1", "E1", "E3"]
}
]
let tags = [ "B1", "D1"]
console.log(array1.filter( f => tags.every(e => f.tags.includes(e)))
);
I'm new on lucence, solr queries, I have doubts about how to make a query to nested documents.
I have nested document indexed, as below
[
{
"id": "1",
"title": "Solr1",
"_childDocuments_": [
{
"id": "2",
"title": "Solr2",
"_childDocuments_": [
{
"id": "3",
"title": "Solr3",
"_childDocuments_": [
{
"id": "4",
"title": "SolrCloud supports it"
}
],
"something_else":"irrelevant"
}
],
"something_else":"irrelevant"
}
],
"something_else":"irrelevant"
},
{
"id": "5",
"title": Solr5",
"_childDocuments_": [
{
"id": "6",
"title": "SolrCloud here as well"
}
]
}
]
How do I search title:SolrCloud, and listed all children's parents? Such as
[
{
"id": "1",
"title": "Solr1",
"_childDocuments_": [
{
"id": "2",
"title": "Solr2",
"_childDocuments_": [
{
"id": "3",
"title": "Solr3",
"_childDocuments_": [
{
"id": "4",
"title": "SolrCloud supports it"
}
]
}
]
}
]
},
{
"id": "5",
"title": Solr5",
"_childDocuments_": [
{
"id": "6",
"title": "SolrCloud here as well"
}
]
}
]
which listed all parents of document 4(Sorl1, Solr2, Solr3) and document 6(Solr5).
And the depth of documents is not constants.
My current solution is that massaging data, add trace into original data, so I will know document come from. such as
[
{
"id": "1",
"title": "Solr1",
"_childDocuments_": [
{
"id": "2",
"title": "Solr2",
**"parent_id":"1",**
**"trace":"Solr1",**
"_childDocuments_": [
{
"id": "3",
"title": "Solr3",
**"parent_id":"2",**
**"trace":"Solr1/Solr2",**
"_childDocuments_": [
{
"id": "4",
"title": "SolrCloud supports it"
**"parent_id":"3",**
**"trace":"Solr1/Solr2/Solr3",**
}
],
"something_else":"irrelevant"
}
],
"something_else":"irrelevant"
}
],
"something_else":"irrelevant"
},
{
"id": "5",
"title": Solr5",
"_childDocuments_": [
{
"id": "6",
**"parent_id":"5",**
**"trace":"Solr5",**
"title": "SolrCloud here as well"
}
]
}
]
So after indexed, I could know who's parent document from result.
Could someone agree on this?
Looking for better solution than this.
I am new to solr and trying to fetch results from it. I want results which exactly matches my phrase ex: "test test1 test2" and any document which has any of these keywords, so kind of union of exact match and partial matches.
I am pretty much achieving it using dismax. Now i want the results to be sorted/boosted on the basis of some field value eg: cat:A, then cat:B then cat:C also I want this sorting to be applied first on exactly matched result then partial matched results.
Indexed data is:
[
{
"id": "001",
"name": "test test1 test2 Z",
"price": 12.9,
"cat": "A"
},
{
"id": "002",
"name": "test test1 test2 X",
"price": 91.5,
"cat": "B"
},
{
"id": "003",
"name": "test test1 test2 R",
"price": 45.5,
"cat": "C"
},
{
"id": "004",
"name": "test test1 test2 C",
"price": 78.67,
"cat": "B"
},
{
"id": "005",
"name": "data",
"price": 91.5,
"cat": "A"
},
{
"id": "008",
"name": "test test1 test2 D",
"price": 45.09,
"cat": "A"
},
{
"id": "009",
"name": "test test1 test2 B",
"price": 34.09,
"cat": "B"
},
{
"id": "010",
"name": "test test1 test2 A",
"price": 39.19,
"cat": "C"
},
{
"id": "011",
"name": "test this data",
"price": 89.19,
"cat": "A"
},
{
"id": "012",
"name": "this is my data",
"price": 89.19,
"cat": "C"
},
{
"id": "013",
"name": "test1 is this data title",
"price": 89.19,
"cat": "A"
}
]
Please help.Thanks in advance.
I am sure I am missing some small detail. I have a factory which reads data from the server and retrieves JSON results. Then, in my controller I do the following:
.controller('homeController', ['$scope','RecentItems', function($scope,RecentItems)
{
//scopes for the items
$scope.items={};
RecentItems.getRecentItems().done(function(data){
$scope.items = data;
console.log($scope.items);
});
}])
In the HTML template i have an ng-repeat directive > ng-repeat="item in items"
And the results don't appear in the view. console.log($scope.items) displays the data in the console however, in the view the items appears to be empty.
I know that it has to do with the variable scopes in JavaScript rather than being an AngularJS problem
THIS IS THE JSON RESULT:
[
{
"iid": "32",
"name": "Womens bag 2",
"brand": "Gucci",
"category": "Handtaschen",
"minprice": "20",
"retailprice": "400",
"duedate": "2015-06-10 21:25:00",
"description": "second bag",
"img": "images/items/1433274066696.jpg",
"current_price": "0"
},
{
"iid": "31",
"name": "Womens Bag",
"brand": "Gucci",
"category": "Handtaschen",
"minprice": "20",
"retailprice": "300",
"duedate": "2015-06-10 21:25:00",
"description": "Bag",
"img": "images/items/1433274042147.jpg",
"current_price": "0"
},
{
"iid": "30",
"name": "Watch2",
"brand": "Swiss",
"category": "Uhren",
"minprice": "20",
"retailprice": "200",
"duedate": "2015-06-10 21:25:00",
"description": "watch to test",
"img": "images/items/1433273997525.png",
"current_price": "0"
},
{
"iid": "29",
"name": "Watch",
"brand": "Swiss",
"category": "Uhren",
"minprice": "10",
"retailprice": "200",
"duedate": "2015-06-10 21:25:00",
"description": "Test",
"img": "images/items/1433273879811.png",
"current_price": "0"
}
]