Discover the frequency of dynamically generated data stored in the Mongo Collection

When users add links to the Mongo Collection, I aim to retrieve the most popular ones based on the frequency of a particular link in the collection. For predefined data like the number of links from YouTube, you can use the following:

Meteor.users.find({ "site": "youtube" }).count()

However, how do you handle dynamic data, for example, such as these specific links:

Meteor.users.find({ "url": "youtube.com/watch?v=u1z4vkPWkLQ" }).count()

UPDATE:

How can this be displayed on the client side? When attempting the code below, an error appears in the console indicating that Videos.aggregate is not recognized as a function.

Template.frontPage.helpers({
mostDownloadedVideos: function() {
return Videos.aggregate(
[ { $unwind : "$filename"},
{ $group : { _id : "$filename", number : { $sum : 1} } },
{ $sort : {number : -1} },
{ $limit : 3 }
]
);
}
});

Answer №1

To retrieve total number of joins per month in MongoDB, you can utilize the aggregate function. Here is an example tailored to your scenario:

Meteor.users.aggregate([
    { $project : { popular : { $url : "$url" } } } ,
    { $group : { _id : {url:"$url"} , number : { $sum: 1 } } },
    { $sort : { "_id.number": -1 } }
])

This will allow you to either retrieve the top result or display a list based on popularity.

Answer №2

The meteor framework does not provide aggregate function by default. Nevertheless, you can make use of it by incorporating the package: meteorhacks:aggregate

var data = new Mongo.Collection('data');
var pipeline = [
  {$group: {_id: null, totalSales: {$sum: "$sales"}}}
];
var outcome = data.aggregate(pipeline);

Reference

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Guide on generating a multi-select dropdown list using numbers in JavaScript or React.js

In my current project simulation, I am tasked with choosing an option that corresponds to a number (1, 2, 3, 4, 5, 6....). For example, selecting option number 3 will dynamically generate 3 input elements. ...

Guide on how to perform a POST request within a service worker?

I am faced with the challenge of sending a POST request to the back-end every time a client clicks on a Push notification from the front-end, in order to confirm that the client has received the notification. Here is the system I currently have in place f ...

Accessing a key from an AJAX JSON response and applying it within a .each() loop in jQuery

I'm struggling with a seemingly simple task that I just can't seem to get right. My goal is to convert multiple text inputs into select fields using AJAX and jQuery. Everything works smoothly, except when trying to make the $.each function dynam ...

Issue with the Core php code - In relation to the onclick and onload events

The code below is currently functioning correctly: <a href ="#" onclick="accept ("<?php echo $getservice [$i]['id']; ?>, document.frml)"> Accept </a> In the above code, a Javascript function is being ca ...

Updating a MongoDB Record in NodeJS

I am facing an issue with updating a user document username: "Ralph", email: "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4f3d2e233f270f28222e2623612c28">[email protected]</a>", resetPasswordToken: null, resetPassw ...

Guide on retrieving and presenting images in React from a Cloudinary URL stored in MongoDB

Recently, I started integrating Cloudinary into my application for uploading images. These images are stored in a MongoDB database as URL links. The upload process was successful, and this is how the database looks after saving: id:5f90315331dc1727bc869afe ...

The textures in three.js material do not interact with lighting sources

I am curious about whether a textured geometry can be successfully lit in three.js. In my current scene, I have noticed that all materials receive lighting effectively when using spotLight and directionalLight, except for those that have a texture applied ...

Showing the user's current location on a MapView in React Native

I'm having trouble setting the initial location of a MapView in my React Native app to the device's geolocation. Currently, I'm using a ref to update the coordinates with the help of the geolocation package. However, I'm facing an issu ...

Trapping an iframe refresh event using jQuery

I am trying to dynamically load an iframe using jQuery. Here is my code: jQuery('<iframe id="myFrame" src="iframesrc.php"></iframe>').load(function(){ // do something when loaded }).prependTo("#myDiv"); Now, I need to capture th ...

The rendered output in three.js is failing to appear on the screen

I recently started experimenting with three.js (and don't have much experience with it). I followed a tutorial that instructed a green box to be displayed on the screen. Despite not encountering any errors in the console, all I see is a blank black sc ...

Triggering onClick without interfering with its populated variable

I'd like to add the following code snippet to my document: $('#myDiv).append("<div id='myDiv2' onclick="+extElementConfig.onClickDo+">Do</div>"); The code above uses an object with properties to populate the onClick attrib ...

What is the method for extracting a variable from a URL using JavaScript?

For instance, consider this URL- website.com/page/?var=20 I am looking to extract the "var" variable from the URL using JavaScript. By the way, my actual URL is- https://bipit2.pranaypro21292.repl.co/d21292auth/?code=pimWoHEuV7fgKEVQMTntRnzXqVla3G&gu ...

Error alert: The reference to emailjs is not recognized in the onsubmit function of HTMLFormElement

I'm currently attempting to utilize Emailjs as a client-side script for sending emails. However, I keep encountering the error "Uncaught ReferenceError: emailjs is not defined at HTMLFormElement.onsubmit". Below is the HTML code that is causing the i ...

Guide on displaying gallery images when clicking on a specific class

I am in the process of developing a gallery tab for a website, initially displaying three thumbnails. The goal is to reveal the remaining thumbnails when a class named show-all is clicked. I have successfully animated the gallery's height to show the ...

What is the most effective method for displaying error messages in Extjs?

I am currently using the store.synch() method to post data, with validation being done on the server side. I am currently displaying error messages in a message box, but I want to explore alternative ways to show errors without having to use markInvalid( ...

Managing multiple client requests at the same time with Node and Express

I am faced with the challenge of handling multiple user requests simultaneously in my express server and storing their data in a MongoDB. While I have all the necessary code prepared, I am struggling to devise a method for assigning a unique identifier to ...

Angular Material: Enhanced search input with a universal clear button

After searching for a cross-browser search control with a clear button similar to HTML5, I found the solution rendered by Chrome: <input type="search> The code that gave me the most relevant results can be found here. I used the standard sample w ...

Implementing the Context API with React Router: using version '^5.2.0' of react-router-dom and version '17.0.2' of React

I am currently struggling to retrieve the user value from my Header component using the Context API, but unfortunately it is returning as undefined. Below is my code snippet. import React, { createContext, useEffect, useState } from "react"; exp ...

Error in Webpack 5: Main module not found - Unable to locate './src'

When trying to build only Express and gql server-related files separately using webpack5, an error occurs during the process. ERROR in main Module not found: Error: Can't resolve './src' in '/Users/leedonghee/Dropbox/Project/observe ...

Conceal on External Click with AngularJS

My code looks something like this... I am using the 'left-menu-active' class to toggle the menu in css... I have two issues and I am looking to resolve them using angular js... I want to dynamically add a class to the parent element using angu ...