{"id":14956815,"url":"https://github.com/benweissmann/memongo","last_synced_at":"2025-10-17T01:55:56.683Z","repository":{"id":36069978,"uuid":"192843286","full_name":"benweissmann/memongo","owner":"benweissmann","description":"Easily run an in-memory MongoDB server for your Go unit tests","archived":false,"fork":false,"pushed_at":"2024-04-11T17:00:58.000Z","size":62,"stargazers_count":79,"open_issues_count":6,"forks_count":13,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-01-29T10:21:15.286Z","etag":null,"topics":["golang","mocking","mongo","mongodb","unit-testing"],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/benweissmann.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-06-20T03:37:53.000Z","updated_at":"2024-12-16T12:16:56.000Z","dependencies_parsed_at":"2024-06-18T15:36:07.807Z","dependency_job_id":"16c2f6ad-04ab-4ec9-88ca-ef2e9b5dc940","html_url":"https://github.com/benweissmann/memongo","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/benweissmann%2Fmemongo","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/benweissmann%2Fmemongo/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/benweissmann%2Fmemongo/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/benweissmann%2Fmemongo/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/benweissmann","download_url":"https://codeload.github.com/benweissmann/memongo/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":237950886,"owners_count":19392667,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["golang","mocking","mongo","mongodb","unit-testing"],"created_at":"2024-09-24T13:13:34.779Z","updated_at":"2025-10-17T01:55:51.642Z","avatar_url":"https://github.com/benweissmann.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# memongo\n\n[![CI](https://github.com/benweissmann/memongo/workflows/Go/badge.svg)](https://github.com/benweissmann/memongo/actions?workflow=Go) [![GoDoc](https://godoc.org/github.com/benweissmann/memongo?status.svg)](https://godoc.org/github.com/benweissmann/memongo) [![Go Report Card](https://goreportcard.com/badge/github.com/benweissmann/memongo)](https://goreportcard.com/report/github.com/benweissmann/memongo)\n\n`memongo` is a Go package that spins up a real MongoDB server, backed by in-memory\nstorage, for use in testing and mocking during development. It's based on\n[mongodb-memory-server](https://github.com/nodkz/mongodb-memory-server) for\nNodeJS.\n\nIn general, it's better to mock out interaction with the database, so you don't\nneed to run a Mongo server during testing. But becuase most Mongo clients use\na fluent interface that's tough to mock, and sometimes you need to test the\nqueries themselves, it's often helpful to be able to spin up a Mongo server\nquickly and easily. That's where `memongo` comes in!\n\n# Project Status\n\nBeta. Tests and CI are set up and working, but more esoteric configurations may not work. If Memongo isn't working on your platform, you might want to use `memongo.StartWithOptions()` and pass the correct download URL for your platform manually.\n\n# Caveats and Notes\n\nCurrently, `memongo` only supports UNIX systems. CI will run on MacOS, Ubuntu Xenial, Ubuntu Trusty, and Ubuntu Precise. Other flavors of Linux may or may not work.\n\n# Basic Usage\n\nSpin up a server for a single test:\n\n```go\nfunc TestSomething(t *testing.T) {\n  mongoServer, err := memongo.Start(\"4.0.5\")\n  if (err != nil) {\n    t.Fatal(err)\n  }\n  defer mongoServer.Stop()\n\n  connectAndDoStuff(mongoServer.URI(), memongo.RandomDatabase())\n}\n```\n\nSpin up a server, shared between tests:\n\n```go\nvar mongoServer memongo.Server;\n\nfunc TestMain(m *testing.M) {\n  mongoServer, err = memongo.Start(\"4.0.5\")\n  if (err != nil) {\n    log.Fatal(err)\n  }\n  defer mongoServer.Stop()\n\n  os.Exit(m.Run())\n}\n\nfunc TestSomething(t *testing.T) {\n  connectAndDoStuff(mongoServer.URI(), memongo.RandomDatabase())\n}\n```\n\n# How it works\n\nBehind the scenes, when you run `Start()`, a few things are happening:\n\n1. If you specified a MongoDB version number (rather than a URL or binary path),\n   `memongo` detects your operating system and platform to determine the\n   download URL for the right MongoDB binary.\n\n2. If you specified a MongoDB version number or download URL, `memongo`\n   downloads MongoDB to a cache location. For future runs, `memongo` will just\n   use the copy from the cache. You only need to be connected to the internet\n   the first time you run `Start()` for a particular MongoDB version.\n\n3. `memongo` starts a process running the downloaded `mongod` binary. It uses\n   the `ephemeralForTest` storage engine, a temporary directory for a `dbpath`,\n   and a random free port number.\n\n4. `memongo` also starts up a \"watcher\" process. This process is a simple\n   portable shell script that kills the `mongod` process when the current\n   process exits. This ensures that we don't leave behind `mongod` processes,\n   even if your tests exit uncleanly or you don't call `Stop()`.\n\n# Configuration\n\nThe behavior of `memongo` can be controlled by using\n`memongo.StartWithOptions` instead of `memongo.Start`. See\n[the godoc](https://godoc.org/github.com/benweissmann/memongo) for all the options. Many options can also be set via environment variable.\n\nA few common use-cases are covered here:\n\nNote that you must use MongoDB version 3.2 or greater, because the `ephemeralForTest` storage engine was not present before 3.2.\n\n## Set the cache path\n\n`memongo` downloads a pre-compiled binary of MongoDB from https://www.mongodb.org and caches it on your local system. This path is set by (in order of preference):\n\n- The `CachePath` passed to `memongo.StartWithOptions`\n- The environment variable `MEMONGO_CACHE_PATH`\n- If `XDG_CACHE_HOME` is set, `$XDG_CACHE_HOME/memongo`\n- `~/.cache/memongo` on Linux, or `~/Library/Caches/memongo` on MacOS\n\n## Override download URL\n\nBy default, `memongo` tries to detect the platform you're running on and download an official MongoDB release for it. If `memongo` doesn't yet support your platform, of you'd like to use a custom version of MongoDB, you can pass `DownloadURL` to `memongo.StartWithOptions` or set the environment variable `MEMONGO_DOWNLOAD_URL`.\n\n`memongo`'s caching will still work with custom download URLs.\n\n## Use a custom MongoDB binary\n\nIf you'd like to bypass `memongo`'s download beahvior entirely, you can pass `MongodBin` to `memongo.StartWithOptions`, or set the environment variable `MEMONGO_MONGOD_BIN` to the path to a `mongod` binary. `memongo` will use this binary instead of downloading one.\n\nIf you're running on a platform that doesn't have an official MongoDB release (such as Alpine), you'll need to use this option.\n\n## Reduce or increase logging\n\nBy default, `memongo` logs at an \"info\" level. You may call `StartWithOptions` with `LogLevel: memongolog.LogLevelWarn` for fewer logs, `LogLevel: memongolog.LogLevelSilent` for no logs, or `LogLevel: memongolog.LogLevelDebug` for verbose logs (including full logs from MongoDB).\n\nBy default, `memongo` logs to stdout. To log somewhere else, specify a `Logger` in `StartWithOptions`.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbenweissmann%2Fmemongo","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbenweissmann%2Fmemongo","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbenweissmann%2Fmemongo/lists"}