Query to $unwind $group and $match with mongo db - database

I am trying to find solution to this query, I am using $group, $match.
This would be the collection:
PlayerTournament: [
{
"_id": 1,
"Name": "John Aims",
"Gender": "M",
"DoB": ISODate("1990-01-01T00:00:00Z"),
"Nationality": "USA",
"Hand": "R",
"YearTurnedPro": 2010,
"Tournament": [
{
"TournamentYear": 2016,
"TournamentCode": "GS1",
"Position": 8,
"PrizeMoney": 125000,
"RankingPoints": 250
},
{
"TournamentYear": 2019,
"TournamentCode": "GS4",
"Position": 2,
"PrizeMoney": 625000,
"RankingPoints": 1000
},
{
"TournamentYear": 2021,
"TournamentCode": "GS3",
"Position": 4,
"PrizeMoney": 312500,
"RankingPoints": 500
}
]
},
{
"_id": 2,
"Name": "George Brown",
"Gender": "M",
"DoB": ISODate("1997-03-04T00:00:00Z"),
"Nationality": "GB",
"Hand": "L",
"YearTurnedPro": 2013,
"Tournament": [
{
"TournamentYear": 2016,
"TournamentCode": "GS1",
"Position": 4,
"PrizeMoney": 250000,
"RankingPoints": 500
},
{
"TournamentYear": 2019,
"TournamentCode": "GS3",
"Position": 2,
"PrizeMoney": 625000,
"RankingPoints": 1000
}
]
},
{
"_id": 3,
"Name": "Kate Upson",
"Gender": "F",
"DoB": ISODate("1999-12-07T00:00:00Z"),
"Nationality": "GB",
"Hand": "L",
"YearTurnedPro": 2013,
"Tournament": [
{
"TournamentYear": 2016,
"TournamentCode": "GS1",
"Position": 1,
"PrizeMoney": 1000000,
"RankingPoints": 2000
},
{
"TournamentYear": 2019,
"TournamentCode": "GS1",
"Position": 4,
"PrizeMoney": 250000,
"RankingPoints": 500
},
{
"TournamentYear": 2020,
"TournamentCode": "GS4",
"Position": 2,
"PrizeMoney": 625000,
"RankingPoints": 1000
},
{
"TournamentYear": 2017,
"TournamentCode": "GS2",
"Position": 2,
"PrizeMoney": 625000,
"RankingPoints": 1000
}
]
},
{
"_id": 4,
"Name": "Mary Bones",
"Gender": "F",
"DoB": ISODate("1998-10-04T00:00:00Z"),
"Nationality": "AUSTRALIA",
"Hand": "L",
"YearTurnedPro": 2015,
"Tournament": [
{
"TournamentYear": 2018,
"TournamentCode": "GS3",
"Position": 1,
"PrizeMoney": 1250000,
"RankingPoints": 2000
},
{
"TournamentYear": 2019,
"TournamentCode": "GS2",
"Position": 2,
"PrizeMoney": 625000,
"RankingPoints": 1000
}
]
},
{
"_id": 5,
"Name": "Yuri Roza",
"Gender": "M",
"DoB": ISODate("2000-05-11T00:00:00Z"),
"Nationality": "BELARUS",
"Hand": "R",
"YearTurnedPro": 2018,
"Tournament": [
{
"TournamentYear": 2020,
"TournamentCode": "GS4",
"Position": 4,
"PrizeMoney": 250000,
"RankingPoints": 500
},
{
"TournamentYear": 2018,
"TournamentCode": "GS2",
"Position": 4,
"PrizeMoney": 312500,
"RankingPoints": 500
}
]
}
]
This is what I tried.
db.PlayerTournament.aggregate([
{
"$unwind": "$Tournament"
},
{
$match: {
"Tournament.TournamentYear": {
$gte: 2020
}
}
},
{
"$group": {
"_id": {
Name: "$Name"
},
"total_qty": {
"$sum": "$Tournament.PrizeMoney"
}
}
}
])
I am getting all the player that have won something after the year 2020.
I am looking to select the players' name of those that won more than 500000(prizemoney) after the year 2020.

You need one more $match stage to filter grouped results:
db.collection.aggregate([
{
"$unwind": "$Tournament"
},
{
$match: {
"Tournament.TournamentYear": {
$gte: 2020
}
}
},
{
"$group": {
"_id": {
Name: "$Name"
},
"total_qty": {
"$sum": "$Tournament.PrizeMoney"
}
}
},
{
"$match": { "total_qty": { "$gte": 500000 } }
}
])
Mongo Playground

Related

How to scroll to a particular section of the data fetched from an API while filtering the data on some parameter?

I am calling an API from a server, and I am getting this as a response. A part of the data I am getting -
{
"id": 322854,
"date": "2022-09-20T18:00:00+00:00",
"time": "18:00",
"timestamp": 1663696800,
"timezone": "UTC",
"stage": null,
"week": null,
"status": {
"long": "Game Finished",
"short": "FT",
"timer": null
},
"league": {
"id": 9,
"name": "French Cup",
"type": "cup",
"season": 2022,
"logo": "https://media-2.api-sports.io/basketball/leagues/9.png"
},
"country": {
"id": 4,
"name": "France",
"code": "FR",
"flag": "https://media-2.api-sports.io/flags/fr.svg"
},
"teams": {
"home": {
"id": 22,
"name": "Boulazac",
"logo": "https://media-2.api-sports.io/basketball/teams/22.png"
},
"away": {
"id": 2294,
"name": "CEP Lorient",
"logo": "https://media-2.api-sports.io/basketball/teams/2294.png"
}
},
"scores": {
"home": {
"quarter_1": 16,
"quarter_2": 12,
"quarter_3": 21,
"quarter_4": 26,
"over_time": null,
"total": 75
},
"away": {
"quarter_1": 9,
"quarter_2": 16,
"quarter_3": 19,
"quarter_4": 23,
"over_time": null,
"total": 67
}
}
}
{
"id": 322854,
"date": "2022-09-20T18:00:00+00:00",
"time": "18:00",
"timestamp": 1663696800,
"timezone": "UTC",
"stage": null,
"week": null,
"status": {
"long": "Game Not Started",
"short": "NS",
"timer": null
},
"league": {
"id": 9,
"name": "French Cup",
"type": "cup",
"season": 2022,
"logo": "https://media-2.api-sports.io/basketball/leagues/9.png"
},
"country": {
"id": 4,
"name": "France",
"code": "FR",
"flag": "https://media-2.api-sports.io/flags/fr.svg"
},
"teams": {
"home": {
"id": 22,
"name": "Boulazac",
"logo": "https://media-2.api-sports.io/basketball/teams/22.png"
},
"away": {
"id": 2294,
"name": "CEP Lorient",
"logo": "https://media-2.api-sports.io/basketball/teams/2294.png"
}
},
"scores": {
"home": {
"quarter_1": 16,
"quarter_2": 12,
"quarter_3": 21,
"quarter_4": 26,
"over_time": null,
"total": 75
},
"away": {
"quarter_1": 9,
"quarter_2": 16,
"quarter_3": 19,
"quarter_4": 23,
"over_time": null,
"total": 67
}
}
}
There are a bunch of such objects in an array each having a different fixture. As you can see that status?.short === "NS" in one while the other is status?.short === "FT". Now, I want to filter the data using these properties.
<div className="fixturesTab-container">
<div className="fixturesTab-container__header">
<h4>Fixtures</h4>
</div>
<div className="fixturesTab-container__body">
{fixtures.map((fixture) => (
<div
key={fixture?.id}
className="fixturesTab-container__body__box"
>
......
</div>
))}
</div>
</div>
I want the data to be displayed in such a manner that when the user browse to that page and the API is being fetched, eventhough the entire data (fixtures) is being displayed in the page. I want the fixtures that have not yet started, i.e has the property of status?.short === "NS", to be displayed in the window that is visible to the user. And the rest data can be acquired by either scrolling up, which will have the fixtures that have been completed, or by scrolling down which will have the fixtures that have not yet been started.
If, I am unable to understand what I want properly, then you can check out this page . This is exactly what I am planning to achieve.

Solr get facets on each root document

I have a JSON with documents and each document has a few _childDocuments_ I want to search in the parent and get back the parents and facets on each parent separate
Here is a sample JSON
[{
"id": 1,
"productName": "Ford Explorer",
"_childDocuments_": [{
"id": 6,
"color": "blue",
"price": 1000
},
{
"id": 7,
"color": "red",
"price": 2000
}
]
},
{
"id": 1,
"productName": "Ford F150",
"_childDocuments_": [{
"id": 10,
"color": "blue",
"price": 5000
},
{
"id": 11,
"color": "red",
"price": 6000
}
]
},
{
"id": 2,
"productName": "Toyota Highlander",
"_childDocuments_": [{
"id": 8,
"color": "green",
"price": 1200
},
{
"id": 9,
"color": "red",
"price": 2000
}
]
}
]
I want when I search for Ford I should get back all Fords and in each root document get back facets of the children.
Something like this:
[{
"id": 1,
"productName": "Ford Explorer",
"facets": {
"count": 2,
"prices": {
"buckets": [{
"val": 2000,
"count": 1
},
{
"val": 3000,
"count": 1
}
]
}
}
},
{
"id": 1,
"productName": "Ford F150",
"facets": {
"count": 2,
"prices": {
"buckets": [{
"val": 5000,
"count": 1
},
{
"val": 6000,
"count": 1
}
]
}
}
}
]
This is what I got so far
q={!parent+which=type:parent}&json.facet={ "prices": {
"type": "range",
"field": "price",
"start": 1000,
"end": 1000,
"gap": 1000
}
,
}
But this is only returning facets on all child documents.
Thanks in advance

Post Array from React using Axios

I want to post an array with `axios` into `react`, however it is not working...
The array to post:
[
{
"Nombre": "Remera",
"Precio": "1599",
"Photo": null,
"Stock": [
{
"id": 0,
"talle": "M",
"cantidad": "12"
},
{
"id": 1,
"talle": "S",
"cantidad": "14"
},
{
"id": 2,
"talle": "XS",
"cantidad": "6"
}
]
}
]
The data posted:
{
"Nombre": "Remera",
"Precio": "1599",
"Photo": null,
"Stock": [
{
"id": 0,
"talle": "M",
"cantidad": "12"
},
{
"id": 1,
"talle": "S",
"cantidad": "14"
},
{
"id": 2,
"talle": "XS",
"cantidad": "6"
}
]
}
Can someone help me on this one?
Thanks!
You can not send the pure array.
You have to send the JSON as object and add the identifier to your array as below:
{
"data": [
{
"Nombre": "Remera",
"Precio": "1599",
"Photo": null,
"Stock": [
{
"id": 0,
"talle": "M",
"cantidad": "12"
},
{
"id": 1,
"talle": "S",
"cantidad": "14"
},
{
"id": 2,
"talle": "XS",
"cantidad": "6"
}
]
},
{...nextObject},
{...nextObject}
]
}

How do I query more information of an array in a json file using mongodb?

{
"userid": 1,
"following": [2, 3],
"salary": 1000,
"age": 19
},
{
"userid": 2,
"following": [],
"salary": 2000,
"age": 25
},
{
"userid": 3,
"following": [2],
"salary": 1500,
"age": 20
},
"following" shows the userid of the people the user follows.
I would like to have an output query showing the userid and the salary of those that he follows only (no need age and the user's salary). I saw that $lookup() could be used but I am unsure of using it.
My desired output is shown below, may I know if it is possible? I am also open to any other outputs. Thank you in advance!
{
"userid": 1,
"following": [{2, 2000}, {3, 1500}]
},
{
"userid": 2,
"following": []
},
{
"userid": 3,
"following": [{2, 2000}]
}
You can use graphLookup to join the collection to itself, then use project stage to exclude unwanted fields like this:
db.collection.aggregate([
{
$graphLookup: {
from: "collection",
startWith: "$following",
connectFromField: "following",
connectToField: "userid",
as: "following",
maxDepth: 0
}
},
{
$project: {
"_id": 0,
"userid": 1,
"following.salary": 1,
"following.userid": 1
}
}
])
The output is not the same as you wanted, but as you said you are open to all suggestions I posted:
[
{
"following": [
{
"salary": 2000,
"userid": 2
},
{
"salary": 1500,
"userid": 3
}
],
"userid": 1
},
{
"following": [],
"userid": 2
},
{
"following": [
{
"salary": 2000,
"userid": 2
}
],
"userid": 3
}
]
Playground:
https://mongoplayground.net/p/WnF1vRY5Avr

Summing values across array elements

I have the following document:
{
"month": 2,
"year": 2019,
"categories": [
{
"title": "Income",
"budget": 5000,
"amount": 2000,
"subcategories": [
{
"title": "Paychecks",
"budget": 4000,
"amount": 1000,
"transactions": [
{
"day": 3,
"amount": 600,
"vendor": "Microsoft",
"memo": "Paycheck 1"
},
{
"day": 10,
"amount": 400,
"vendor": "Google",
"memo": "Paycheck 2"
}
]
},
{
"title": "Other",
"budget": 1000,
"amount": 1000,
"transactions": [
{
"day": 7,
"amount": 600,
"vendor": "Mom",
"memo": "Gift for Baby"
},
{
"day": 19,
"amount": 400,
"vendor": "Amy",
"memo": "Gift for Julie"
}
]
}
]
},
{
"title": "Housing",
"budget": 3000,
"amount": 3000,
"subcategories": [
{
"title": "Rent",
"budget": 2000,
"amount": 2000,
"transactions": [
{
"day": 1,
"amount": 2000,
"vendor": "Homes4Rent",
"memo": "Rent"
}
]
},
{
"title": "Electric",
"budget": 1000,
"amount": 1000,
"transactions": [
{
"day": 14,
"amount": 1000,
"vendor": "Tampa Electric",
"memo": "Electric"
}
]
}
]
}
]}
I need to ensure that the "categories.budget" amount is always equal to the sum of the "categories.subcategories.budget" amounts for that category.
For example, I need to ensure that the budget for "Income" is 5000 because the sum of the budgets for its subcategories ("Paychecks" and "Other") is exactly 5000.
The most desirable solution would be one that can perform the sum and the "categories.budget" field update in a single query but I'm unsure how to accomplish this or if this is even possible. Here's what I've come up with so far:
db.collection.aggregate(
[
{
$match: {
month:2,
year:2019
}
},
{
$unwind: "$categories"
},
{
$unwind: "$categories.subcategories"
},
{
$group: {
_id: {
category: "$categories.title"
},
sum: {
$sum: "$categories.subcategories.budget"
}
}
}
])
Unfortunately, this only seems to be printing out the category budgets instead of summing them. Whichever solution is realized will ultimately be used in various places throughout my application to perform similar operations as well.
Thank you so much and let me know if I need to provide any further information.

Resources