MongoDB queries and tasks you should know

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

MongoDB queries and tasks you should know Picture 1

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:

MongoDB queries and tasks you should know Picture 2

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:

 

MongoDB queries and tasks you should know Picture 3

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.

MongoDB queries and tasks you should know Picture 4

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:

MongoDB queries and tasks you should know Picture 5

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.

5 ★ | 1 Vote

May be interested

  • Text Search in MongoDBText Search in MongoDB
    starting with version 2.4, mongodb started supporting text indexes to search within the string content.
  • Limitations of indexes in MongoDBLimitations of indexes in MongoDB
    each index not only occupies part of the memory space but also causes an overhead on each insert, update, and delete operation. so, if you rarely use your collection for read operations, you should not use the index for it.
  • Create parameter queries in Access 2016Create parameter queries in Access 2016
    parameter queries are one of the simplest and most useful queries you can create. because parameter queries are so simple, they can be easily updated to reflect the new search terms.
  • Shard in MongoDBShard in MongoDB
    sharding 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.
  • Learn about Java Driver in MongoDBLearn about Java Driver in MongoDB
    in the following article, we will introduce you some basic features of mongodv java driver as well as how to deploy and apply in practice.
  • Install MongoDBInstall MongoDB
    instructions for installing mongodb on windows.
  • Map Reduce in MongoDBMap Reduce in MongoDB
    in mongodb documentation, map-reduce is a data processing system that condenses a large amount of data into useful overall results. mongodb uses mapreduce command for map-reduce operation. in general, map reduce is used to handle large data sets.
  • Aggregation in MongoDBAggregation in MongoDB
    aggregation 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.
  • Data modeling in MongoDBData modeling in MongoDB
    data in mongodb has a flexible schema. documents in the same collection need not have the same set of fields or structures, and common fields in collection documents can keep different data types.
  • How to use Aggregation Pipeline in MongoDBHow to use Aggregation Pipeline in MongoDB
    if you are using mongodb's mapreduce, it is best to switch to aggregation pipeline for more efficient computation.