Skip to content

Instantly share code, notes, and snippets.

@mmarcon
Created March 26, 2024 10:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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:s3cr3t@127.0.0.1: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"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment