Working with Documents

Inserting Documents

You can insert a single document into a collection using the insert() method:

db.collectionName.insert({name: "Jane Smith", age: 25, email: "[email protected]"})

To insert multiple documents at once, use the insertMany() method:

db.collectionName.insertMany([
  {name: "Alice", age: 35, city: "New York"},
  {name: "Bob", age: 28, city: "London"},
  {name: "Charlie", age: 42, city: "Paris"}
])

Displaying Documents

To retrieve and display documents from a collection, use the find() method:

db.collectionName.find()

This will return all documents in the collection.

You can also format the output for better readability using the pretty() method:

db.collectionName.find().pretty()

Displaying Collections

To list all collections in the current database, use the show collections command:

show collections

To view statistics and information about a specific collection, use the stats() method:

db.collectionName.stats()

Last updated

Was this helpful?