MongoDB : voters table need to merge with candidates table - arrays

candidates details table :
{
"_id": {
"$oid": "63d5fe37f15fc0e2c23c833b"
},
"Constituency": 236,
"seat": "Sylhet-6",
"year": 2018,
"district": "Sylhet",
"candidates": [
{
"id": "y2018s236cola",
"name": "anwarul islam chowdhury",
"name_bangla": "আনোয়ারুল ইসলাম চৌধুরী ",
"party": "BNP",
"symbole": "paddy",
"vote": [
{
}
]
},
{
"id": "y2018s236colb",
"name": "zahirul islam chowdhury",
"name_bangla": "জহিরুল ইসলাম চৌধুরী ",
"party": "JP",
"symbole": "plow",
"vote": [
{
}
]
},
{
"id": "y2018s236colc",
"name": "monwarul islam chowdhury",
"name_bangla": "মনোয়ারুল ইসলাম চৌধুরী ",
"party": "AL",
"symbole": "boat",
"vote": [
{
}
]
}
]
}
voters table
Table name : vote
// vote data copied from excel sheet
-------------------------
centers y2018s236cola y2018s236colb y2018s236colc
center1 340 408 766
center2 345 87 99
center3 56 364 76
center4 340 456 89
center5 74 87 90
center6 345 23 68
--------------------------
// after merge together table will be like this//
{
"Constituency": 236,
"seat": "Sylhet-6",
"year": 2018,
"district": "Sylhet",
"candidates": [
{
"id": "y2018s236cola",
"name": "anwarul islam chowdhury",
"name_bangla": "আনোয়ারুল ইসলাম চৌধুরী ",
"party": "BNP",
"symbole": "paddy",
"vote": [
{
"center1": 241,
"center2": 256,
"center3": 134,
"center4": 610
}
]
},
{
"id": "y2018s236colb",
"name": "zahirul islam chowdhury",
"name_bangla": "জহিরুল ইসলাম চৌধুরী ",
"party": "JP",
"symbole": "plow",
"vote": [
{
"center1": 445,
"center2": 36,
"center3": 350,
"center4": 710
}
]
},
{
"id": "y2018s236colc",
"name": "monwarul islam chowdhury",
"name_bangla": "মনোয়ারুল ইসলাম চৌধুরী ",
"party": "AL",
"symbole": "boat",
"vote": [
{
"center1": 441,
"center2": 126,
"center3": 344,
"center4": 460
}
]
}
]
}
// 2 MongoDB tables need to merge

Related

Node js object in specific pattern from array of object, i am geting only objects from second query i have to merge two json data

I'm facing some issue , I am working in typescript api I am trying to fetch two mysql query data array of object. if i fetch two query i am getting bellow output.
This logic I used for merge two mysql query
1."values" from this name i am geting first query data.
2."getEmployerDetail" from this name i am geting second query data.
if i use above logic i am geting below output.I am getting also same output again.
[
{
issuer_id: 2639,
job_title: 'Sales Manager',
'hr-contact': '9767865459',
adress: 'bangalore',
image: 'http://localhost:3003/public/uploads/company-logos/undefined',
job_type: 'top',
company_name: 'Limendo'
},
{
issuer_id: 2639,
job_title: 'Sales Manager',
'hr-contact': '9767865459',
adress: 'bangalore',
image: 'http://localhost:3003/public/uploads/company-logos/undefined',
job_type: 'top',
company_name: 'Limendo',
getEmployerDetail: [
[Object], [Object],
[Object], [Object],
[Object], [Object],
[Object]
]
}
]
This output should be come inside of "getEmployerDetail"
{
"issuer_id": 2639,
"Field_of_activity": "Jobs",
"id": 220,
"Section": "Benefits",
"Content": "Childcare",
"foa_section_content_id": 220
},
{
"issuer_id": 2639,
"Field_of_activity": "Jobs",
"id": 215,
"Section": "Benefits",
"Content": "Enrolment programme",
"foa_section_content_id": 215
},
{
"issuer_id": 2639,
"Field_of_activity": "Jobs",
"id": 162,
"Section": "Branch",
"Content": "Banks, Finance, Insurance",
"foa_section_content_id": 162
},
{
"issuer_id": 2639,
"Field_of_activity": "Jobs",
"id": 150,
"Section": "Professional field",
"Content": "Marketing, Graphics, PR",
"foa_section_content_id": 150
},
{
"issuer_id": 2639,
"Field_of_activity": "Jobs",
"id": 113,
"Section": "Communes",
"Content": "Schnals",
"foa_section_content_id": 113
},
{
"issuer_id": 2639,
"Field_of_activity": "Jobs",
"id": 112,
"Section": "Communes",
"Content": "Wolkenstein in Gröden",
"foa_section_content_id": 112
},
{
"issuer_id": 2639,
"Field_of_activity": "Jobs",
"id": 111,
"Section": "Communes",
"Content": "Mühlwald",
"foa_section_content_id": 111
}
My exact output is i have attache below :
[
{
"issuer_id": 2639,
"job_title": "Sales Manager",
"hr-contact": "9767865459",
"adress": "bangalore",
"image": "http://localhost:3003/public/uploads/company-logos/undefined",
"Communes": [
{
"Communes": "Mühlwald",
"id": 111
},
{
"Communes": "Wolkenstein in Gröden",
"id": 112
},
{
"Communes": "Schnals",
"id": 113
}
],
"Professional field": [
{
"Professional field": "Marketing, Graphics, PR",
"id": 150
}
],
"Branch": [
{
"Branch": "Banks, Finance, Insurance",
"id": 162
}
],
"Benefits": [
{
"Benefits": "Enrolment programme",
"id": 215
},
{
"Benefits": "Childcare",
"id": 220
}
]
}
]

I'm new to this, What am I doing wrong? Creating array of line items in JSON and getting parse error. - Expecting 'STRING', got '{'

enter image description here
Parse error on line 15:
...,
"Qty": 3
},
{
"Amount":
--------------------^
Expecting 'STRING', got '{'
I don't know how to format the code for stack overflow but this is the JSON array I'm trying to put together
{
"MetaData": {
"CreateTime": "2019-05-16T18:13:13-08:00",
"LastUpdatedTime": "2019-05-16T18:13:45-08:00"
},
"Line": [{
"Amount": 135,
"DetailType": "SalesItemLineDetail",
"SalesItemLineDetail": {
"ItemRef": {
"value": "1",
"name": "LV"
},
"Qty": 3
},
{
"Amount": 135,
"DetailType": "SalesItemLineDetail",
"SalesItemLineDetail": {
"ItemRef": {
"value": "1",
"name": "LV"
},
"Qty": 3
} }
],
"CustomerRef": {
"value": "20"
}
}
The solution is probably simple
{
"MetaData": {
"CreateTime": "2019-05-16T18:13:13-08:00",
"LastUpdatedTime": "2019-05-16T18:13:45-08:00"
},
"Line": [
{
"Amount": 135,
"DetailType": "SalesItemLineDetail",
"SalesItemLineDetail": {
"ItemRef": {
"value": "1",
"name": "LV"
},
"Qty": 3
} <--- MISSING BRACE HERE
},
{
"Amount": 135,
"DetailType": "SalesItemLineDetail",
"SalesItemLineDetail": {
"ItemRef": {
"value": "1",
"name": "LV"
},
"Qty": 3
}
}
],
"CustomerRef": {
"value": "20"
}
}

MOngoDB find maximum

Good Morning:
I have the following MongoDB structure:
{
"_id": 19,
"name": "Fredia Donan",
"faculty": "Zoomzone",
"lectures": [
{
"lecturerID": 25,
"name": "Sigismondo Brecknell",
"email": "sbrecknello#pinterest.com",
"imparts": [
{
"_id": 76,
"codCourse": 23,
"language": "Malay",
"max_students": 59,
"students": [
{
"studentID": 25
}
],
"classes": [
{
"date": ISODate("2022-02-02T09:23:32.59"),
"cod_classroom": 76
}
]
}
]
}
]
}
Having that I want to find the lecturer that imparts class to the maximum number of students. So I have to count the number of students that a lecturer teaches to and the find the max from across all lecturers from the department. How could I do it? Can it be done within the same code of the aggregation bellow? It is way over my MongoDB knowledge and I would be forever grateful if anyone could help.
I have read about $size and $count but everywhere I try it gives me different errors.
Thank you very much in advance!
The output above is done by an aggregation of two collections department and group with the following code:
[{"$unwind": "$lecturers"}, {"$unwind": "$lecturers.imparts"},
{"$lookup":
{"from": "group",
"localField": "lecturers.imparts.groupID",
"foreignField": "_id",
"as": "lecturers.imparts"}},
{"$group":
{"_id": {"_id": "$_id", "lecturersID": "$lecturers.lecturerID"},
"name": {"$first": "$name"},
"faculty": {"$first": "$faculty"},
"lecturers":
{"$first":
{"lecturerID": "$lecturers.lecturerID",
"name": "$lecturers.name",
"email": "$lecturers.email"}},
"imparts": {"$push": "$lecturers.imparts"}}},
{"$set":
{"lecturers":
{"$mergeObjects":
["$lecturers",
{"imparts":
{"$reduce":
{"input": "$imparts",
"initialValue": [],
"in": {"$concatArrays": ["$$value", "$$this"]}}}}]},
"imparts": "$$REMOVE"}},
{"$group":
{"_id": "$_id._id",
"name": {"$first": "$name"},
"faculty": {"$first": "$faculty"},
"lectures": {"$push": "$lecturers"}}}])
My desired output would be to get the lecturer of each department that teaches the most students. For instance, if I have this:
{
"_id": 19,
"name": "Fredia Donan",
"faculty": "Zoomzone",
"lectures": [
{
"lecturerID": 25,
"name": "Sigismondo Brecknell",
"email": "sbrecknello#pinterest.com",
"imparts": [
{
"_id": 76,
"codCourse": 23,
"language": "Malay",
"max_students": 59,
"students": [
{
"studentID": 25
}
],
"classes": [
{
"date": ISODate("2022-02-09T23:32:59.000Z"),
"cod_classroom": 76
}
]
},
{
"_id": 77,
"codCourse": 24,
"language": "Malayff",
"max_students": 59,
"students": [
{
"studentID": 28
}
],
"classes": [
{
"date": ISODate("2022-02-09T23:32:59.000Z"),
"cod_classroom": 77
}
]
}
]
},
{
"lecturerID": 36,
"name": "Sigismondo Brecknell",
"email": "sbrecknello#pinterest.com",
"imparts": [
{
"_id": 76,
"codCourse": 23,
"language": "Malay",
"max_students": 59,
"students": [
{
"studentID": 45
}
],
"classes": [
{
"date": ISODate("2022-02-09T23:32:59.000Z"),
"cod_classroom": 76
}
]
},
{
"_id": 77,
"codCourse": 24,
"language": "Malayff",
"max_students": 59,
"students": [
{
"studentID": 54
},
{
"studentID": 435
},
{
"studentID": 45
}
],
"classes": [
{
"date": ISODate("2022-02-09T23:32:59.000Z"),
"cod_classroom": 77
}
]
}
]
}
]
}
]
I would like to get that for the department with _id 19 the lecturer that teaches the most students is the one with id 36 since he imparts class to 4 students while the lecturer with id 25 imparts class to only 2 students.
So I would like to get the following output:
"_id": 19,
"name": "Fredia Donan",
"lecturerID": 36,
"maxImpartsStudents": 4
}
And I would like to get this information for every existing department. Every document in the collection department
Welcome Cristina Aranda!
EDIT:
For each lecturer, iterate all imparts using $map and creates one set that includes all students, returning its size as maxImpartsStudents. We use $setUnion to avoid counting the same student more than once, if they are in more than one class of the same lecturer.
$reduce the lecturers in each document to include only the one with maximal value of maxImpartsStudents .
Format
db.collection.aggregate([
{
$project: {
name: 1,
lectures: {
$map: {
input: "$lectures",
as: "item",
in: {
$mergeObjects: [
{
maxImpartsStudents: {
$size: {
$reduce: {
input: "$$item.imparts",
initialValue: [],
in: {$setUnion: ["$$value", "$$this.students"]}
}
}
}
},
{
name: "$$item.name",
lecturerID: "$$item.lecturerID"
}
]
}
}
}
}
},
{
$set: {
lectures: {
$reduce: {
input: "$lectures",
initialValue: {maxImpartsStudents: 0},
in: {
$cond: [
{$gte: ["$$this.maxImpartsStudents", "$$value.maxImpartsStudents"]},
"$$this", "$$value"]
}
}
}
}
},
{
$project: {
lecturerID: "$lectures.lecturerID",
maxImpartsStudents: "$lectures.maxImpartsStudents",
departmentName: "$name"
}
}
])
See how it works on the playground example

Select random id from list in jq and update value

I have some sample payload that I am going to be receiving, it looks like this:
[
{
"Id": "9",
"Line": [
{
"Amount": 100,
"Description": "Weekly Gardening Service",
"DetailType": "SalesItemLineDetail",
"Id": "1",
"LineNum": 1,
"SalesItemLineDetail": {
"ItemAccountRef": {
"name": "Landscaping Services",
"value": "45"
},
"ItemRef": {
"name": "Gardening",
"value": "6"
},
"Qty": 4,
"TaxCodeRef": {
"value": "TAX"
},
"UnitPrice": 25
}
},
{
"Amount": 100,
"DetailType": "SubTotalLineDetail",
"SubTotalLineDetail": {}
}
]
},
{
"Id": "10",
"Line": [
{
"Amount": 140,
"Description": "Weekly Gardening Service",
"DetailType": "SalesItemLineDetail",
"Id": "1",
"LineNum": 1,
"SalesItemLineDetail": {
"ItemAccountRef": {
"name": "Landscaping Services",
"value": "45"
},
"ItemRef": {
"name": "Gardening",
"value": "6"
},
"Qty": 4,
"TaxCodeRef": {
"value": "NON"
},
"UnitPrice": 35
}
},
{
"Amount": 35,
"Description": "Pest Control Services",
"DetailType": "SalesItemLineDetail",
"Id": "2",
"LineNum": 2,
"SalesItemLineDetail": {
"ItemAccountRef": {
"name": "Pest Control Services",
"value": "54"
},
"ItemRef": {
"name": "Pest Control",
"value": "10"
},
"Qty": 1,
"TaxCodeRef": {
"value": "NON"
},
"UnitPrice": 35
}
},
{
"Amount": 175,
"DetailType": "SubTotalLineDetail",
"SubTotalLineDetail": {}
}
]
}
]
These I know are valid and I need to cross reference them, by id, in another payload I am receiving. But, the data I am receiving I can't assume to have valid ID's.
So, I want to take all the valid Ids from above, and shove them, randomly, into the sample data I have, that looks like this ($.invoices[].qbId):
[
{
"id": "fb2430c5-5970-46b0-9947-aaa0b9f177bb",
"invoices": [
{
"description": "2022-02-03 - 179",
"dueDate": "2022-02-03T22:51:10.206Z",
"id": "6f904b18-71c6-4fec-a016-7452f6a6b1dc",
"invoiceDate": "2022-02-03T22:51:10.347Z",
"openBalance": 200,
"paidAmount": 200,
"qbId": "1",
"totalAmount": 212
}
]
},
{
"id": "fa5b77b5-bfd4-4178-ac31-386ec83f530c",
"invoices": [
{
"description": "2022-01-12 - 95",
"dueDate": "2022-01-12T14:08:26.219Z",
"id": "05a58be3-4396-4c15-b9c2-ece68cb2b3fb",
"invoiceDate": "2022-01-12T14:08:26.399Z",
"openBalance": 7.33,
"paidAmount": 7.33,
"qbId": "",
"totalAmount": 7.33
},
{
"description": "2022-01-12 - 95",
"dueDate": "2022-01-12T14:08:26.219Z",
"id": "91f5ecd0-e18d-4029-8745-143323e02007",
"invoiceDate": "2022-01-12T14:08:26.580Z",
"openBalance": 53.13,
"paidAmount": 53.13,
"qbId": "",
"totalAmount": 53.13
}
]
}
]
this jq will get me my ids jq '.QueryResponse.Invoice | map(.Id)' which can be readily consumed by jq. The question now is (and this is what I don't know) how to randomly choose from this array and update the sample payload:
jq 'map(. + {
invoices : .invoices | map(. + {qbId: ??random here })
})
'
If I understood correctly, you want to replace each id field (spelling may differ, sometimes it's Id) with a randomly generated id string.
This solution first extracts the paths of all such id fields (in various spellings) using jq, then iterates over the result in the shell, using uuidgen to generate an id for each, which is fed into another jq call which uses setpath to change the value at the paths saved to the ids generated:
file="input.json"
jq -c '
paths(.. | scalars) | select(.[-1] == ("id", "Id", "ID")) | tojson
' "$file" |
while read -r json; do printf '["%s",%s]\n' "$(uuidgen)" "$json"; done |
jq -n --argfile file "$file" '
reduce inputs as [$id,$json] ($file; setpath($json | fromjson; $id))
'
[
{
"Id": "10162eb7-29ba-4b60-ad20-e5b1133eca63",
"Line": [
{
"Amount": 100,
"Description": "Weekly Gardening Service",
"DetailType": "SalesItemLineDetail",
"Id": "272832df-a8f5-4877-92de-1545150afc33",
"LineNum": 1,
"SalesItemLineDetail": {
"ItemAccountRef": {
"name": "Landscaping Services",
"value": "45"
},
"ItemRef": {
"name": "Gardening",
"value": "6"
},
"Qty": 4,
"TaxCodeRef": {
"value": "TAX"
},
"UnitPrice": 25
}
},
{
"Amount": 100,
"DetailType": "SubTotalLineDetail",
"SubTotalLineDetail": {}
}
]
},
{
"Id": "190b0e50-e007-46a4-b1ca-c3efb762629c",
"Line": [
{
"Amount": 140,
"Description": "Weekly Gardening Service",
"DetailType": "SalesItemLineDetail",
"Id": "f7067227-56d4-4849-873a-3ee5c336999e",
"LineNum": 1,
"SalesItemLineDetail": {
"ItemAccountRef": {
"name": "Landscaping Services",
"value": "45"
},
"ItemRef": {
"name": "Gardening",
"value": "6"
},
"Qty": 4,
"TaxCodeRef": {
"value": "NON"
},
"UnitPrice": 35
}
},
{
"Amount": 35,
"Description": "Pest Control Services",
"DetailType": "SalesItemLineDetail",
"Id": "181d7c6b-0afa-4f44-a568-2c482fc5c285",
"LineNum": 2,
"SalesItemLineDetail": {
"ItemAccountRef": {
"name": "Pest Control Services",
"value": "54"
},
"ItemRef": {
"name": "Pest Control",
"value": "10"
},
"Qty": 1,
"TaxCodeRef": {
"value": "NON"
},
"UnitPrice": 35
}
},
{
"Amount": 175,
"DetailType": "SubTotalLineDetail",
"SubTotalLineDetail": {}
}
]
}
]
This shows how to select elements at random from an array, assuming a bash or sufficiently bash-like environment:
#!/bin/bash
< /dev/urandom tr -cd '0-9' | fold -w 1 | jq -MRnc '
# Output: a prn in range(0;$n) where $n is `.`
def prn:
if . == 1 then 0
else . as $n
| ([1, (($n-1)|tostring|length)]|max) as $w
| [limit($w; inputs)] | join("") | tonumber
| if . < $n then . else ($n | prn) end
end;
# Input: an array
# Output: an array, being a selection of $k elements from . chosen at random without replacement
def prns($k):
if $k <= 0 then []
else . as $in
| length as $n
| if $k > $n then "no can do" | error
else ($n|prn) as $ix
| [$in[$ix]] + (($in[0:$ix] + $in[$ix+1:])|prns($k-1))
end
end;
# Two illustrations
# Three from range(0,10) (with replacement):
[range(0;10) | ( ["a", "b", "c"] | .[length|prn]) ],
# Three from an array, without replacement:
([range(0;10)] | prns(3))
'

Find the documents whose age is 24 or lives in the state UP

Sample data
{
"_id": ObjectId("6068b4d1ba926fd240c5216f"),
"name": "Amit",
"branch": "ECE",
"joiningYear": 2017,
"language": [
"Python",
"C#"
],
"personal": {
"contactinfo": 234556789,
"state": "UP",
"age": 25,
"semestermarks": [
80,
80.1,
98,
70
]
},
"salary": 10000
}
Query
db.college.find({$or:[{"age":24},{"state":"UP"}]})
Demo - https://mongoplayground.net/p/mJeXeT-oCe8
Key is "personal.age"
As your data is inside personal key you have to query inside it.
db.collection.find({
$or: [
{
"personal.age": 24
},
{
"personal.state": "UP"
}
]
})

Resources