This setup is loosely based on this except I removed the volumes used for data persistence as they were not needed here.
Created
March 26, 2024 10:05
-
-
Save mmarcon/5d92025875b1b6cf873a0ca21f5abd19 to your computer and use it in GitHub Desktop.
MongoDB Atlas Search Local Development
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "localdev-test", | |
"version": "1.0.0", | |
"description": "", | |
"main": "index.js", | |
"scripts": { | |
}, | |
"author": "", | |
"license": "ISC", | |
"dependencies": { | |
"mongodb": "^6.5.0" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In my case, it does not run out of the box. Throws
So, cloning the repo and running docker-compose up does not spin up the atlas deployment in a container.