Skip to content

Instantly share code, notes, and snippets.

@mmarcon
Created March 26, 2024 10:05
Show Gist options
  • Save mmarcon/5d92025875b1b6cf873a0ca21f5abd19 to your computer and use it in GitHub Desktop.
Save mmarcon/5d92025875b1b6cf873a0ca21f5abd19 to your computer and use it in GitHub Desktop.
MongoDB Atlas Search Local Development

MongoDB Atlas Search local

This setup is loosely based on this except I removed the volumes used for data persistence as they were not needed here.

services:
mongo:
image: mongodb/atlas
privileged: true
volumes:
- ./entrypoint.sh:/entrypoint.sh
entrypoint: ["/entrypoint.sh"]
ports:
- 27017:27017
environment:
- MONGO_INITDB_ROOT_USERNAME=admin
- MONGO_INITDB_ROOT_PASSWORD=s3cr3t
#!/bin/bash
stop_atlas() {
echo "Stopping Atlas deployment..."
atlas deployments stop
}
start_atlas() {
echo "Starting Atlas deployment..."
atlas deployments start
}
trap 'stop_atlas' SIGTERM SIGINT
deployment_status=$(atlas deployments list | grep 'LOCAL')
if [[ -z "$deployment_status" ]]; then
echo "No local deployment found. Setting up..."
atlas deployments setup dev --bindIpAll --username $MONGO_INITDB_ROOT_USERNAME --password $MONGO_INITDB_ROOT_PASSWORD --type local --port 27017 --mdbVersion 7.0 --force
else
if [[ $deployment_status == *"STOPPED"* ]]; then
start_atlas
fi
fi
while true
do
tail -f /dev/null & wait ${!}
done
const { MongoClient, ObjectId } = require("mongodb");
async function testSearchIndexes() {
const mongo = new MongoClient(
"mongodb://admin:[email protected]:27017?directConnection=true"
);
await mongo.connect();
const db = mongo.db("test-database");
await db.createCollection("test-collection");
const testColl = db.collection("test-collection");
await testColl.createSearchIndex({
definition: {
mappings: {
dynamic: true,
},
},
});
await testColl.insertMany([
{
_id: new ObjectId(),
name: "Test value",
},
{
_id: new ObjectId(),
name: "Other value",
},
{
_id: new ObjectId(),
name: "Other value 2",
},
{
_id: new ObjectId(),
name: "Test value 2",
},
{
_id: new ObjectId(),
name: "Test value 3",
},
]);
while ((await testColl.listSearchIndexes().next()).status !== "READY") {
console.log("Waiting for search index to be ready...");
await new Promise((resolve) => setTimeout(resolve, 1000));
}
console.log("Search index is ready!");
const result = await testColl
.aggregate([
{
$search: {
text: {
query: "Test",
path: {
wildcard: "*",
},
},
},
},
])
.toArray();
console.log(result);
await mongo.close();
}
testSearchIndexes().catch(console.error);
{
"name": "localdev-test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
},
"author": "",
"license": "ISC",
"dependencies": {
"mongodb": "^6.5.0"
}
}
@tomas-m90
Copy link

tomas-m90 commented Sep 23, 2024

In my case, it does not run out of the box. Throws

failed to retrieve local deployments with: neither docker or podman were found in your system, check requirements at https://dochub.mongodb.org/core/atlas-cli-deploy-local-reqs
mongo-1  | container engine not found

So, cloning the repo and running docker-compose up does not spin up the atlas deployment in a container.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment