Skip to main content

MongoDB

MongoDB is a NoSQL , document-oriented database that supports JSON-like documents with a dynamic schema.

Overview​

The MongoDB Document Loader returns a list of Langchain Documents from a MongoDB database.

The Loader requires the following parameters:

  • MongoDB connection string
  • MongoDB database name
  • MongoDB collection name
  • (Optional) Content Filter dictionary

The output takes the following format:

  • pageContent= Mongo Document
  • metadata={β€˜database’: β€˜[database_name]’, β€˜collection’: β€˜[collection_name]’}

Load the Document Loader​

# add this import for running in jupyter notebook
import nest_asyncio

nest_asyncio.apply()
from langchain.document_loaders.mongodb import MongodbLoader
loader = MongodbLoader(
connection_string="mongodb://localhost:27017/",
db_name="sample_restaurants",
collection_name="restaurants",
filter_criteria={"borough": "Bronx", "cuisine": "Bakery"},
)
docs = loader.load()

len(docs)
25359
docs[0]
Document(page_content="{'_id': ObjectId('5eb3d668b31de5d588f4292a'), 'address': {'building': '2780', 'coord': [-73.98241999999999, 40.579505], 'street': 'Stillwell Avenue', 'zipcode': '11224'}, 'borough': 'Brooklyn', 'cuisine': 'American', 'grades': [{'date': datetime.datetime(2014, 6, 10, 0, 0), 'grade': 'A', 'score': 5}, {'date': datetime.datetime(2013, 6, 5, 0, 0), 'grade': 'A', 'score': 7}, {'date': datetime.datetime(2012, 4, 13, 0, 0), 'grade': 'A', 'score': 12}, {'date': datetime.datetime(2011, 10, 12, 0, 0), 'grade': 'A', 'score': 12}], 'name': 'Riviera Caterer', 'restaurant_id': '40356018'}", metadata={'database': 'sample_restaurants', 'collection': 'restaurants'})