I am trying to query the following data:
(obtained from:https://dotnetcodr.com/2017/06/21/introduction-to-couchdb-with-net-part-17-starting-with-mango-queries/)
{
"post_code": 35801,
"country": "United States",
"country_abbreviation": "US",
"places": [
{
"place_name": "Huntsville",
"longitude": -86.5673,
"state": "Alabama",
"state_abbreviation": "AL",
"latitude": 34.7269
}
]
},
..........
My question:
How to include, say, firstly, the place_name, in the query.
The following javascript does not give a result:
{
"selector": {
"post_code": {"$eq": 35801}
},
"fields":["places.placename"]
}
correct index on post_code:
{
"index": {
"fields": [
"post_code"
]
},
"type": "json",
"name": "post-code-index"
}
I did set up an additional index as follows:
{
"index": {
"fields": [
"places.placename"
]
},
"type": "json"
}
What if the data had the following field:
"old_post_code_numbers": [12345, 67890, ......]
or:
What if the data had the following fields:
"places":
{
"place_name": "Huntsville",
"longitude": -86.5673,
"state": "Alabama",
"state_abbreviation": "AL",
"latitude": 34.7269
}
JSON has so many forms and I struggle to get a handle on the principles behind the javascript required for these kind of queries.
I know it is simple. It must be simple.
Many thanks for any guidance or websites I should/could visit.
Edit:
I managed to get the following to work on an online javascript testing site, but I still could not get this to work in CouchDB Mango. Surely this is possible in CouchDB.???
var xxx=
{
"post_code": 82941,
"country": "United States",
"country_abbreviation": "US",
"places": [
{
"place_name": "Pinedale",
"longitude": -109.8561,
"state": "Wyoming",
"state_abbreviation": "WY",
"latitude": 42.8543
}]
}
console.log(xxx.places[0].place_name);
I have been able to get a result for part 1. of my question (based on the answer to this question), as follows:
My index:
{
"type": "json",
"def": {
"fields": [
{
"places.0.place_name": "asc"
}
]
}
}
My query:
{
"selector": {
"places.0.place_name": {
"$gte": null
}
},
"fields": [
"_id",
"places.0.place_name"
],
"sort": [
{
"places.0.place_name": "asc"
}
]
}
Result:
{"docs":[
{"_id":"254b9a8c7a46934363076cc3d9034082","places":{"0":{"place_name":"Aberdeen"}}},
{"_id":"254b9a8c7a46934363076cc3d9037559","places":{"0":{"place_name":"Altavista"}}},
{"_id":"254b9a8c7a46934363076cc3d900e4d2","places":{"0":{"place_name":"Anchorage"}}},
{"_id":"254b9a8c7a46934363076cc3d900f3b9","places":{"0":{"place_name":"Anchorage"}}},
{"_id":"254b9a8c7a46934363076cc3d902c738","places":{"0":{"place_name":"Ashland"}}},
{"_id":"254b9a8c7a46934363076cc3d901a2f2","places":{"0":{"place_name":"Atlanta"}}},
{"_id":"254b9a8c7a46934363076cc3d901a374","places":{"0":{"place_name":"Atlanta"}}}
.................
In this case I have omitted the "post_code": {"$eq": 35801} but this can be added without an error occurring.
part3:
Index:
{
"index": {
"fields": [
{
"places.place_name": "asc"
}
]
},
"type": "json"
}
Query:
{
"selector": {
"places.place_name": {
"$gte": null
}
},
"fields": [
"places.place_name"
],
"sort": [
{
"places.place_name": "asc"
}
]
}
Result:
{"docs":[
{"places":{"place_name":"Anchorage"}},
{"places":{"place_name":"Anchorage"}},
{"places":{"place_name":"Huntsville"}},
{"places":{"place_name":"Huntsville"}},
{"places":{"place_name":"Phoenix"}},
{"places":{"place_name":"Phoenix"}}
]}
Part 2:
Index:
{
"index": {
"fields": [
{
"old_post_code_numbers": "asc"
}
]
},
"type": "json"
}
Query:
{
"selector": {
"old_post_code_numbers": {
"$gte": null
}
},
"fields": [
"places.place_name",
"old_post_code_numbers"
],
"sort": [
{
"old_post_code_numbers": "asc"
}
]
}
Result:
{"docs":[
{"places":{"place_name":"Huntsville"},"old_post_code_numbers":[12345,67890]},
{"places":{"place_name":"Huntsville"},"old_post_code_numbers":[12345,67890]},
{"places":{"place_name":"Anchorage"},"old_post_code_numbers":[12345,67890]},
{"places":{"place_name":"Anchorage"},"old_post_code_numbers":[12345,67890]},
{"places":{"place_name":"Phoenix"},"old_post_code_numbers":[12345,67890]},
{"places":{"place_name":"Phoenix"},"old_post_code_numbers":[12345,67890]}
]}
The last example using: "old_post_code_numbers.0" throughout, gives:
{"docs":[
{"places":{"place_name":"Huntsville"},"old_post_code_numbers":{"0":12345}},
{"places":{"place_name":"Huntsville"},"old_post_code_numbers":{"0":12345}},
{"places":{"place_name":"Anchorage"},"old_post_code_numbers":{"0":12345}},
{"places":{"place_name":"Anchorage"},"old_post_code_numbers":{"0":12345}},
{"places":{"place_name":"Phoenix"},"old_post_code_numbers":{"0":12345}},
{"places":{"place_name":"Phoenix"},"old_post_code_numbers":{"0":12345}}
]}
I would appreciate any comments.
Related
I am tring to make a query where use the value and try to interpolate a string in a new field.
Mongo Database:
[
{
"state": "1",
"events": {
"1": [
{
"date": 123.2,
"msg": "msg1"
},
{
"date": 124.2,
"msg": "msg2"
}
],
"2": [
{
"date": 125.2,
"msg": "msg3"
},
{
"date": 126.2,
"msg": "msg4"
}
],
}
},
{
"state": "2",
"events": {
"1": [
{
"date": 123.2,
"msg": "msg1"
},
{
"date": 124.2,
"msg": "msg2"
}
],
"2": [
{
"date": 125.2,
"msg": "msg3"
},
{
"date": 126.2,
"msg": "msg4"
}
],
}
}
]
Aggregate query:
db.collection.aggregate({
"$match": {
"state": {
"$in": [
"1",
"2"
]
}
}
},
{
"$group": {
"_id": {
"state": "$state"
},
"this_path": {
"$first": {
"$concat": [
"events.",
"$state",
".0.date"
]
}
}
}
})
"this_path" gets "events.1.0.date", but how to use this value, in another query(line), I would like to do like a string interpolation. Some thing like
...
"date": {
"$first": { `\$${this_path}`}
...
so it become the "events.1.date" then "$events.1.0.date" then "123.2"
you can define it by let just for example a fragment from pipeline:
$lookup: {
from: contentCollectionName,
as: 'content',
let: {
parentId: '$id',
},
The id is taken from above matched documents, but it can be anything
I am looking to sort a GeoJSON file on a property so I can then slice it keeping only the top 5 features. So for example I wish to take this GeoJSON and sort the file in descending order by the incidents property:
...
[
-75.1972382872565,
39.9294288475177
]
]
]
]
},
"properties": {
"area": "",
"location": "24th St. & Wolf St.",
"perimeter": "81903.64182498",
"dist_numc": "01",
"sum_area": "",
"area_sqmi": "216350124.153",
"district_i": "",
"objectid": "321",
"globalid": "c040618c-ac93-4b22-b174-0ea08e0f805d",
"district_": "1",
"_feature_id": "1",
"_feature_id_string": "Police_Districts.1",
"dist_num": "1",
"div_code": "SPD",
"phone": "686-3010",
"incidents": 11553
}
},
...
I'm looking to basically rearrange the order of the features array so that it's in descending order and the highest incident features are first.
How would I go about this? I am used to creating a compare function and then sorting an array with something like this:
//FUNCTION: SORT
export const compare = ( a, b ) => {
if ( a.incidents < b.incidents ){
return -1;
}
if ( a.incidents > b.incidents ){
return 1;
}
return 0;
}
But in this case I need to sort a JSON and only sort an array inside of it so I am unsure how to proceed.
I set up an example where i attempt to sort via a button click but doesn't seem to be working:
https://codesandbox.io/s/slicing-geojson-qg1hb?file=/src/App.js
Consider this file as JSON. To do that, I've copied the first three data from that geojson file and store into a variable called data. So we need to sort that array according to the incidents property. Now execute javascript's sort() function to data.features. Since your data is big and you're sorting the data according to the incidents property, make sure to print only incidents property to see whether your data is being sorted or not. As StackOverflow asks limited characters(30000) in this section, I'm going to keep only the first three coordinates from every geometry property. Have a look at the snippet below:
var data = {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "MultiPolygon",
"coordinates": [
[
[
[
-75.0544420852857,
40.044532749252
],
[
-75.054816199384,
40.0441717116827
],
[
-75.0551367855513,
40.0438732632911
],
]
]
]
},
"properties": {
"area": "",
"location": "Harbison Ave. & Levick St.",
"perimeter": "63587.3693993",
"dist_numc": "02",
"sum_area": "",
"area_sqmi": "192346097.032",
"district_i": "",
"objectid": "322",
"globalid": "95200c4a-2617-47c2-93f1-07d6359a92e9",
"district_": "2",
"_feature_id": "2",
"_feature_id_string": "Police_Districts.2",
"dist_num": "2",
"div_code": "NEPD",
"phone": "686-3020",
"incidents": 32340
}
}
,
{
"type": "Feature",
"geometry": {
"type": "MultiPolygon",
"coordinates": [
[
[
[
-75.1972382872565,
39.9294288475177
],
[
-75.1969248893574,
39.9291749499585
],
[
-75.1966534158686,
39.9293232425744
],
]
]
]
},
"properties": {
"area": "",
"location": "24th St. & Wolf St.",
"perimeter": "81903.64182498",
"dist_numc": "01",
"sum_area": "",
"area_sqmi": "216350124.153",
"district_i": "",
"objectid": "321",
"globalid": "c040618c-ac93-4b22-b174-0ea08e0f805d",
"district_": "1",
"_feature_id": "1",
"_feature_id_string": "Police_Districts.1",
"dist_num": "1",
"div_code": "SPD",
"phone": "686-3010",
"incidents": 11553
}
},
{
"type": "Feature",
"geometry": {
"type": "MultiPolygon",
"coordinates": [
[
[
[
-75.1343640909741,
39.9529315410666
],
[
-75.1352375679666,
39.9506957263754
],
[
-75.1352409996735,
39.9506869425429
]
]
]
]
},
"properties": {
"area": "",
"location": "11th St. & Winter St.",
"perimeter": "34655.32085552",
"dist_numc": "06",
"sum_area": "",
"area_sqmi": "69279267.8986235",
"district_i": "",
"objectid": "325",
"globalid": "e0afa680-ecd3-470e-b0f8-59b70d4f72b5",
"district_": "6",
"_feature_id": "5",
"_feature_id_string": "Police_Districts.5",
"dist_num": "6",
"div_code": "CPD",
"phone": "686-3060",
"incidents": 23428
}
}
]
};
console.log("Before sorting");
console.log(data.features[0].properties.incidents);
console.log(data.features[1].properties.incidents);
console.log(data.features[2].properties.incidents);
data.features.sort(function (a, b) {
if (a.properties.incidents < b.properties.incidents)
return -1;
else if (a.properties.incidents > b.properties.incidents)
return 1;
});
console.log("After sorting");
console.log(data.features[0].properties.incidents);
console.log(data.features[1].properties.incidents);
console.log(data.features[2].properties.incidents);
I'm querying data, where the structure is something like this.
I have two models, FoodDrinks and Cuisines. They have many to many relation.
Now on the following query
{
"filter": {
"include": "cuisines"
}
}
I am getting the following results.
[
{
"id": 1,
"name": "Biryani",
"cuisines": [
{
"id": 1,
"name": "Mughlai"
},
{
"id": 2,
"name": "North Indian"
},
{
"id": 3,
"name": "Afghani"
}
]
},
{
"id": 2,
"name": "Chhole Bhature",
"cuisines": [
{
"id": 2,
"name": "North Indian"
}
]
},
{
"id": 3,
"name": "Amritsari Naan",
"cuisines": [
{
"id": 2,
"name": "North Indian"
},
{
"id": 6,
"name": "Punjabi"
},
]
}
]
Now I want only those food drinks which have cuisines from the following array.
let cuisinesIDs = ["1", "2"]
What will the following query for that?
I guess something like the below should work:
{
"filter": {
"include": "cuisines"
},
"where": {
"cuisines.id": {
"inq": cuisinesIDs
}
}
}
It might be the same answer as #Behrooz, but I would try:
{
"filter": {
"include": {
relation: 'cuisines',
scope: {
where: {
id: {inq: cuisinesIDs}
}
}
}
}
}
This might be a duplicate of this but did not get proper solution over there. I have object as below,
var replyDataObj = {
"department": {
"name": getCache("departmentName")
},
"subject": replyEmailSubject,
"payload": {
"email": {
"contents": {
"content": [
{
"type": "html",
"value": replyEmailContent
}
]
},
"emailAddresses": {
"from": fromEmailId,
"to": {
"address": [
toEmailId
]
}
}
}
}
}
I want to add following key values to 'emailAddresses' key dynamically depending upon whether cc field is present or not,
"cc": {
"address": [
ccEmailId
]
}
So it will look like,
var replyDataObj = {
"department": {
"name": getCache("departmentName")
},
"subject": replyEmailSubject,
"payload": {
"email": {
"contents": {
"content": [
{
"type": "html",
"value": replyEmailContent
}
]
},
"emailAddresses": {
"from": fromEmailId,
"to": {
"address": [
toEmailId
],
"cc": {
"address": [
ccEmailId
]
}
}
}
}
}
}
I tried to add this using object[key] as below, object.key but no luck
replyDataObj[payload][emailAddresses][cc]={
"address": [
ccEmailId
]
}
I tried multiple ways and searched a lot but did not get the solution. Any help in this regard will be greatly appreciated. Thank you.
Put strings inside []:
replyDataObj['payload']['emailAddresses']['cc']={
"address": [
ccEmailId
]
}
As answered by #K.Kirsz, I was missing strings inside [] (quotes). Also added missing 'email' key to solve my issue.
replyDataObj['payload']['email']['emailAddresses']['cc']={
"address": [
ccEmailId
]
}
I am trying to scrape output from a JavaScript command using VBA.
Unfortunately I am a bit lost with how. I have tried .getElementsByTagName("script")(4).innertext, but all I get back is [object]
See the HTML below. I am trying to extract on the data array's. Any help will be appreciated
Cheers
<script type="text/javascript">
var _chartit_hco_array = [
{
"series": [
{
"stacking": false,
"data": [
4006,
34940.1034,
61062.5161,
95107.6333,
167971.8,
218549.129,
272389.2143,
288584.7097,
317959.5
],
"type": "column",
"name": "Dau"
}
],
"yAxis": [
{
"title": {
"text": "Daily Active Users (DAU)"
}
}
],
"chart": {
"renderTo": "dauchart_container"
},
"xAxis": [
{
"categories": [
"May<br>2013",
"Jun<br>2013",
"Jul<br>2013",
"Aug<br>2013",
"Sep<br>2013",
"Oct<br>2013",
"Nov<br>2013",
"Dec<br>2013",
"Jan<br>2014"
],
"title": {
"text": "Date"
}
}
],
"title": {
"text": "Daily Active Users (Monthly avg.)"
}
},
{
"series": [
{
"stacking": false,
"data": [
12456.96,
204106.3103,
320728.5161,
442251.9333,
646994.0333,
1017031.3226,
1232705.2857,
1377097.3871,
1432762.5
],
"type": "column",
"name": "Mau",
"label": {
"text": "Light breeze",
"style": {
"color": "#606060"
}
}
}
],
"yAxis": [
{
"title": {
"text": "Monthly Active Users (MAU)"
}
}
],
"chart": {
"renderTo": "mauchart_container"
},
"xAxis": [
{
"categories": [
"May<br>2013",
"Jun<br>2013",
"Jul<br>2013",
"Aug<br>2013",
"Sep<br>2013",
"Oct<br>2013",
"Nov<br>2013",
"Dec<br>2013",
"Jan<br>2014"
],
"title": {
"text": "Date"
}
}
],
"title": {
"text": "Monthly Active Users (Monthly avg.)"
}
},
{
"series": [
{
"stacking": false,
"data": [
36.904,
18.963103,
18.939677,
21.466333,
25.936,
21.515806,
22.113929,
20.959032,
22.19
],
"type": "column",
"name": "Dau2Mau Percent"
}
],
"yAxis": [
{
"title": {
"text": "Daily / Monthly Active Users (DAU/MAU)"
}
}
],
"chart": {
"renderTo": "dau2mauchart_container"
},
"xAxis": [
{
"categories": [
"May<br>2013",
"Jun<br>2013",
"Jul<br>2013",
"Aug<br>2013",
"Sep<br>2013",
"Oct<br>2013",
"Nov<br>2013",
"Dec<br>2013",
"Jan<br>2014"
],
"title": {
"text": "Date"
}
}
],
"title": {
"text": "DAU as percentage of MAU (Monthly avg.)"
}
},
{
"series": [
{
"stacking": false,
"data": [
11057.24,
104310.5862,
153653.3226,
222996.8667,
392113.7,
546472.0645,
674174.0714,
710372.6774,
771079.5
],
"type": "column",
"name": "Wau"
}
],
"yAxis": [
{
"title": {
"text": "Weekly Active Users (WAU)"
}
}
],
"chart": {
"renderTo": "wauchart_container"
},
"xAxis": [
{
"categories": [
"May<br>2013",
"Jun<br>2013",
"Jul<br>2013",
"Aug<br>2013",
"Sep<br>2013",
"Oct<br>2013",
"Nov<br>2013",
"Dec<br>2013",
"Jan<br>2014"
],
"title": {
"text": "Date"
}
}
],
"title": {
"text": "Weekly Active Users (Monthly avg.)"
}
}
];</script>
You were almost right, you have to do:
document.getElementsByTagName("script")(4).innerText; //Check the uppercase T in innerText
(If it returns empty string, it means the script is from other domain, if that's the case you cannot get it).
Cheers