Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
18 views3 pages

MongoDB Queries

mongoDB queries for aggregation.

Uploaded by

ayushi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views3 pages

MongoDB Queries

mongoDB queries for aggregation.

Uploaded by

ayushi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 3

[

{
$group: {
_id: null,
totalReplies: { $sum: "$reply_count" }
}
}
]

----------------------------------------------------------------------------------

[
{
$match: {
query_id: ObjectId('67516b65b6ca73eefa8a3f35')
}
},
{
$project: {
start_date: {
$dateToString: {
format: "%Y-%m-%d %H:%M:%S",
date: {
$dateAdd: {
startDate: {
$dateAdd: {
startDate: "$start_date",
unit: "hour",
amount: 5 // Add 5 hours
}
},
unit: "minute",
amount: 30 // Add 30 minutes
}
}
}
},
end_date: {
$dateToString: {
format: "%Y-%m-%d %H:%M:%S",
date: {
$dateAdd: {
startDate: {
$dateAdd: {
startDate: "$end_date",
unit: "hour",
amount: 5 // Add 5 hours
}
},
unit: "minute",
amount: 30 // Add 30 minutes
}
}
}
},
consumer_type: 1
}
}
]

------------------------------------------------------------------
[
{
$group: {
_id: "$encapsulation_marker", // Group by the encapsulation_marker field
count: { $sum: 1 } // Count the number of documents for each encapsulation_marker
}
},
{
$project: {
// Exclude the _id field
count: 1,

}
},
{
$sort: { count: -1 } // Sort by the count in descending order
}
]

-------------------------------------------------------------------------------
[
{
$group: {
_id: "$encapsulation_marker", // Group by the encapsulation_marker field
count: { $sum: 1 } // Count the number of documents for each encapsulation_marker
}
},
{
$project: {
_id: 0, // Exclude the _id field
encapsulation_marker: "$_id", // Rename _id to encapsulation_marker
count: 1 // Include the count field
}
},
{
$sort: { encapsulation_marker: 1 } // Sort alphabetically by the encapsulation_marker field
}
]

------------------------------------------------------------------------------------------------
from pymongo import MongoClient

uri = "mongodb+srv://capabilities_dev:Uii4tTYHqf1D4pjr@capabilities-org-
dev.xz5rt.mongodb.net/"

# Establish the connection using the URI


client = MongoClient(uri)

db = client['tellagence1_66aa62ecd2fd9f2b3da2798a_org']

collections = ['olivia_rodrigo_vampire_66f6fe38804090e8f1df0a95_rac',
'api_rotation_test_6731d875048e19ca17c14035_rac',
'categorization_artist_hate_671686cbb29eb94295a3755a_rac',
'categorization_politics_vgsc_abquue_6706b6e29588a4330dc3f314_rac',
'youtube_core_shorts_h1_67058de42656dcc16fe575af_rac']

for col in collections:


collection = db[col]
cursor = collection.find({}, {"date":1})

data = pd.DataFrame(list(cursor))

data['year'] = data['date'].dt.isocalendar().year
data['month'] = data['date'].dt.month

data["year_month"] = data["year"].astype(str) + "_" + data["month"].apply(lambda x:


f"{x:02d}")

# Perform the group by operation


grouped_data = data.groupby("year_month").size()
name = "monthly" + col

pd.DataFrame(grouped_data).to_csv(f"{name}.csv")

You might also like