Meta: Integrate AI into your MongoDB workflow with Cody. This tutorial helps you automate database tasks, create queries, and manage data more effectively in VSCode.
MongoDB has become a popular choice for developers, offering features like time series data, full-text search, vector search, and aggregation pipelines. Since its introduction in 2007, it has been downloaded over 365 million times, largely due to its open-source development model.
However, working with MongoDB‘s features, such as replica sets and built-in replication, can sometimes be challenging. Advanced querying, building complex aggregations, and managing tasks like indexing or replication through its integrated set of related services can become cumbersome and time-consuming.
That is where Sourcegraph‘s Cody comes in. Cody is an AI assistant designed to streamline development tasks by helping you generate efficient queries, simplify aggregation creation, and manage key operations like indexing and replication.
In this tutorial, you will learn how to integrate AI into your MongoDB project. You will be guided through using Cody to write queries, perform aggregations, and manage database operations step by step.
Prerequisites
To follow along, you will need:
- Visual Studio Code (VSCode): Ensure it is installed as your code editor.
- Basic MongoDB knowledge: Familiarity with MongoDB is needed to follow the tutorial effectively.
- JavaScript knowledge: Basic JavaScript skills are required for scripting and writing uncomplicated queries.
Setting up the environment
To continue with the tutorial, you must install and set up both MongoDB and Cody in your code editor (VSCode).
Installing and setting up MongoDB
Start by installing the MongoDB server. For instructions specific to your operating system, refer to the official MongoDB installation documentation
Note: For this tutorial, install the MongoDB community edition.
Follow the guide that matches your operating system to get MongoDB running. You can also use MongoDB Atlas, the cloud-hosted database service.
Next, install the official MongoDB extension for VS Code to interact with your locally hosted MongoDB server. Search for “MongoDB for VS Code” in the extensions section, or use the MongoDB extension link to get it from the marketplace.
Click Install when you find the extension. After installation, you will need to connect your MongoDB extension to your local MongoDB server.
Connecting your MongoDB server to the extension
To connect your VSCode MongoDB client to the database server, you'll need the host address, port number, and password. For the community edition, your host address is usually localhost, and the port is 27017, provided the MongoDB server is running, and the port is available.
Open the MongoDB extension from the sidebar in VSCode.
Click Add Connection to create a new connection.
Next, click Open form to enter your credentials (host, port, and password if needed).
Click Save & Connect, then update the URI field with your credentials in the format below:
mongodb://<host>:<port>
Your MongoDB setup is now complete.
Next, you will create a playground to run your MongoDB commands. Click Create New Playground.
This will generate a playground-1.mongodb.js
file where you can write and test your commands.
Delete the file content, keeping only use("mongdbVSCodePlaygroundDB")
, which specifies the database for the playground.
Now that you’ve completed the setup for MongoDB let’s proceed to set up Cody in your VSCode environment.
Installing and configuring Cody
Cody supports multiple development environments, including JetBrains IntelliJ and VSCode. To install it in VSCode, open the VSCode Marketplace and search for “Cody AI” in the extensions search bar. Locate the Cody extension as shown below:
Once located, select Install to add Cody to your VSCode setup.
If you do not have an account, create one to access Cody.
Follow the on-screen instructions to sign in and start coding with Cody.
Writing MongoDB operations, queries, and aggregations
While MongoDB offers a flexible database language, writing queries manually can often lead to errors and take up a significant amount of time, particularly for complex operations. Let’s look at how you can perform the following operations:
- InsertMany
- Find
- Aggregations
InsertMany Operation
For example, suppose you have a collection called students
and need to insert around 30 records from a JSON or CSV file.
Manual approach: You would need to format all the records as an array of objects, ensuring that each entry matches the fields in the collection. This might look something like this:
const data = [
{
firstName: "Mike",
lastName: "Owen",
grade: 2,
houseNumber: 5,
nextOfKin: "Ben"
// additional fields...
},
{
firstName: "Patrick",
lastName: "Elijah",
grade: 1,
houseNumber: 5,
nextOfKin: "Ben"
// additional fields...
}
// more records...
];
db.students.insertMany(data);
Manually entering data in this format can be tedious and error-prone, especially when dealing with large datasets.
Cody’s approach: With Cody, streamlining this process is simple. Copy the data from your file, then press Opt + K (or Alt + K) or right-click, select Cody, and choose Edit code to open the Edit Code interface.
Next, type in the following instructions:
Write a MongoDB query to insert `<paste your data here>` into the student's collection.
Here is a video on how you can perform the insertMany using Cody.
Cody will generate the query for you. Simply click Accept to confirm the generated code.
You can then run the code by clicking on the play icon in the playground.
Find Query
Let’s say you need to retrieve all students with a GPA above 3.5 who enrolled between March and November. Here is how to do it.
Manual approach: You might use the find
method to handle this query. However, dealing with date ranges in MongoDB can get tricky, especially when you need to match specific months.
Here is an example of what you might come up with:
db.students.find({
gpa: { $gt: 3.5 },
enrolmentDate: {
$gte: new Date("1900-03-01T00:00:00Z"),
$lt: new Date("3000-12-01T00:00:00Z")
}
});
While this query seems to work, it has some potential issues; first, it won't fetch data beyond the year 3000. Additionally, the query retrieves data based on the date rather than specifically targeting the month. If you're not highly experienced with MongoDB, finding the correct function can be challenging.
Cody approach: With Cody, you can open the Edit interface once again, then type:
Write a query to fetch all students with a GPA above 3.5 and an enrolment date between March and November.
Cody generates the query for you, saving time and effort, as demonstrated in the video below.
Similarly, you can do the same for the update, updateMany, and delete operations.
Performing Aggregations
The aggregation pipeline allows you to perform more complex queries and subqueries.
Let's say you need to calculate the average GPA of students who enrolled between March and November and group them by their enrollment year. Aggregation can be complex, especially when you're trying to perform calculations or grouping.
Manual approach: You would have to use the aggregate
method to group and calculate, possibly using multiple stages like $match
, $group
, and $project
. This can be quite challenging if you‘re not familiar with MongoDB's aggregation framework.
Here is an example of what you might come up with:
db.students.aggregate([
{
$match: {
enrollment_date: {
$gte: new Date("1900-03-01T00:00:00Z"),
$lt: new Date("3000-12-01T00:00:00Z")
}
}
},
{
$group: {
_id: { $year: "$enrollment_date" },
averageGPA: { $avg: "$gpa" }
}
},
{
$project: {
_id: 0,
enrolmentYear: "$_id",
averageGPA: 1
}
}
])
While this works, it involves multiple stages and requires some knowledge of the MongoDB aggregation pipeline. Getting it right can be tricky, especially when working with date ranges and grouping data.
Cody’s approach: With Cody, you can just open the Edit interface and type:
Write an aggregation query to group students by the year they enrolled, calculate the average GPA for each group, and only consider students who enrolled between March and November.
Next, Cody generates the aggregation pipeline:
db.students.aggregate([
{
$match: {
$expr: {
$and: [
{ $gte: [{ $month: "$enrollment_date" }, 3] },
{ $lte: [{ $month: "$enrollment_date" }, 11] }
]
}
}
},
{
$group: {
_id: { $year: "$enrollment_date" },
averageGPA: { $avg: "$gpa" }
}
},
{
$project: {
_id: 0,
enrollmentYear: "$_id",
averageGPA: { $round: ["$averageGPA", 2] }
}
},
{
$sort: { enrollmentYear: 1 }
}
])
Here is a video of Cody in action.
Creating Cody’s custom command to help with writing queries and operations
Creating Cody’s custom command allows you to automate tasks related to your MongoDB database interactions, making your workflow more efficient. These commands can be fine-tuned to generate, optimize, or refactor data requests, all while leveraging Cody's context-aware capabilities to ensure they fit seamlessly within your codebase.
Let's create one. First, click the Cody extension icon:
Next, click Manage:
Then, click New Custom Command:
Enter “MongoDBExpert” as the command name.
Select Ask for the command mode, which controls how Cody interacts with the editor (VSCode):
Now, enter the following prompt:
You are a MongoDB expert skilled in writing optimized queries, operations, and aggregation pipelines. You provide practical advice on managing tasks like sharding, replication, and indexing to improve performance and scalability.
The above prompt tells Cody to assume a role as an expert in MongoDB.
Ensure that Selected Code and Current File are both checked so the command has access to the highlighted source code and the current file you are working with:
Choose Workspace Settings to store the command for this workspace:
Your Cody command is ready and can be used within the project's workspace.
Updating operation using the command
Building on our previous example with student
records, imagine you need to update the expected graduation dates for students born in March by adding five years. You need to perform the following actions.
First, click the Cody extension icon and select the MongoDBExpert command:
In the chat box, type:
Write an operation to update the students database so that for every student born in March, add 5 more years to their expected_graduation.
Click the Play icon to execute the instruction:
After that, click Copy, then paste it into your playground to test:
Managing MongoDB operations
Managing MongoDB operations involves various tasks, including indexing and replication. Understanding and effectively utilizing these tasks is crucial for maintaining the performance, availability, and scalability of your MongoDB database.
Indexing
Indexes improve query performance by allowing MongoDB to quickly locate data without scanning every document in a collection. Here's how you can work with indexes.
Manual approach: To create indexes, you must be aware of the data points that affect the speed of querying information. For example, you can create an index with the following command:
db.students.createIndex({ email: 1, enrollment_date: 1 })
While this works, it's not necessarily the most efficient approach.
Cody’s approach: You can leverage Cody to optimize this process. First, activate Cody‘s chat by using Opt + L (or Alt + L). Cody will automatically access the context of your playground file.
Enter the following prompt:
Given that I would mostly be trying to get students by their enrollment_date, name, and probably by when they graduate and their GPA, suggest the best approach to indexing.
After that, Cody will provide an optimized solution and a step-by-step guide to implementing your indexing.
You can then click on Apply to add these changes to your file.
Here is the Code Indexing code generated by Cody.
Here is the final result:
Replication
Replication involves creating copies of your data across multiple servers to ensure redundancy, high availability, and fault tolerance. Here’s how you can perform replication.
Manual approach: To set up replication, you must decide the number of servers based on your available resources and database load. Then, perform the following steps:
- Spin up different instances of MongoDB:
# On server 1:
mongod--auth--replSet "rs0" --bind_ip localhost, server1_ip--port 27017 --dbpath / data / db1
# On server 2:
mongod--auth--replSet "rs0" --bind_ip localhost, server2_ip--port 27017 --dbpath / data / db2
# On server 3:
mongod--auth--replSet "rs0" --bind_ip localhost, server3_ip--port 27017 --dbpath / data / db3
- Connect to one instance and configure the replica set:
rs.initiate({
_id: "rs0",
members: [
{ _id: 0, host: "server1_ip:27017" },
{ _id: 1, host: "server2_ip:27017" },
{ _id: 2, host: "server3_ip:27017" }
]
})
You may encounter issues such as determining the appropriate number of replicas, choosing the best approach for handling read and write within the replica set, and ensuring optimal replication.
Cody's approach: To simplify this process, use a custom command in Cody. Navigate to the custom command section and enter the following prompt:
What is the best approach to setting up replication in MongoDB for a students database that requires high availability and minimal downtime during failovers? Consider a setup that includes at least one primary and two secondary nodes, and explain how to configure read preferences for optimal performance while ensuring data consistency.
Next, Cody will suggest how you can approach replication to improve the availability of your database.
After following Cody's instructions, click Apply to add the code-related section to the file to be executed.
You can even ask Cody to write the commands (in any of your preferred programming languages) that efficiently handle the Read or Write across the replicas.
Overall, using AI tools like Cody is about making your MongoDB operations smoother and more effective—working alongside your expertise to overcome any challenge.
Key takeaways
By integrating AI into your development teams’ workflow, you can further improve your development process and create better solutions faster. It‘s not about replacing your MongoDB skills but amplifying them with the support of AI.
With an AI tool like Cody, you can:
- Adapt quickly to new challenges.
- Write efficient queries and improve operations.
- Enhance your database management skills.
So, by combining your creativity and expertise with AI's vast knowledge, you can build better tools and deliver improved products to your customers.
Ready to take the next step? Sign up for Cody today.
Top comments (0)