To find documents in MongoDB, we can use the "find()" method.
This method retrieves documents from a collection that match the specified query criteria.
Here's how we can use it in the MongoDB shell:
Before creating a Collection(table) in MongoDB, First, we need to connect to our MongoDB instance using the MongoDB shell or a MongoDB client library using our programming language.
Using the Mongo shell, we can connect to a MongoDB server running on localhost by simply typing the `mongo` command in our terminal.
mongo
Once our connection is established successfully, we can check databases in MongoDB using the below command:
show dbs;
Switch to the database where you want to create the collection using the "use" command.
use your_database_name;
To check the current database name using mongo shell.
db
Use the "createCollection()" method to explicitly create a collection.
db.createCollection("your_collection_name")
# Connect to MongoDB (assuming it's running locally on the default port) mongo # Switch to the desired database use your_database_name # Create a collection named your_collection_name db.createCollection("your_collection_name")
The above command will create a collection in our specified Database.
# Insert multiple documents into a collection db.your_collection_name.insertMany([ {name: "Alice", Department: "HR", age: 36, city: "London"}, {name: "Bella", Department: "Director", age: 32, city: "Polo Alto"}, {name: "Warner", Department: "Business Analyst", age: 30, city: "London"} ])
Use the "find()" method on the collection to retrieve documents based on your query criteria.
# Connect to MongoDB (assuming it's running locally on the default port) mongo # Switch to the desired database use your_database_name # Find all documents in a collection db.your_collection_name.find() # Find All documents in the specific collection db.your_collection_name.find({ }) # Find All documents in the specific collection
Find all documents that match certain conditions.
# Find documents that match specific criteria db.your_collection_name.find({ age: { $gt: 30 } }) # Find documents where age is greater than 30 # Find documents that match specific criteria db.your_collection_name.find({ name: "Alice" }) # Find documents where name is "Alice"
using the below query, we can find the specified or single document from the collection.
# Find documents where _id is "xxxxx-324-xxxxxxx-xxx" db.your_collection_name.find({ _id: ObjectId("xxxxx-324-xxxxxxx-xxx") })
Replace "your_database_name" with the name of your database and "your_collection_name" with the name of your collection.
We can also replace the query criteria with your own criteria.
The "find()" method returns a cursor, which we can iterate over to access the documents.
If we need to display the documents in a more readable format, we can use the "pretty()" method:
# Find documents and display them in a more readable format db.your_collection_name.find().pretty()
This will display the documents with proper formatting for easier readability.
MongoDB uses a query language that supports various operators like "$eq", "$gt", "$lt", "$in", "$or", etc. to build complex queries.
We can tailor our queries to match our specific requirements.