MongoDB queries and tasks you should know
If you want to master MongoDB's common queries & operations, you should read this article.
Whether you're looking to retrieve or manipulate data, deploy powerful models, or build responsive apps, a deep understanding of common MongoDB queries and operations can enhance your skills . .
Create or transfer database
Creating a local database via the MongoDB Shell is simple, especially when you have set up a remote cluster. You can create a new database in MongoDB with the use command:
use db_name
While the above command creates a new database, you can use it to switch to an existing database without having to create a new file from scratch.
Delete database
First, switch to the database you want to delete with the use command as you did before. Next, delete the database with the dropDatabase() command :
use db_name db.dropDatabase()
Create a collection
To create a collection, switch to the target database. Use the createCollection() keyword to create a new MongoDB collection:
db.createCollection("collection_name")
Replace collection_name with the selected collection name.
Insert a document into a gallery
While sending data to the collection, you can insert a document or an array of documents.
To insert a document:
db.collection_name.insertOne({"Name":"Idowu", "Likes":"Chess"})
You can also use the above method to insert an array of documents with an ID:
db.collection_name.insertOne([{"Name":"Idowu", "Likes":"Chess"}, {"Language": "Mongo", "is_admin": true}])
To insert multiple documents at once, each with its own ID, use the insertMany keyword :
db.collection_name.insertMany([{"Name":"Idowu", "Likes":"Chess"}, {"Name": "Paul", "Likes": "Wordle"}])
Get an entire document from a collection
You can query entire documents from a collection using the find() keyword :
db.collection_name.find()
The above command returns all documents inside the specified collection:
You can also restrict the returned data to a specific number. For example, you can use the following command to get only the first two documents:
db.collection_name.find().limit(2)
Filter documents in a gallery
There are many ways to filter documents in MongoDB. Consider the following example:
If querying only a specific field in the document, use the find method :
db.collection_name.find({"Likes":"Wordle"}, {"_id":0, "Name":1})
The above command returns the entire document where the value of Likes is Wordle . It just outputs the name and ignores the document ID.
You can also filter a collection by numeric factor. Let's say you want to get the names of all users older than 21 years old, using the $gt operator :
db.collection_name.find({"Likes":"Chess", "Age":{"$gt":21}}, {"_id":0, "Name":1})
The result will look like this:
Try replacing find with findOne to see what happens. However, there are many other filter keywords available:
- $lt : All values less than a certain value.
- $gte : Value equal to or greater than the specified value.
- $lte : Value less than or equal to the specified value.
- $eq : Get the entire value equal to the specified value.
- $ne : The whole value is not equal to the specified value.
- $in : Use it when querying against an array. It retrieves the entire value that matches any item in the array. The $nin keyword is the opposite.
Above are the MongoDB queries and operations you need to know. Hope the article is useful to you.
You should read it
- Index (Mong) in MongoDB
- Query analysis in MongoDB
- Instructions on 2 ways to install MongoDB on Raspberry Pi
- Aggregation in MongoDB
- Advantages of MongoDB
- Data type in MongoDB
- MongoDB malicious code attacks more than 26,000 victims in a week
- Query Document in MongoDB
- Learn about security features and authentication in MongoDB
- ObjectId in MongoDB
- Text Search in MongoDB
- Regular Expression in MongoDB
Maybe you are interested
How to Use Google Slides to Create a Diagram
Why use Waze instead of Google Maps?
5 Ways to Use Google Maps Without Planning a Route
How to use Auto Clicker Assist to automatically click the mouse
Useful tips to improve your Smart TV experience
How to change the default font in Google Docs - Use the font of choice