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
May be interested
- Aggregation in MongoDBaggregation can be understood as aggregation. the aggregation operation handles data records and returns calculated results. the operations group the values from multiple documents together, and can perform multiple operations on the grouped data to return a single result. in sql, count (*) and group by are equivalent to aggregation in mongodb.
- ObjectId in MongoDByou have seen the use of mongodb objectid in previous chapters. in this chapter, we will understand the structure of objectid.
- Replica Set in MongoDBreplication is the process of synchronizing data from multiple servers. replication provides redundancy and increased data availability for multiple data copies on many different database servers. replication protects a database from the loss of a particular server. replication also allows you to recover data from hardware errors or service disconnections. with additional data copies, you can use it for recovery, reporting, or backup.
- Shard in MongoDBsharding is a process of storing data records across multiple devices and it is a method of mongodb to meet the requirement for increasing data. when the size of the data increases, a single device cannot be enough to store data.
- Create Backup in MongoDBto create a database backup in mongodb, you should use the mongodump command. this command will dump all server data into dump directory. there are many options available from which you can limit the amount of data or backup created by remote server.
- MongoDB Deploymentwhen you are preparing a mongodb deployment, you should understand how your application is being supported in production.