I deployed atlas locally using this docker-compose:
version: "3.9"
name: test-database
services:
database:
image: mongodb/atlas
privileged: true
command: |
/bin/bash -c "atlas deployments setup --type local --port 27017 --bindIpAll --username admin --password admin --force && tail -f /dev/null"
volumes:
- /var/run/docker.sock:/var/run/docker.sock
ports:
- "27017:27017"
I then ran the following snippet to create a search index, insert some data and then query that data using a search index.
const {MongoClient, ObjectId} = require("mongodb");
async function testSearchIndexes() {
const mongo = new MongoClient("<URL>");
await mongo.connect();
const db = mongo.db("test-database");
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"
}]);
await new Promise((resolve) => setTimeout(resolve, 10000));
const result = await testColl.aggregate([
{
$search: {
text: {
query: "Test",
path: {
wildcard: '*',
},
},
}
}
]).toArray();
console.log(result);
await mongo.close();
}
testSearchIndexes().catch(console.error);
However, this returns 0 documents. When I run the same aggregate on compass afterwards I get 3 documents as expected and I also get these 3 documents when rerunning the snippet. This is relevant to us, as this behaviour makes it very hard to run our E2E Tests.