Speak now
Please Wait Image Converting Into Text...
Embark on a journey of knowledge! Take the quiz and earn valuable credits.
Challenge yourself and boost your learning! Start the quiz now to earn credits.
Unlock your potential! Begin the quiz, answer questions, and accumulate credits along the way.
General Tech Bugs & Fixes 2 years ago
Posted on 16 Aug 2022, this text provides information on Bugs & Fixes related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.
Turn Your Knowledge into Earnings.
I am new to mongo and trying to perform aggregation query to calculate min/max of timestamps for a given document.
Sample documents are below -
{ "_id" : ObjectId("5c9cd93adddca9ebb2b3fcba"), "frequency" : 5, "s_id" : "30081993", "timestamp" : NumberLong(1546300800000), "date" : ISODate("2019-01-01T00:00:00.000Z"), "values" : { "1547439900000" : { "number_of_values" : 3, "min_value" : 32.13, "max_value" : 81.42 }, "1547440200000" : { "number_of_values" : 3, "min_value" : 48.08, "max_value" : 84.52 }, "1547440500000" : { "number_of_values" : 2, "min_value" : 27.39, "max_value" : 94.64 } } }
{ "_id" : ObjectId("5c9cd851dddca9ebb2b3f2ac"), "frequency" : 5, "s_id" : "27061995", "timestamp" : NumberLong(1546300800000), "date" : ISODate("2019-01-01T00:00:00.000Z"), "values" : { "1547539900000" : { "number_of_values" : 31, "min_value" : 322.13, "max_value" : 831.42 }, "1547540200000" : { "number_of_values" : 3, "min_value" : 418.08, "max_value" : 8114.52 }, "1547740500000" : { "number_of_values" : 2, "min_value" : 207.39, "max_value" : 940.64 } } }
I have come up with the following query which works for a single document.
db.testdb.aggregate([ { $match: { "s_id": "30081993", "frequency": 5, } }, { $project: { _id: 1, valuesarray: { $objectToArray: "$values" } } }, { $unwind: "$valuesarray" }, { $group: { "_id": "", "min_timestamp": { $min: "$valuesarray.k" }, "max_timestamp": { $max: "$valuesarray.k" } } } REPLY 0 views 0 likes 0 shares Facebook Twitter Linked In WhatsApp
Yes, only small changes are required to make this work for multiple documents.
In $match stage, specify your $in query:
$match
$in
$match: { "s_id": { $in : [ "30081993", "27061995" ] }, "frequency": 5, }
In $project stage, rename s_id to _id, to ensure we keep the s_id associated with each document:
$project
s_id
_id
$project: { _id: "$s_id", valuesarray: { $objectToArray: "$values" } }
In $group stage, group by _id (originally s_id), to ensure we correctly group the timestamps together before calculating $min/$max:
$group
$min
$max
$group: { "_id": "$_id", "min_timestamp": { $min: "$valuesarray.k" }, "max_timestamp": { $max: "$valuesarray.k" } }
Whole pipeline:
db.testdb.aggregate([ { $match: { "s_id": { $in : [ "30081993", "27061995" ] }, "frequency": 5, } }, { $project: { _id: "$s_id", valuesarray: { $objectToArray: "$values" } } }, { $unwind: "$valuesarray" }, { $group: { "_id": "$_id", "min_timestamp": { $min: "$valuesarray.k" }, "max_timestamp": { $max: "$valuesarray.k" } } } ]);
No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.
General Tech 9 Answers
General Tech 7 Answers
General Tech 3 Answers
General Tech 2 Answers
Ready to take your education and career to the next level? Register today and join our growing community of learners and professionals.