i want to have a lookup between two nested object arrays in MongoDB - mongodb

I have the following two collections
1)template_data
2)static_data
template_data
{
"_id" : "e1a0a44a-abc1-4185-aefc-2194597a62a2",
"templateName" : "IB Tradebook",
"period" : "Jul-2020",
"attributeData" : [
{
"attributeName" : "BUY/SELL",
"attributeValue" : "B"
},
{
"attributeName" : "EXCHANGE ID",
"attributeValue" : "NYBT"
},
{
"attributeName" : "ACCOUNT NUMBER",
"attributeValue" : "XXXXXXX"
},
{
"attributeName" : "BTML NAME",
"attributeValue" : "CCCCCCCCCCCCCCCCCCCCCC"
},
{
"attributeName" : "TRADE SUMMARY ID",
"attributeValue" : "AAA-BBBB-CCCC"
},
{
"attributeName" : "TRADER NAME",
"attributeValue" : "TOM"
}]
},
{
"_id" : "e1a0a44a-abc1-4185-aefc-2194597a62a2",
"templateName" : "CB Tradebook",
"period" : "Jul-2020",
"attributeData" : [
{
"attributeName" : "BUY/SELL",
"attributeValue" : "B"
},
{
"attributeName" : "EXCHANGE ID",
"attributeValue" : "NYBT"
},
{
"attributeName" : "ACCOUNT NUMBER",
"attributeValue" : "XXXXXXX"
},
{
"attributeName" : "BTML NAME",
"attributeValue" : "CCCCCCCCCCCCCCCCCCCCCC"
},
{
"attributeName" : "TRADE SUMMARY ID",
"attributeValue" : "AAA-BBBB-CCCC"
},
{
"attributeName" : "TRADER NAME",
"attributeValue" : "TOM"
}]
}
static_data
{
"_id" : "ce62f96f-5ef5-4bb0-8bab-9b0a743f968c",
"templateName" : "User_Inventory",
"period" : "Jul-2020",
"attributeData" : [
{
"attributeName" : "TRADING SYSTEM",
"attributeValue" : "ZZZZ",
},
{
"attributeName" : "CLIENT NAME",
"attributeValue" : "XXX TRADING COMPANY",
},
{
"attributeName" : "EFLOW USER ID",
"attributeValue" : "",
},
{
"attributeName" : "TRADER EMAIL ID",
"attributeValue" : "xxxx#goole.com",
},
{
"attributeName" : "USER ID",
"attributeValue" : "TOM"
}]
},
{
"_id" : "ce62f96f-5ef5-4bb0-8bab-9b0a743f968c",
"templateName" : "Client_Inventory",
"period" : "Jul-2020",
"attributeData" : [
{
"attributeName" : "TRADING SYSTEM",
"attributeValue" : "ZZZZ",
},
{
"attributeName" : "CLIENT NAME",
"attributeValue" : "XXX TRADING COMPANY",
},
{
"attributeName" : "EFLOW USER ID",
"attributeValue" : "",
},
{
"attributeName" : "TRADER EMAIL ID",
"attributeValue" : "xxxx#goole.com",
},
{
"attributeName" : "USER ID",
"attributeValue" : "TOM"
}]
}
My requirement is
1.From template_data collection i have filter all the documents with
templateName = 'IB Tradebook',
period = 'Jul-2020'
2.From statci_data collection i have filter all the documents with
templateName = 'User_Inventory',
period = 'Jul-2020'
3.filter condition from both collections
template_data.attributeData.attributeName = "TRADER NAME"
static_data.attributeData.attributeName = "USER ID"
SQL Equivalent:
Select template_data.attributeData.attributeName, template_data.attributeData.attributeValue,
static_data.attributeData.attributeName, static_data.attributeData.attributeValue
from template_data
left join static_data on (template_data.attributeData.attributeValue = static_data.attributeData.attributeValue)
Where template_data.attributeData.attributeName in ("BUY/SELL", "EXCHANGE ID", "TRADER NAME")
and static_data.attributeData.attributeName in ("TRADING SYSTEM","CLIENT NAME", "USER ID")
and template_data.templateName = 'IB Tradebook' and template_data.period = 'Jul-2020'
and static_data.templateName = 'User_Inventory' and static_data.period = 'Jul-2020';
db.template_data.aggregate([
{
$lookup :
{ from : "static_data",
localField: "template_data.attributeData.atributeValue"
ForeignField : "static_data.attributeData.atributeValue"
as : "static_details"
}
}
4.$project and expected results
for the above condition the
"template_data.attributeData.atributeValue" (TOM) matches with
"static_data.attributeData.atributeValue" (TOM)
AttributNames to be considered in output
template_data : attributeName["BUY/SELL", "EXCHANGE ID", "TRADER NAME"]
static_data : attributeName["TRADING SYSTEM","CLIENT NAME", "USER ID" ]
output :
{
"_id" : "e1a0a44a-abc1-4185-aefc-2194597a62a2",
"templateName" : "IB Tradebook",
"period" : "Jul-2020",
"attributeData" : [
{
"attributeName" : "BUY/SELL",
"attributeValue" : "B"
},
{
"attributeName" : "EXCHANGE ID",
"attributeValue" : "NYBT"
},
{
"attributeName" : "TRADER NAME",
"attributeValue" : "TOM"
}
],
static_details : {
"_id" : "ce62f96f-5ef5-4bb0-8bab-9b0a743f968c",
"templateName" : "User_Inventory",
"period" : "Jul-2020",
"attributeData" : [
{
"attributeName" : "TRADING SYSTEM",
"attributeValue" : "ZZZZ",
},
{
"attributeName" : "CLIENT NAME",
"attributeValue" : "XXX TRADING COMPANY",
},
{
"attributeName" : "USER ID",
"attributeValue" : "TOM"
}]
}
}

Related

Group by a hirearchical array and get count for each level

Hi have a collection with user detaiils. each document has the user's location address as an array.
[
{
"name": "User 1",
"location": ["India", "Uttar Pradesh","Budaun","Sahaswan"]
},
{
"name": "User 2",
"location": ["India", "Uttar Pradesh","Budaun", "Ujhani"]
},
{
"name": "User 3",
"location": ["India", "Uttar Pradesh","Budaun"]
},
{
"name": "User 4",
"location": ["India", "Uttar Pradesh"]
},
{
"name": "User 5",
"location": ["Sri Lanka", "North Province", "Mannar District", "Mannar"]
}
]
This location array is not a fixed length array. Can I get the user count for each level using mongodb aggregations?
My Expected Output
[
{
"name": "India",
"userCount": 4,
"items":[
{
"name":"Uttar Pradesh",
"userCount": 4,
"items": [
{
"name": "Budaun",
"userCount": 3,
"items":[
{
"name": "Sahaswan",
"userCount": 1,
"items":[]
},
{
"name": "Ujhani",
"userCount": 1,
"items":[]
}
]
}
]
}
]
},
{
"name": "Sri Lanka",
"userCount": 1,
"items":[
{
"name":"North Province",
"userCount": 1,
"items": [
{
"name": "Mannar District",
"userCount": 1,
"items":[
{
"name": "Mannar",
"userCount": 1,
"items":[]
}
]
}
]
}
]
}
]
I think first step you may $unwind your data.
db.collection.aggregate([
{
$unwind:
{
path: "$location",
includeArrayIndex: "level",
preserveNullAndEmptyArrays: false
}
}
])
Then, you can use the output from $unwind to get what you want.
/* 1 */
{
"_id" : ObjectId("612c6d30a6f18312d1338383"),
"name" : "User 1",
"location" : "India",
"level" : NumberLong(0)
}
/* 2 */
{
"_id" : ObjectId("612c6d30a6f18312d1338383"),
"name" : "User 1",
"location" : "Uttar Pradesh",
"level" : NumberLong(1)
}
/* 3 */
{
"_id" : ObjectId("612c6d30a6f18312d1338383"),
"name" : "User 1",
"location" : "Budaun",
"level" : NumberLong(2)
}
/* 4 */
{
"_id" : ObjectId("612c6d30a6f18312d1338383"),
"name" : "User 1",
"location" : "Sahaswan",
"level" : NumberLong(3)
}
/* 5 */
{
"_id" : ObjectId("612c6d30a6f18312d1338386"),
"name" : "User 2",
"location" : "India",
"level" : NumberLong(0)
}
/* 6 */
{
"_id" : ObjectId("612c6d30a6f18312d1338386"),
"name" : "User 2",
"location" : "Uttar Pradesh",
"level" : NumberLong(1)
}
/* 7 */
{
"_id" : ObjectId("612c6d30a6f18312d1338386"),
"name" : "User 2",
"location" : "Budaun",
"level" : NumberLong(2)
}
/* 8 */
{
"_id" : ObjectId("612c6d30a6f18312d1338386"),
"name" : "User 2",
"location" : "Ujhani",
"level" : NumberLong(3)
}
/* 9 */
{
"_id" : ObjectId("612c6d30a6f18312d1338389"),
"name" : "User 3",
"location" : "India",
"level" : NumberLong(0)
}
/* 10 */
{
"_id" : ObjectId("612c6d30a6f18312d1338389"),
"name" : "User 3",
"location" : "Uttar Pradesh",
"level" : NumberLong(1)
}
/* 11 */
{
"_id" : ObjectId("612c6d30a6f18312d1338389"),
"name" : "User 3",
"location" : "Budaun",
"level" : NumberLong(2)
}
/* 12 */
{
"_id" : ObjectId("612c6d30a6f18312d133838c"),
"name" : "User 4",
"location" : "India",
"level" : NumberLong(0)
}
/* 13 */
{
"_id" : ObjectId("612c6d30a6f18312d133838c"),
"name" : "User 4",
"location" : "Uttar Pradesh",
"level" : NumberLong(1)
}
/* 14 */
{
"_id" : ObjectId("612c6d30a6f18312d133838f"),
"name" : "User 5",
"location" : "Sri Lanka",
"level" : NumberLong(0)
}
/* 15 */
{
"_id" : ObjectId("612c6d30a6f18312d133838f"),
"name" : "User 5",
"location" : "North Province",
"level" : NumberLong(1)
}
/* 16 */
{
"_id" : ObjectId("612c6d30a6f18312d133838f"),
"name" : "User 5",
"location" : "Mannar District",
"level" : NumberLong(2)
}
/* 17 */
{
"_id" : ObjectId("612c6d30a6f18312d133838f"),
"name" : "User 5",
"location" : "Mannar",
"level" : NumberLong(3)
}

Cloudformation template with multiple resources

I have a fairly simple cloudformation template. I am trying to learn about them. I created one where I am trying to create 2 dyanmo table when I deploy the stack. But only one table gets created. Not two. I am not sure what is wrong with my syntax. Pasting the json below
"AWSTemplateFormatVersion" : "2010-09-09",
"Resources" : {
"resource1" : {
"Type" : "AWS::DynamoDB::Table",
"Properties" : {
"AttributeDefinitions" : [
{
"AttributeName" : "Name",
"AttributeType" : "S"
},
{
"AttributeName" : "Age",
"AttributeType" : "S"
}
],
"KeySchema" : [
{
"AttributeName" : "Name",
"KeyType" : "HASH"
},
{
"AttributeName" : "Age",
"KeyType" : "RANGE"
}
],
"ProvisionedThroughput" : {
"ReadCapacityUnits" : "5",
"WriteCapacityUnits" : "5"
},
"TableName" : "tablecloudformation3_1"
}
}
},
"Resources" : {
"resource2" : {
"Type" : "AWS::DynamoDB::Table",
"Properties" : {
"AttributeDefinitions" : [
{
"AttributeName" : "Name",
"AttributeType" : "S"
},
{
"AttributeName" : "Age",
"AttributeType" : "S"
}
],
"KeySchema" : [
{
"AttributeName" : "Name",
"KeyType" : "HASH"
},
{
"AttributeName" : "Age",
"KeyType" : "RANGE"
}
],
"ProvisionedThroughput" : {
"ReadCapacityUnits" : "5",
"WriteCapacityUnits" : "5"
},
"TableName" : "tablecloudformation3_2"
}
}
},
}
There are few mistakes in the template. One already pointed out by #MariaInesParnisari.
The other ones are missing open bracket and unneeded brackets in the middle.
I fixed the template and can confirm it works:
{
"AWSTemplateFormatVersion" : "2010-09-09",
"Resources" : {
"resource1" : {
"Type" : "AWS::DynamoDB::Table",
"Properties" : {
"AttributeDefinitions" : [
{
"AttributeName" : "Name",
"AttributeType" : "S"
},
{
"AttributeName" : "Age",
"AttributeType" : "S"
}
],
"KeySchema" : [
{
"AttributeName" : "Name",
"KeyType" : "HASH"
},
{
"AttributeName" : "Age",
"KeyType" : "RANGE"
}
],
"ProvisionedThroughput" : {
"ReadCapacityUnits" : "5",
"WriteCapacityUnits" : "5"
},
"TableName" : "tablecloudformation3_1"
}
},
"resource2" : {
"Type" : "AWS::DynamoDB::Table",
"Properties" : {
"AttributeDefinitions" : [
{
"AttributeName" : "Name",
"AttributeType" : "S"
},
{
"AttributeName" : "Age",
"AttributeType" : "S"
}
],
"KeySchema" : [
{
"AttributeName" : "Name",
"KeyType" : "HASH"
},
{
"AttributeName" : "Age",
"KeyType" : "RANGE"
}
],
"ProvisionedThroughput" : {
"ReadCapacityUnits" : "5",
"WriteCapacityUnits" : "5"
},
"TableName" : "tablecloudformation3_2"
}
}
}
}
More generally, the CloudFormation Linter can help catch these template issues faster with errors like:
E0000 Duplicate found "Resources" (line 35)

I would like to add couple of fields from a collection(Release) into another collection(Story)based on matching conditions(ID) in Mongo DB

Collection: Story ( This is the main collection where 2 fields from the release collection needs to be appended.)
{
"_id" : ObjectId("5d131d93e2838c3b753e6631"),
"expand" : "schema,names",
"total" : 2,
"issues" : [
{
"expand" : "operations,versionedRepresentations,editmeta,changelog,renderedFields",
"id" : "131",
"fields" : {
"updateddate" : "2019-06-12T10:58:12.345+0530",
"fixVersions" : [
{
"id" : "10008",
"description" : "New Release 2",
"name" : "New Release 2"
},
{
"id" : "10009",
"description" : "New Release 3",
"name" : "New Release 3"
}
],
"aggregatetimespent" : null
}
},
{
"expand" : "operations,versionedRepresentations,editmeta,changelog,renderedFields",
"id" : "10032",
"fields" : {
"updateddate" : "2019-06-22T10:58:12.345+0530",
"fixVersions" : [
{
"id" : "10008",
"description" : "New Release 2",
"name" : "New Release 2"
}
],
"aggregatetimespent" : null
}
}
]
}
Collection: Release( This is another collection with few fields from which fields)
{
"_id" : ObjectId("5d131a35e2838c3b753e6626"),
"id" : "10008",
"startDate" : "2019-06-28",
"releaseDate" : "2019-07-05"
}
{
"_id" : ObjectId("5d131a35e2838c3b753e6626"),
"id" : "10009",
"startDate" : "2019-08-28",
"releaseDate" : "2019-09-05"
}
Start date and Release Date need to be appended to the array fields based on the condition.
Expected output:
Collection: Story
{
"_id" : ObjectId("5d131d93e2838c3b753e6631"),
"expand" : "schema,names",
"total" : 2,
"issues" : [
{
"expand" : "operations,versionedRepresentations,editmeta,changelog,renderedFields",
"id" : "131",
"fields" : {
"updateddate" : "2019-06-12T10:58:12.345+0530",
"fixVersions" : [
{
"id" : "10008",
"description" : "New Release 2",
"name" : "New Release 2",
"startDate" : "2019-06-28",
"releaseDate" : "2019-07-05"
},
{
"id" : "10009",
"description" : "New Release 3",
"name" : "New Release 3",
"startDate" : "2019-08-28",
"releaseDate" : "2019-09-05"
}
],
"aggregatetimespent" : null
}
},
{
"expand" : "operations,versionedRepresentations,editmeta,changelog,renderedFields",
"id" : "10032",
"fields" : {
"updateddate" : "2019-06-22T10:58:12.345+0530",
"fixVersions" : [
{
"id" : "10008",
"description" : "New Release 2",
"name" : "New Release 2",
"startDate" : "2019-06-28",
"releaseDate" : "2019-07-05"
}
],
"aggregatetimespent" : null
}
}
]
}

MongoDb query for getting count information after aggregation for more than one size from collecgtion

What i am try to achieve is getting more than one size from collection.below query not working as expected.
shown below is collection.
{
"_id" : ObjectId("59e9c029c817c359508b4568"),
"client_id" : "20",
"device_details" : {
"type" : "others",
"name" : "Google Bot",
"os" : null,
"os_version" : null,
"browser" : null,
"browser_version" : null,
"user_agent" : "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)",
"width" : NumberLong("1024"),
"height" : NumberLong("1024"),
"fingerid" : "c714d8f7eb7d216693c0ddd06cfc0940",
"canvasfingerid" : "84f1922a78572180c7f29f938486c1db",
"webglfingerid" : "24700f9f1986800ab4fcc880530dd0ed",
"hardwarefingerid" : "431171c98112e1f0dfb3b40f617111f9",
"platformos" : "Linux x86_64",
"adblock" : "false",
"donot" : "unknown",
"liedlanguage" : "false",
"liedos" : "false",
"liedbrowser" : "false",
"ratio" : NumberLong("1"),
"depth" : NumberLong("24"),
"language" : "en-US",
"liedresolution" : "false",
"timezoneoffset" : "420",
"regularplugin" : "No plugins found",
"localstorage" : "true",
"sessionstorage" : "true",
"touchsupport" : "false",
"canvas" : "Tracked",
"webgl" : "Unknown",
"hardwareconc" : "8",
"referrer" : null,
"referrer_url" : null,
"scrorientation" : "landscape-primary"
},
"ip_list" : [
"66.249.66.146",
"66.249.66.21"
],
"ip_details" : [
{
"ip_address" : "66.249.66.146",
"latitude" : 37.41919999999999,
"longitude" : -122.0574,
"city" : "Mountain View",
"postal_code" : "94043",
"country_code" : "US",
"country_name" : "United States",
"continent_code" : "NA",
"created_date" : ISODate("2017-10-20T14:51:45.176+05:30")
},
{
"ip_address" : "66.249.66.21",
"latitude" : 37.41919999999999,
"longitude" : -122.0574,
"city" : "Mountain View",
"postal_code" : "94043",
"country_code" : "US",
"country_name" : "United States",
"continent_code" : "NA",
"created_date" : ISODate("2017-10-24T08:27:24.385+05:30")
}
],
"urls" : [
{
"host" : {
"time" : ISODate("2017-10-20T14:51:45.197+05:30"),
"url" : "http://www.indianmobileprices.com/index.php/mobile/info/9-APPLE/7062-APPLE-iPhone+7+/dehradun"
}
},
{
"host" : {
"time" : ISODate("2017-10-24T08:27:24.405+05:30"),
"url" : "http://www.indianmobileprices.com/brand/mobiles/10-LENOVO/bhubaneswar"
}
}
],
"created_date" : ISODate("2017-10-24T08:27:24.405+05:30"),
"current_timestamp" : NumberLong("1508491305"),
"traffic" : null,
"traffic_url" : [
{
"url" : "",
"date" : ISODate("2017-10-20T14:51:45.197+05:30")
},
{
"url" : "",
"date" : ISODate("2017-10-24T08:27:24.314+05:30")
},
{
"url" : "",
"date" : ISODate("2017-10-24T08:27:24.578+05:30")
}
],
"returning_user" : NumberLong("1"),
"returninguser" : "yes",
"updated_timestamp" : NumberLong("1508813844"),
"multisession" : NumberLong("2")
},
Here is the query i have used.
db.audience.aggregate([
{"$group":{
"_id":"$ip_details.country_name",
"device_information":{
"$addToSet":{
"ipaddress":"$ip_details.ip_address",
"country_name":"$ip_details.country_name",
"device_name":"$device_details.name",
"device_type":"$device_details.type",
"device_os":"$device_details.os",
"devicen_osversion":"$device_details.os_version"
}
},
"count":{"$sum":1}
}},
{ "$project":{
"device_information":1,
"_id":0,
"countrySize":{ "$size":"$device_information.country_name" },
"desktopSize":{ "$size":'$device_information.device_type' }
}},
{ "$match":{ "countrySize":{"$gt":1} }},
{ "$match":{"desktopSize":{"$device_information.device_type":"desktop"} }}
])
For that i could not get expected result is given below.need to change query for the result.
/* 1 */
{
"countrySize" : NumberInt("4"),
"desktopSize" : NumberInt("2"),
"device_information" : [
{
"country_name" : [
"Singapore"
],
"device_name" : "generic web browser",
"device_os" : "Windows",
"device_type" : "desktop",
"devicen_osversion" : "10",
"ipaddress" : [
"52.220.245.83"
]
},
{
"country_name" : [
"Singapore"
],
"device_name" : "Apple iPhone",
"device_os" : "iOS",
"device_type" : "others",
"devicen_osversion" : "10.3.3",
"ipaddress" : [
"185.89.218.227"
]
},
{
"country_name" : [
"Singapore"
],
"device_name" : "generic web browser",
"device_os" : "Mac OS X",
"device_type" : "desktop",
"devicen_osversion" : "10.12.6",
"ipaddress" : [
"121.7.242.239"
]
},
{
"country_name" : [
"Singapore"
],
"device_name" : "Samsung Galaxy Note 4",
"device_os" : "Android",
"device_type" : "mobile",
"devicen_osversion" : "6.0.1",
"ipaddress" : [
"220.255.108.72"
]
}
]
},
I need query for results that is given above.

How to use MongoDB $group stage to both group and count repeated values?

I am having trouble with the $group stage in my aggregation. I want to group all the "recentPlays.quiz" values together and count the repeated values, so the end result I want from the aggregation is two fields: the quiz object and the total. In this case it would be something like:
{
"recentPlays" : [
{
"quiz" : {
"author" : "red-tester1",
"title" : "Asdffff Dfasdf"
},
"count": 1
},
{
"quiz" : {
"author" : "red-tester3",
"title" : "Creation Test 2"
},
"count": 1
},
{
"quiz" : {
"author" : "blue-tester1",
"title" : "Finky Fink"
},
"count": 4
}
]
}
Here is the aggregation I have so far:
db.users.aggregate([
{$match: { "recentPlays.date": {$gte:twentyFourHrsAgo}}},
{$project: {"recentPlays.quiz":1, _id:0}}
]).pretty();
Here is that aggregation's output:
MongoDB shell version: 3.2.1
connecting to: videoQuiz
{
"recentPlays" : [
{
"quiz" : {
"author" : "red-tester1",
"title" : "Asdffff Dfasdf"
}
},
{
"quiz" : {
"author" : "red-tester3",
"title" : "Creation Test 2"
}
},
{
"quiz" : {
"author" : "blue-tester1",
"title" : "Finky Fink"
}
},
{
"quiz" : {
"author" : "blue-tester1",
"title" : "Finky Fink"
}
},
{
"quiz" : {
"author" : "blue-tester1",
"title" : "Finky Fink"
}
},
{
"quiz" : {
"author" : "blue-tester1",
"title" : "Finky Fink"
}
}
]
}
Here is the entire collection:
MongoDB shell version: 3.2.1
connecting to: videoQuiz
{
"_id" : ObjectId("580f7be62c6fd3c8065577f5"),
"user" : "blue-tester1",
"email" : "aslfjjcc#lkcjasdc.com",
"createdAt" : ISODate("2016-10-25T15:36:06.933Z"),
"recentPlays" : [
{
"quiz" : {
"author" : "red-tester1",
"title" : "Asdffff Dfasdf"
},
"score" : "0",
"date" : ISODate("2016-10-25T15:36:27.546Z")
},
{
"quiz" : {
"author" : "red-tester3",
"title" : "Creation Test 2"
},
"score" : "100",
"date" : ISODate("2016-10-25T15:37:09.142Z")
}
],
"mostRecentQuiz" : {
"author" : "red-tester3",
"title" : "Creation Test 2"
},
"mostRecentQuizTime" : ISODate("2016-10-25T15:37:09.142Z"),
"plays" : 2
}
{
"_id" : ObjectId("580a5dea650296d808082e65"),
"user" : "red-tester3",
"email" : "aldkdk#ccc.com",
"createdAt" : ISODate("2016-10-21T18:26:50.870Z"),
"recentPlays" : [
{
"quiz" : {
"author" : "red-tester2",
"title" : "TOP PLAYED QUIZ - Today"
},
"score" : "0",
"date" : ISODate("2016-10-21T18:27:16.292Z")
},
{
"quiz" : {
"author" : "red-tester2",
"title" : "TOP LIKED QUIZ - TODAY"
},
"score" : "100",
"date" : ISODate("2016-10-21T18:27:32.788Z")
},
{
"quiz" : {
"author" : "red-tester2",
"title" : "TOP LIKED QUIZ - TODAY"
},
"score" : "100",
"date" : ISODate("2016-10-21T18:27:44.497Z")
},
{
"quiz" : {
"author" : "Bertram",
"title" : "frfrf"
},
"score" : "100",
"date" : ISODate("2016-10-21T18:28:43.893Z")
},
{
"quiz" : {
"author" : "Bertram",
"title" : "Here We Go With the New Thing"
},
"score" : "0",
"date" : ISODate("2016-10-21T18:43:51.205Z")
},
{
"quiz" : {
"author" : "red-tester3",
"title" : "Presidents of the United States"
},
"score" : "0",
"date" : ISODate("2016-10-23T00:53:29.167Z")
},
{
"quiz" : {
"author" : "red-tester3",
"title" : "Presidents of the United States"
},
"score" : "0",
"date" : ISODate("2016-10-23T00:53:44.815Z")
},
{
"quiz" : {
"author" : "red-tester3",
"title" : "Creation Test 1"
},
"score" : "100",
"date" : ISODate("2016-10-23T23:50:55.355Z")
},
{
"quiz" : {
"author" : "red-tester3",
"title" : "Creation Test 2"
},
"score" : "100",
"date" : ISODate("2016-10-23T23:52:33.210Z")
},
{
"quiz" : {
"author" : "red-tester3",
"title" : "Here Is a New Title"
},
"score" : "100",
"date" : ISODate("2016-10-23T23:58:53.683Z")
}
],
"mostRecentQuiz" : {
"author" : "red-tester3",
"title" : "Here Is a New Title"
},
"mostRecentQuizTime" : ISODate("2016-10-23T23:58:53.683Z"),
"plays" : 10,
"likedQuizzes" : [
{
"title" : "TOP LIKED QUIZ - TODAY",
"author" : "red-tester2",
"date" : ISODate("2016-10-21T18:27:34.893Z")
},
{
"title" : "frfrf",
"author" : "Bertram",
"date" : ISODate("2016-10-21T18:28:45.863Z")
},
{
"title" : "Here We Go With the New Thing",
"author" : "Bertram",
"date" : ISODate("2016-10-21T18:43:53.148Z")
}
],
"createdQuizzes" : [
{
"title" : "Yeah Here We Go",
"id" : ObjectId("580a63f274b9a89c061f973e")
},
{
"title" : "Z Alpha",
"id" : ObjectId("580a641474b9a89c061f973f")
},
{
"title" : "Tags Limit Test",
"id" : ObjectId("580a6bda8d8049ac0bc1df2e")
},
{
"title" : "Tags Limit test2",
"id" : ObjectId("580a6bf98d8049ac0bc1df2f")
},
{
"title" : "Presidents of the United States",
"id" : ObjectId("580c09d28d8049ac0bc1df30")
},
{
"title" : "Creation Test 1",
"id" : ObjectId("580d4cca8d8049ac0bc1df31")
},
{
"title" : "Creation Test 2",
"id" : ObjectId("580d4d2d8d8049ac0bc1df32")
},
{
"title" : "Here Is a New Title",
"id" : ObjectId("580d4ead8d8049ac0bc1df33")
}
]
}
Thanks in advance for any guidance. Please excuse the dummy text in these documents, it is for testing purposes only.
This will be a two step process. The first step is to $unwind the "recentPlays" array. The second step is to $group by "recentPlays.quiz".
For example:
db.users.aggregate([
{ "$match" : { "recentPlays.date": { "$gte" : twentyFourHrsAgo}}},
{ "$project" : {"recentPlays.quiz":1, _id:0}},
{ "$unwind" : "$recentPlays" },
{ "$group" : { "_id" : "$recentPlays.quiz", "total" : { "$sum" : 1 } } }
]).pretty();