MongoDB
Install
| npm install @testcontainers/mongodb --save-dev
|
MongoDBContainer
These examples use the following libraries:
Choose an image from the container registry and substitute IMAGE.
Execute a query
1
2
3
4
5
6
7
8
9
10
11
12 | await using container = await new MongoDBContainer(image).start();
const db = mongoose.createConnection(container.getConnectionString(), { directConnection: true });
const obj = { value: 1 };
const collection = db.collection("test");
await collection.insertOne(obj);
const result = await collection.findOne({ value: 1 });
expect(result).toEqual(obj);
await db.close();
|
With credentials
| await using container = await new MongoDBContainer(IMAGE)
.withUsername("customUsername")
.withPassword("customPassword")
.start();
|
MongoDBAtlasLocalContainer
The MongoDB Atlas Local image combines the MongoDB database engine with MongoT for Atlas Search.
Choose an image from the container registry and substitute IMAGE.
The connection string returned by getConnectionString() does not include directConnection=true; pass directConnection: true to your MongoDB client options instead.
Get a connection string
1
2
3
4
5
6
7
8
9
10
11
12 | await using container = await new MongoDBAtlasLocalContainer(IMAGE).start();
⋯
await using container = await new MongoDBAtlasLocalContainer(IMAGE).start();
⋯
await using container = await new MongoDBAtlasLocalContainer(IMAGE)
.withUsername("customUsername")
.withPassword("customPassword")
.start();
|
Get a database connection string
| await using container = await new MongoDBAtlasLocalContainer(IMAGE).start();
|
Connect with credentials
| await using container = await new MongoDBAtlasLocalContainer(IMAGE)
.withUsername("customUsername")
.withPassword("customPassword")
.start();
|
Create an Atlas Search index and search it
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58 | await using atlasLocalContainer = await new MongoDBAtlasLocalContainer(IMAGE).start();
const db = mongoose.createConnection(atlasLocalContainer.getConnectionString(), {
dbName: "test",
directConnection: true,
});
try {
const collection = db.collection("test");
await db.createCollection("test");
await collection.createSearchIndex({
name: "AtlasSearchIndex",
definition: ATLAS_SEARCH_INDEX,
});
await new IntervalRetry<string | undefined, Error>(10).retryUntil(
async () => {
const searchIndexes = await collection.listSearchIndexes("AtlasSearchIndex").toArray();
return (searchIndexes[0] as { status?: string } | undefined)?.status;
},
(status) => status?.toUpperCase() === "READY",
() => new Error("Atlas Search index did not become ready in time"),
5_000
);
await collection.insertOne({ test: "tests", test2: 123, test3: true });
const found = await new IntervalRetry<Record<string, unknown> | null, Error>(10).retryUntil(
async () =>
collection
.aggregate([
{
$search: {
index: "AtlasSearchIndex",
text: {
query: "test",
path: "test",
fuzzy: {},
},
},
},
])
.next(),
(result) => result !== null,
() => new Error("Atlas Search did not return a result in time"),
5_000
);
if (found instanceof Error) {
throw found;
}
expect(found).toMatchObject({ test: "tests", test2: 123, test3: true });
} finally {
await db.close();
}
|