MongoDB queries and tasks you should know

MongoDB is one of the most coveted and admired NoSQL databases for professional development. Its flexibility, scalability and handling of large volumes of data make it the first choice for modern applications.

If you want to master MongoDB's common queries & operations, you should read this article.

Picture 1 of MongoDB queries and tasks you should know

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:

Picture 2 of MongoDB queries and tasks you should know

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:

 

Picture 3 of MongoDB queries and tasks you should know

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.

Picture 4 of MongoDB queries and tasks you should know

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:

Picture 5 of MongoDB queries and tasks you should know

Try replacing find with findOne to see what happens. However, there are many other filter keywords available:

  1. $lt : All values ​​less than a certain value.
  2. $gte : Value equal to or greater than the specified value.
  3. $lte : Value less than or equal to the specified value.
  4. $eq : Get the entire value equal to the specified value.
  5. $ne : The whole value is not equal to the specified value.
  6. $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.

Update 18 August 2023
Category

System

Mac OS X

Hardware

Game

Tech info

Technology

Science

Life

Application

Electric

Program

Mobile