Yes, MongoDB uses memory to store data for efficient access, but it's important to understand that MongoDB is not purely an in-memory database. It uses a combination of both disk storage and in-memory storage.
MongoDB, like many other databases, uses RAM as a cache for frequently accessed documents and indexes. This functionality is based on the WiredTiger storage engine, which is the default storage engine starting from MongoDB 3.2.
The WiredTiger storage engine attempts to keep as much data (including indexes) in the system's physical RAM to improve read/write performance. However, MongoDB writes all changes out to disk, ensuring that data persists beyond restarts and power failures.
Here is a Python code snippet demonstrating how you could connect to a MongoDB instance using PyMongo:
Remember, to optimize MongoDB's use of RAM, you should design your application so that most frequently accessed data fits in RAM, and fine-tune your indexing strategy so that the majority of your queries can be resolved using the index.