Determine the number of documents in MongoDB that meet a specific pattern using

In my database, I have records that contain various URLs, such as

https://www.youtube.com/watch?v=blablabla
.

My goal is to tally the number of URLs for each individual site. For example:

[{
    site: 'youtube.com',
    count: 25
},
{
    site: 'facebook.com',
    count: 135
}]

To achieve this, I attempted to use the following aggregation pipeline:

db.getCollection('records').aggregate([
    {'$match': {'url': /.*youtube\.com.*/}}, // using youtube as an example
    {'$group': {'_id': {'site': '$url', 'count': {'$sum': 1}}}},
    {'$project': {'_id': false, 'site': '$_id.site', 'count': '$_id.count'}}
]);

The result of this pipeline is:

[{
    "site" : "youtube.com/blablabla1",
    "count" : 1.0
},
{
    "site" : "youtube.com",
    "count" : 1.0
},
{
    "site" : "www.youtube.com/blablabla2",
    "count" : 1.0
},
{
    "site" : "www.youtube.com/blablabla1",
    "count" : 1.0
}]

However, this method is not able to accurately count identical strings.

What could be the issue with my current approach?

Answer №1

This code snippet will calculate the total number of websites:

The name of the website is identified by the following regular expression:

const testData = ['https://www.youtube.com/watch?v=UbQgXeY_zi4&list=RDUbQgXeY_zi4&index=1', 'https://www.facebook.com/maciej.kozieja.9', 'http://example.com', 'http://www.example.com']

const websites = testData.map(site => (site + '/').match(/(?:https?:\/\/)?(?:www\.)?([\w.]+)(?=\/)/)[1])

console.log(websites)

Next, we need to apply the mapReduce function to our collection:

db.collection('links').mapReduce(
    function () {
        emit((this.site + '/').match(/(?:https?:\/\/)?(?:www\.)?([\w.]+)(?=\/)/)[1], 1)
    },
    function (key, values) {
        return values.length
    }, { out: 'websiteLinksCount' }
)

After that, we can perform further operations on the data:

.then(result => {
    result.find({}).toArray((error, result) => {
        console.log(result) // This will give you an array of objects like [{_id: siteName, value: count}]
    })
})

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

Having trouble getting Ajax post to function properly when using Contenteditable

After successfully implementing a <textarea> that can post content to the database without needing a button or page refresh, I decided to switch to an editable <div> for design reasons. I added the attribute contenteditable="true", but encounte ...

What could be causing my MongoDB Node.js driver's $lookup aggregation query to not produce the expected results?

In my database, I have two collections: users and roles Each user has an array of roles assigned to them: { _id : 61582a79fd033339c73ee290 roles:[615825966bc7d715021ce8fc, 615825966bc7d715021ce8fd] } The roles are defined as follows: [ { ...

A guide on converting a Javascript datetime to a C# DateTime parameter

In a previous question that I asked on StackOverflow, I mentioned that I was able to retrieve my values successfully as the object was no longer null. The object contains a DateTime property named CreatedOn, which I am sending from JavaScript using new Dat ...

Rendering content conditionally using react technology

There is a functional component that receives two items as props. The properties can have values like `undefined`, `""`, `null`, `"null"`, or a valid string (e.g. `"test"`). The goal is to conditionally render these props based on their values. If both ` ...

Video player for Angular 2

Hey there! I am currently exploring Angular 2 and attempting to create a video playlist feature. My goal is to showcase my favorite videos in a table layout, where clicking on a row will lead to the playback of the selected video. Right now, I am passing t ...

Leveraging Angular 2 to retrieve information from mongoDB

I recently finished setting up my nodejs project which includes a database and some data. The database was created using the following URL: mongodb://localhost:27017/ Check out the code snippet below: var MongoClient = require('mongodb').MongoC ...

How can I transfer a variable from one webpage to another within the MEAN stack?

This is the Angular View Code: <button class="btn btn-primary" ng-click="edit(obj._id)">Edit</button> Here is the Angular Controller code: $scope.edit=function(id) { console.log('edit() called........'); console.log(id); ...

What could be causing a momentary 404 error when I click on a next.js `Link` that connects to an API endpoint before redirecting to the intended page?

Hello there, I recently followed a tutorial on adding authentication with Passport to Next.js from this resource: https://auth0.com/blog/next-js-authentication-tutorial/ However, I encountered a minor issue. When I click the "/login" Link in my Next.js ...

Remove checked rows in a table with a single click

I have created a table with a list and checkboxes next to each element. There is also a Delete button that I want to connect to the delete operation. Here is the code for the Delete button: <tr id="deleteproject" > <td wi ...

Troubleshooting the issue with dynamically adding form fields in AngularJS

I am currently working on an online course application and facing an issue with adding form fields dynamically to include additional video lectures for each course. When I attempt to click on the "Add Another URL" button, nothing happens. https://i.sstatic ...

Tips for repairing damaged HTML in React employ are:- Identify the issues

I've encountered a situation where I have HTML stored as a string. After subsetting the code, I end up with something like this: <div>loremlalal..<p>dsdM</p> - that's all How can I efficiently parse this HTML to get the correct ...

Can you provide guidance on decompressing SVGZ files using JavaScript?

I'm currently working on implementing a high-resolution world map using SVGZ instead of the standard SVG format, aiming to provide my users with a more intricate and detailed visualization. Although I have experimented with utilizing js-deflate to de ...

Looping through a series of URLs in AngularJS and performing an $

Currently, I am facing an issue while using angular.js with a C# web service. My requirement is to increment ng-repeat item by item in order to display updated data to the user. To achieve this, I attempted to utilize $http.get within a loop to refresh t ...

Limiting client requests on the Azure Translation API

I have a client who needs to limit the number of requests made to my Azure Translation API service. I found information from Microsoft on how to implement request throttling, but it's unclear where exactly in the request this throttling data should be ...

Troubleshooting: Browser fails to update after CSSStyleRule modification using JavaScript

When making changes to CSS properties of elements using JS methods like CSSStyleSheet, insertRule, deleteRule, or CSSStyleRule.style.setProperty(), I noticed that the underlying CSS is updated but the page does not reflect these changes immediately. The c ...

The functionality of Jquery datatables seems to be faulty when navigating to the second page

While utilizing the jQuery datatables plugin, I encountered an issue where the event click function only worked on the first page and not on subsequent pages. To address this problem, I discovered a helpful resource at https://datatables.net/faqs/ Q. My ...

Troubleshooting a unique CSS bug in jQuery mouseover functionality

Check out this pen: https://codepen.io/anon/pen/eKzEVX?editors=1111 I recently created a Form Select in Laravel: {!! Form::select('status_id', $statuses, $post->status_id, ['class' => 'form-control post-sub-items-label &apo ...

Testing the local transmission of form data via Javascript: A Step-by-Step guide

Currently studying how to send forms using JavaScript by manually creating an XMLHttpRequest. Towards the end of the provided example, there's a note: Note: If you want to send data to a third party website, keep in mind that this use of XMLHttpRequ ...

Exploring the documentation of node.js with doxygen

When it comes to my C projects, I make sure to document them using Doxygen. Recently, I delved into the world of NodeJs and attempted to document .js files with Doxygen, but unfortunately, no output was generated. Despite my efforts to search for answers ...

The test suite encountered an error (EBUSY: resource busy or locked) and unfortunately failed to run at Nx version 14.5.10 and Jest version 27.5.1. It seems there was an

I have recently upgraded my NX Monorepo from version 13 to 14, and everything seems to be working fine except for the Jest migration. I keep encountering this error even after trying various solutions like clearing the cache, deleting node_modules, and rei ...