Using mongolite in R to extract individual array items - r

I'm using mongolite in R to read a mongo collection with the following structure:
[{_id: 0, date: 20221201, dailyAnswer:[
{question:a,score:1},
{question:b,score:3},
{question:c,score:2}
]},
{_id: 1, date: 20221201, dailyAnswer:[
{question:a,score:3},
{question:b,score:2},
{question:c,score:1}
]},
{_id: 0, date: 20221202, dailyAnswer:[
{question:a,score:2},
{question:b,score:2},
{question:c,score:3}
]},
{_id: 1, date: 20221202, dailyAnswer:[
{question:a,score:3},
{question:b,score:1},
{question:c,score:1}
]}]
For each document I'd like to extract each question score into a column, with the table structure:
_id | date | question_a_score | question_b_score | question_c_score
In MongoDB Compass I've written a query to extract them:
{
q_a_score: { $arrayElemAt: [ "$dailyAnswer.score",0]},
q_b_score: { $arrayElemAt: [ "$dailyAnswer.score",1]},
q_c_score: { $arrayElemAt: [ "$dailyAnswer.score",2]}
}
Which returns:
[{
_id: 0,
question_a_score:1,
question_b_score:3,
question_c_score:2},
...,
{
_id: 1,
question_a_score:3,
question_b_score:1,
question_c_score:1}
}]
However, I'm not sure whether to use the $aggregate or $find methods in mongolite in R, and how to structure the pipeline or query arguments in those methods respectively.

Use the aggregate method with the $project and $arrayElemAt operators:
checkins_questions <- collection_connection$aggregate(pipeline = '[{"$project": {"dailyAnswerScore1": { "$arrayElemAt": [ "$dailyAnswer.score", 0 ] },
"dailyAnswerScore2": { "$arrayElemAt": [ "$dailyAnswer.score", 1 ] },
"dailyAnswerScore3": { "$arrayElemAt": [ "$dailyAnswer.score", 2 ] }}}]')

Related

How can I spread array elements in parent object by jq?

I want to transform following type of json obj
[
{
"A": "a",
"Tags": [
{ "key":"x", "value":0},
{ "key":"y", "value":1},
]
},
{...}
]
to this, including Tags list on top
[
{
"A": "a",
"x": 0,
"y": 1
},
{...}
]
I try to use JQ but without result.
map(. + (.Tags | from_entries) | del(.Tags))
Will map() over all the objects in the array and:
Convert .Tags to an object using from_entries
This is added to the original object (. + ())
Delete the original .Tags
Output:
[
{
"A": "a",
"x": 0,
"y": 1
}
]
Online Demo

Using jq to conditionally flatten recursive json

The simplest version of the input document I could come up with is
{
"references": [
{
"version": 5,
"id": "id1",
"objType": "A"
},
{
"version": 4,
"id": "id2",
"objType": "B",
"referencing": []
},
{
"version": 4,
"id": "id3",
"objType": "B",
"referencing": [
{
"version": 2,
"id": "id4",
"objType": "A"
},
{
"version": 3,
"id": "id5",
"objType": "B",
"referencing": []
}
]
}
]
}
Objects of type A have no referencing objects.
Objects of type B can be referenced by either type of object.
There are two outputs I need from this json:
Output #1 is the version info for objects of type A with the id value as a key with the value of version. A objects can be at the top level or at some arbitrary depth in the referencing arrays.
{
"references": {
"id1": {"version": 5},
"id4": {"version": 2}
}
}
The 2nd output is similar: the version info for objects of type B. The can be a chain of type B objects referencing other type B objects.
{
"references": {
"id2": {"version": 4},
"id3": {"version": 4},
"id5": {"version": 3}
}
}
Use recursive decsent operator and from_entries. You don't need to follow the "references" (at least not to produce the expected output in your question)
{
dependencies: [.. | select(.objType=="A")? | { key: .id, value: {version} }] | from_entries
},
{
dependencies: [.. | select(.objType=="B")? | { key: .id, value: {version} }] | from_entries
}
Output:
{
"dependencies": {
"id1": {
"version": 5
},
"id4": {
"version": 2
}
}
}
{
"dependencies": {
"id2": {
"version": 4
},
"id3": {
"version": 4
},
"id5": {
"version": 3
}
}
}
It's also possible to merge (add) objects instead of constructing them from their entries, which makes the code minimally shorter:
{
dependencies: [.. | select(.objType=="A")? | { (.id): {version} }] | add
}
You can use recurse to traverse the document, INDEX to create an object with IDs as keys, map_values to format their values using select to reduce according to your criteria.
jq --arg type A '
.references |= (
INDEX(.[] | recurse(.referencing[]?); .id)
| map_values(select(.objType == $type) | {version})
)
'
{
"references": {
"id1": {
"version": 5
},
"id4": {
"version": 2
}
}
}
Demo
This works for both questions, provide A or B to --arg type.
Note that this is using the error suppression operator ? when recursing down. If you want to restrict the traversal explicitly to .objType == "B", just prepend it in a select expression, i.e. replace recurse(.referencing[]?) with recurse(select(.objType == "B") | .referencing[]). Demo

Is there a way to transform these 2 arrays by using jq, into a set of objects, like in the example down below?

Example json data:
{
"data": [
{
"place": "FM346",
"id": [
"7_day_A",
"7_day_B",
"7_day_C",
"7_day_D"
],
"values": [
0,
30,
23,
43
]
},
{
"place": "LH210",
"id": [
"1_day_A",
"1_day_B",
"1_day_C",
"1_day_D"
],
"values": [
4,
45,
100,
9
]
}
]
}
what i need to transform it into:
{
"data": [
{
"place": "FM346",
"7_day_A": {
"value": 0
},
"7_day_B": {
"value": 30
},
"7_day_C": {
"value": 23
},
"7_day_D": {
"value": 43
}
},
{
"place": "LH210",
"1_day_A": {
"value": 4
},
"1_day_B": {
"value": 45
},
"1_day_C": {
"value": 100
},
"1_day_D": {
"value": 9
}
}
]
}
i have tried this:
{
data:[.data |.[]|
{
place: (.place),
(.id[]):
{
value: (.values[])
}
}]
}
(in jqplay: https://jqplay.org/s/f4BBtN9gwmp)
and this:
{
data:[.data |.[]|
{
place: (.place),
test:
[{
(.id[]):
{
value: (.values[])
}
}]
}]
}
(in jqplay: https://jqplay.org/s/pKIvQe1CzgX)
but they arent grouped in the way i wanted and it gives each value to each id, not the corresponding one.
I have been trying for some time now, but im new to jq and have no idea how to transform it this way, thanks in advance for any answers.
You can use transpose here, which can play a key role in converting the arrays to key/value pairs
.data[] |= {place} +
([ .id, .values ] | transpose | map({(.[0]): { value: .[1] } }) | add)
The solution works by converting the array-of-arrays [.id, .values] by transposing them, i.e. converting
[["7_day_A","7_day_B","7_day_C","7_day_D"],[0,30,23,43]]
[["1_day_A","1_day_B","1_day_C","1_day_D"],[4,45,100,9]]
to
[["7_day_A",0],["7_day_B",30],["7_day_C",23],["7_day_D",43]]
[["1_day_A",4],["1_day_B",45],["1_day_C",100],["1_day_D",9]]
With the transformation done, we construct an object with key as the zeroth index element and value as an object comprising of the value of first index element, and combine the results together with add
Demo - jqplay

Problem setting edges in vis-network with typescript

I'm following the Dynamic Dataset example from vis and I'm getting a typescript error when I try to set edges.
I just have an edge array that looks like:
edgesArray = [
{ from: 1, to: 3 },
{ from: 1, to: 2 },
{ from: 2, to: 4 },
{ from: 3, to: 5 },
]
and I'm setting the data as
let data = {
nodes: new vis.DataSet(nodesArray),
edges: new vis.DataSet(edgesArray)
}
The error I'm getting is in the edges.
No overload matches this call.
Overload 1 of 2, '(options?: DataSetInitialOptions<"id">): DataSet<Partial<Record<"id", string | number>>, "id">', gave the following error.
Type '{ from: number; to: number; } []' has no properties in common with type 'DataSetInitialOptions<"id">'.
Overload 2 of 2, '(data: Partial<Record<"id", string | number>>[], options?: DataSetInitialOptions<"id">): DataSet<Partial<Record<"id", string | number>>, "id">', gave the following error.
Argument of type '{ from: number; to: number; } []' is not assignable to parameter of type 'Partial<Record<"id", string | number>>[]'.
I'm using version 5.2.4 of vis-network.
This is standard TS behavior. You're trying to assign type A into B but they have nothing in common. DataSet can accept items that have optional id. However your edges have no ids and TS simply says you're doing something wrong.
Solution one:
import { Edge } from "vis-network";
const edgesArray: Edge[] = [
{ from: 1, to: 3 },
{ from: 1, to: 2 },
{ from: 2, to: 4 },
{ from: 3, to: 5 },
];
Solution two:
const edgesArray = [
{ id: undefined, from: 1, to: 3 },
{ from: 1, to: 2 },
{ from: 2, to: 4 },
{ from: 3, to: 5 },
];
Solution three:
const edgesArray: { id?: undefined, from: number, to: number }[] = [
{ from: 1, to: 3 },
{ from: 1, to: 2 },
{ from: 2, to: 4 },
{ from: 3, to: 5 },
];

Mapbox Distance Matrix API returns all zeroes

I'm trying to implement the Mapbox Distance Matrix API as an alternative to Google, since it was becoming too expensive. I've tried to reduce the example to something minimal, with only two values:
{
code: "Ok",
distances: [
[
0,
0
]
],
durations: [
[
0,
0,
0,
0,
0,
0
]
],
destinations: [
{
distance: 404951.186070298,
name: "",
location: [
48.761423,
5.731594
]
},
{
distance: 402983.402982556,
name: "",
location: [
48.761423,
5.731594
]
}
],
sources: [
{
distance: 401905.604376238,
name: "",
location: [
48.761423,
5.731594
]
}
]
}
I see that the coordinates of the values are the same, even though they do not match the input coordinates from my URL, which are 52.08515,4.2826;52.11703,4.28716;52.11736,4.28939. The problem persists with all modes of transportation. Any help would be appreciated!
The format is lon,lat - not lat,lon - I made the same mistake but the docs are correct.

Resources