/r/mongodb

Photograph via snooOG

News, articles, and interesting stuff in general about MongoDB (unofficial).

/r/mongodb

21,641 Subscribers

1

Why does Mongodb Atlas send all my Personal Location information including my digital footprint over email?

I am a security researcher and european resident was shocked that they have all this information without my explicit consent. Is this information being shared with others like thier marketing team to spy one me? Who is the responsible person to report to ? I found thier community forums confuisng a and was not sure where to report on their commuity forums

0 Comments
2024/04/07
19:43 UTC

2

Mongodb Timeseries

Hello Everyone, I am creating an eventing system, estimated 1M writes per day. I also want to run aggregate queries for upto 1 year (like MixPanel) Figured out that changing granularity to hours significantly improved the performance of query. Unfortunately mongo only supports granularity in seconds, minutes and hours. I would want to use granularity in days. I’m not sure there’s a way or workaround to do this, please let me know if there’s a way.

Thanks

1 Comment
2024/04/07
14:21 UTC

2

Is anybody planning to take the MongoDB Associate Atlas Administrator Exam?

Mongodb has started a new certification 'MongoDB Associate Atlas Administrator Exam'.

Is anybody planning to take the MongoDB Associate Atlas Administrator Exam?

If anybody has taken the exam, please share your experience.

0 Comments
2024/04/06
21:17 UTC

3

Work life balance at MongoDB

Thinking of applying to the strategy & bizops team at MongoDB, anyone know what the work life balance is like on this team?

0 Comments
2024/04/06
17:43 UTC

1

Problem to use $function aggregate

Hello, guys!

I need a help with the function aggregate of mongoDB. I've the query above:

[
  {
    $project: {
      "product description": {
        $function: {
          body: function (name) {
            return name
              .split("")
              .map(function (char) {
                return ["μ"].includes(char) ? char : char.toUpperCase()
              })
              .join("");
        },
          args: ["$product description"],
          lang: "js",
        },
      },
    },
  },
  {
    $group: {
      _id: {
        "$product description":
          "$product description",
      },
    },
  },
]

On my database, I have product names with special characters like μ. I need to return the product names in uppercase. The function performs very well, but product names with the μ character return with an M.

For example: 'aluminum 25x10 5μ' becomes 'ALUMINUM 25x10 5M' (the function converts μ to M when uppercase is applied).

I used map to identify the use of μ in the string and performed validation to ensure that the character μ is not modified when found in the string.

The code works in JavaScript, however, when I apply it in MongoDB, it doesn't work. It returns the document with the incorrect product name (with M in place of μ).

How can I solve this problem?

Thank you very much in advance

0 Comments
2024/04/04
21:00 UTC

4

MongoDB attribute pattern vs wildcard index

I just read an article about the Mongo attribute pattern

but I also noticed in MongoDB we can have a wild card index

so the attribute pattern can be replaced with the wild card index.

what do you guys think? is there any use case that is only suitable for attribute patterns?

2 Comments
2024/04/04
16:23 UTC

1

Security Practices about communication

What do to make the communication with the MongoDB more security?

Only have username and a complex password is sufficient?

0 Comments
2024/04/04
12:16 UTC

6

How Do Schema migration in mongoose & mongodb ?

I would like to know how to perform schema migration in Mongoose and MongoDB. I've tried searching for resources on the internet, but I couldn't find a clear example. Could you please provide an example code and explain how to do it? Also, if possible, please advise on how to handle schema migration in production environments

6 Comments
2024/04/04
11:29 UTC

4

MongoDB aggregation query with $lookup and $match

**Context:**

I maintain two collections: `Package` and `Module`. Within the `Module` collection, each document contains a field called `packageId`, which corresponds to the `_id` field of a document in the `Package` collection.

Sample document from **Package** collection

{
  "_id": "660dc62edb464b62c8e34b3b",
  "workspaceId": "6606a50d59b56908f026a3ab",
  "packageUUID": "6605b2ee536675159c565857",
  "originPackageId": "6606a51359b56908f026a3b2",
  "version": "0.0.7",
  "latest": true
}

Sample document from **Module** Collection

{
  "_id": "660dc62edb464b62c8e34b3c",
  "packageUUID": "6605b2ee536675159c565857",
  "packageId": "660dc62edb464b62c8e34b3b",
  "version": "0.0.7",
  "type": "QUERY_MODULE",
  "moduleUUID": "6605b324536675159c565869",
  "originModuleId": "6606a51359b56908f026a3bc"
}

**My Use Case:**

When provided with a list of module IDs (i.e., `_ids` in the `Module` collection), I aim to tally the number of packages that meet the following criteria:

Either the `originPackageId` field does not exist.

Or the `latest` field is set to `true`.

**My attempt:**

Here is what I attempted but it always returns `0`

public long countPackages(List<String> moduleIds) {
        AggregationOperation matchModuleIds =
                Aggregation.match(Criteria.where(Module.Fields.id).in(moduleIds));

        LookupOperation lookupOperation = LookupOperation.newLookup()
                .from("package")
                .localField("packageId")
                .foreignField("_id")
                .as("packages");

        AggregationOperation unwindPackages = Aggregation.unwind("$packages"); // tried without `$` as well

        AggregationOperation matchConditions = Aggregation.match(new Criteria()
                .orOperator(
                        Criteria.where("packages.originPackageId").exists(false),
                        Criteria.where("packages.latest").is(true)));

        AggregationOperation groupByNull = Aggregation.group().count().as("total");

        Aggregation aggregation = Aggregation.newAggregation(
                matchModuleIds, lookupOperation, unwindPackages, matchConditions, groupByNull);

        List<Document> results = mongoTemplate
                .aggregate(aggregation, Module.class, Document.class)
                .getMappedResults();

        // Assuming there is only one result
        if (!results.isEmpty()) {
            Document resultDoc = results.get(0);
            return resultDoc.get("total", Long.class);
        } else {
            return 0L;
        }
    }

I appreciate your help in this regard.

4 Comments
2024/04/04
06:09 UTC

2

Can't connect to MongoDB Atlas Cloud

Hello ! I tried...
And I tried...
I pasted the code they gave me :

from pymongo.mongo_client import MongoClient

from pymongo.server_api import ServerApi

uri = "mongodb+srv://mihaiverywise:<password>@dbtest.otwpuwv.mongodb.net/?retryWrites=true&w=majority&appName=DBTest"

# Create a new client and connect to the server

client = MongoClient(uri, server_api=ServerApi('1'))

# Send a ping to confirm a successful connection

try:

client.admin.command('ping')

print("Pinged your deployment. You successfully connected to MongoDB!")

except Exception as e:

print(e)
I put my password there, I just don't want to share it here because it's personal.
I updated the "dnspython".
Tried everything I could from stackoverflow and other forums.
What to do????

2 Comments
2024/04/03
20:41 UTC

5

When to determine when to throw 40x or 50x error codes?

A database query could go wrong in a hundred ways inside a server function which is evident in here: https://www.mongodb.com/docs/manual/reference/error-codes but let's say a document was not found upon document update. How to determine the doc wasn't found and it wasn’t a server error? By number of updated documents and some other result?

And no before someone asks do I want to crash the server upon errors, I obviously try/catch in all server functions.

Edit: now that I think about it I could just throw 404 if the document is null and in other cases throw 500

7 Comments
2024/04/03
20:08 UTC

2

Added TLS to mongodb and cannot connect with mongosh

Hi everybody,

I installed mongodb, created a user, and could connect with the command:

# mongosh "mongodb://localhost:27017/dg" --username dk_user                

Next I created self-signed TLS corticates, updated the /etc/mongod.conf, and successfully started the server again.

However, the command to connect does not work anymore:

# mongosh --tls "mongodb://localhost:27017/dg" --username dk_user  
Enter password:
Current Mongosh Log ID: 660d1ef2444496fd69redacted
Connecting to:          mongodb://localhost:27017/dg?directConnection=true&serverSelectionTimeoutMS=2000
MongoServerSelectionError: self signed certificate
# 

It is an internal server, and a self-signed certificate is acceptable for use where I am.

Does anybody know how to get the self-signed certs working?

//EDIT: I added --tlsAllowInvalidCertificates to the command

e.g

# mongosh --tls "mongodb://localhost:27017/dg" --username dk_user --tlsCertificateKeyFile
1 Comment
2024/04/03
09:29 UTC

1

help please

I am not able to connect my atlas cluster to MongoDB Compass.

things I tried to fix but didn't solved the issue are

  1. fixing firewall
  2. trying with other accounts
  3. making network accessible by any IP and also adding my own IP
  4. tried using vs code extension also but the error was still there- Unable to connect: queryTxt ETIMEOUT

https://preview.redd.it/vnex4ic3b8sc1.png?width=2880&format=png&auto=webp&s=9ba618350c89c5f28dedf06ee91c8b1d633a32e1

7 Comments
2024/04/03
08:55 UTC

3

Connecting a Node.js App to MongoDB: Storing Entries for Logged-In Users

0 Comments
2024/04/03
05:40 UTC

0

Can mongodb fix this?

I have a lot of json files (2.3GB) which contains data from external API. Data from these files is being fetched by Django REST Framework. Can mongodb optimize problem of file size or should i use something else (gzip, etc.)

3 Comments
2024/04/02
12:34 UTC

3

MongoNetworkError?

Is this the response we all get when typed mongosh command or am i the only one getting this?

https://preview.redd.it/ede0ki96p1sc1.png?width=667&format=png&auto=webp&s=1ffd7061b087e2ac9bd337a512dbccb109b901d3

i was trying to connect to a Local Deployment on the Default Port but got this when type:
mongosh

tried this: systemctl status mongod.service

https://preview.redd.it/8n9ft0nfs1sc1.png?width=863&format=png&auto=webp&s=a90d3c7c5299666f23a67e0fcc1dd472c1051075

I am new to backend and ubunbtu (basically everything)... can anyone help out

3 Comments
2024/04/02
10:45 UTC

2

User provisioning for self hosted MongoDB

For those of you that have a self hosted MongoDB running on a virtual machine, what’s the best/easiest way to provisión new users?

1 Comment
2024/04/01
16:55 UTC

2

Noob question about MongoDB architecture to use for my scenario

Hi,

I've been pondering this for days and am uncertain about the best approach, so I'm hoping someone with more experience could offer some guidance.

I have two scenarios to consider:

In the first scenario, I have my application and MongoDB installed on four servers that can all communicate with each other. There is a primary MongoDB instance and three replicas for voting and selecting a new primary MongoDB. I've set up my application with keepalived so that if it fails on Server 1, the virtual IP points to Server 2, where data will continue to be received.

But mongo wont know that my app died, and that app on server2 is now main and that is should write to mongo instance on server 2, and since my mongo on server2 is replice it wont accept write action. That is scenario where only app dies.

The second scenario is where the entire VM dies, then MongoDB dies too, and my replicas know that they need to elect a new primary, and all is well. Server 2 will happily write to its MongoDB since it's voted to be primary.

Then one more thing can happen: my app on server 1 starts working again and it becomes primary, but it can't start writing to its MongoDB since it is now secondary.

In the second scenario, I have two servers hosting my application, each with its own MongoDB instance. Both servers receive slightly different data, and I would like both MongoDB instances to be primary (i.e., writable). Additionally, I'd like them to sync periodically, say every hour. I understand that my current setups may not be ideal, and I'm aware that I need to redesign my approach. However, I'm curious if it's possible to make this work as described.

For the first scenario, is there a way to make the primary MongoDB instance follow my active server? How would you accomplish this, and could you recommend or provide a tutorial on this topic?

Similarly, for the second scenario, is it possible to make both MongoDB instances primary and have them sync periodically? Are there any tutorials you could recommend or provide on this matter? Actually, this would be the best solution for both scenarios: to have all MongoDB instances be primaries and sync from time to time.

Thank you!

4 Comments
2024/04/01
13:13 UTC

2

[HELP] Can I use the stored results of a query for lookup?

Hi, I want to know whether it's possible to store the results of an aggregate operation in a variable, and then use that stored result in another aggregate operation for lookup and match. To make my question clearer, consider the following example:

We have 2 models named users and orders. Let us say that in one function I perform the following:

const response = await userModel.aggregate([
    {
        $match: {
            createdAt: {$gte: new Date(date_var_start), $lt: new Date(date_var_end)}
        }
    },
    {
        $project: {
            someField: 1,
            someField2: 1
        }
    }
])

Now I want to pass the result stored in the response variable to two different functions, each of which will perform a lookup on orders collection on the basis of someField and someField2 respectively. Is it possible to do this? How, if yes?

2 Comments
2024/04/01
07:42 UTC

2

Automatically deleting docs

I am building a task management app with Express.js and Mongoose that has a lot referencing, the structure is like this:

each user can have multiple workspaces and each workspace can have multiple members and admins, each workspace can have multiple boards, and each board can have multiple tasks. each task can have multiple users. now for example if a user is deleted is there a way to automatically delete the reference to this user from all docs ?

I hope I was able to explain it clearly 😅
Thank you.

5 Comments
2024/03/31
22:57 UTC

3

Renaming/Duplicating DB in Atlas

This could be a really dumb question but I have been trying to find an answer for this and have not found anything definitive specifically for Atlas and for the use case I need.

Basically, I have an org, project and cluster set up in Atlas and built out a web app as sort of a learning project that does a few things but the most relevant is that people can sign up and create accounts. Now I am working on a mobile app that is using the same database because I would like to have a universal account system for any apps I make (you make an account on one app, you can sign in with the same account on another). I have some users who have signed up on the web app.

Basically what I didn’t realize is that I am still using the default “test” db that is created when you first make the cluster and I don’t think that is ideal for the sake of keeping things as professional as I can.

So what I would like to do in an ideal world is simply rename the DB. I understand that is likely not possible especially on Atlas and there are workarounds like copying the documents from a collection over at a time, or dumping and restoring to the new db, live migration, using $out for each collection but am unsure about which method is actually feasible for me to use. I am unsure because some of these methods are for self hosted servers, or commands that are restricted from free cluster users, or require reindexing of all the collections afterwards, or require downloading the data locally and then reuploading which would require writing scripts which I would rather not spend a lot of time on.

Is there a simpler solution that I am missing to simply duplicate a db in atlas? If not, what route would be best for me to use? Thanks for any help/suggestions in advance.

2 Comments
2024/03/30
08:15 UTC

3

how to get size of collections quickly ?

Hi,

I have 10k databases in my Replica Set and each database has up to 10k collections. How I can get size of all collections ? I have 2 ideas - mongodriver and mongoshell - $collStats and db.collection.totalSize(). But both approaches quite similar in terms of performance. I have to get size of collections one by one. Is there any way how I can get collections size in batch ? e.g. get size of all collections within database/replica set within single query ? The most important thing to me is performance - I need my query to work fast and obviously getting size of collections one by one is very slow.

Could you please assist ?

5 Comments
2024/03/28
17:06 UTC

2

Learn MongoDb Aggregation 2024 #mongodb #database #coding #100secondsofcode

🚀 Hey everyone! Check out this quick YouTube short where I provide a handy MongoDB pipeline example: [MongoDB Pipeline Example](https://youtube.com/shorts/ukAz94V3x\_s?si=6v13SYZ5LTiyPTDk) 🔍 If you're keen to dive deeper, don't forget to explore my full playlist covering MongoDB aggregation techniques! #MongoDB #Database #DataProcessing #DeveloperTips #CodeTutorial

0 Comments
2024/03/28
15:28 UTC

2

Issue with test.insertone() buffering timed out after 10000ms - tried lots of different options

So after trying a lot of different options, await and then/catch, double checking my .env and my connection on MongoDB, I still get the time out. I even tried to increase the timeout within the MongoClient but that did not work either.. It seems the connection is not the issue because I do get the message "You successfully connected to MongoDB!"

Can someone help out? Any advice is highly appreciated. Thank you!

Here is the server.js file:

const express = require('express'); const mongoose = require('mongoose'); const jwt = require('jsonwebtoken'); require('dotenv').config({ path: '../.env'});

// Async function to establish database connection and start the server const { MongoClient, ServerApiVersion } = require('mongodb'); const uri = "mongodb+srv://XXXXXXXXX" // Create a MongoClient with a MongoClientOptions object to set the Stable API version const client = new MongoClient(uri, { serverApi: { version: ServerApiVersion.v1, strict: true, deprecationErrors: true, } });

async function run() { try { // Connect the client to the server (optional starting in v4.7) await client.connect(); // Send a ping to confirm a successful connection await client.db("admin").command({ ping: 1 }); console.log("You successfully connected to MongoDB!"); } finally { // Ensures that the client will close when you finish/error await client.close(); } } run().catch(console.dir);

// Express app initialization const app = express(); app.use(express.json()); // Middleware to parse JSON bodies

const TestModel = require('../models/TestModel'); // Simplified test route to check MongoDB connection and test model functionality app.get('/test-db', async (req, res) => { try { // Use the TestModel to create a new document const testDoc = new TestModel({ message: 'Hello, MongoDB!' }); await testDoc.save();

// Then retrieve all documents using TestModel const results = await TestModel.find(); res.json(results); } catch (error) { console.error('Error interacting with MongoDB:', error.message); res.status(500).send(error.message); } });

// Start the server const PORT = process.env.PORT || 3000; app.listen(PORT, () => { console.log(Server is running on port ${PORT}); });

testmodel.js file
const mongoose = require('mongoose');

const testSchema = new mongoose.Schema({ message: String });

const TestModel = mongoose.model('Test', testSchema);

module.exports = TestModel;

2 Comments
2024/03/27
21:05 UTC

0

Trying to download Json data from a URL

I was using a json downloader chrome extension however I am assuming since the webpage is too large it is not working? Is there a way to use mongodb to import the data from the url?

here is the webpage for the date

https://github.com/ozlerhakan/mongodb-json-files/blob/master/datasets/companies.json

2 Comments
2024/03/26
22:15 UTC

7

MongoDB Database Administrator - 100% remote (from anywhere in Texas)

The State of Texas IT Public Health is looking for an experienced Mongo DB Administrator. I am the Director who manages that team.

Salary range is currently up to $164K, plus good benefits (medical premiums are paid 100% by the State and lots of time off). The State has authorized one 5% pay increase, for current employees as of September 1, 2024. This position is also eligible for a performance bonus.

This is a new position supporting a new custom application newly deployed in production (EMS Trauma registry - one of the largest in the world).

The position in a nutshell: The Database Administrator V performs highly advanced Mongo DB database administration, maintains and configures Mongo DB instances, comprehends and translates business requirements into technical specifications and builds elegant and efficient and scalable solution based on specifications. The candidate implements MongoDB management service for automating a variety of tasks, including backup and recovery and performance management and has data migration skills to migrate data from a relational database to MongoDB. The candidate provides high level oversight and direction where databases, and database infrastructure are concerned. The work involves planning and scheduling as well as the defining, developing and maintaining database system environments for agency application areas. Works under general direction with minimal supervision.

Please apply here if interested: https://jobshrportal.hhsc.state.tx.us/ENG/careerportal/Job_Profile.cfm?szOrderID=601611&szReturnToSearch=1&&szWordsToHighlight=mongo

Please DM if you have questions.

Edit to correct link and salary information

3 Comments
2024/03/26
21:44 UTC

0

Not being able to connect to the server. Error Message Displaying. How to Resolve?

Connection failed.

SERVER [localhost:27017] (Type: UNKNOWN)

|_/ Connection error (MongoSocketOpenException): Exception opening socket

|____/ Socket error: Connection refused

Details:

Timed out after 5000 ms while waiting to connect. Client view of cluster state is {type=UNKNOWN, servers=[{address=localhost:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.ConnectException: Connection refused}}]

4 Comments
2024/03/26
16:24 UTC

1

Atlas Backup Version problems

Hello, I exported a snapshot from our Atlas cluster and now I'm having issues running it locally.

The cluster is running Mongodb version 5.0.25, so I tried starting the backup with:

docker run -it -p 27017:27017 -v \~/Downloads/restore-6602c4ddc0286d1265216c1f:/data/db mongo:5.0.25

Got the error: "This version of MongoDB is too recent to start up on the existing data files. Try MongoDB 4.2 or earlier."

I then tried, as suggested in the error:

docker run -it -p 27017:27017 -v \~/Downloads/restore-6602c4ddc0286d1265216c1f:/data/db mongo:4.2.0

but I get the error, "unsupported WiredTiger file version: this build only supports versions up to 4, and the file is version 5"

Can anyone make sense of this?

1 Comment
2024/03/26
15:21 UTC

Back To Top