Author: admin
-
MongoDB Aggregation Pipelines
Aggregation Pipelines Aggregation operations allow you to group, sort, perform calculations, analyze data, and much more. Aggregation pipelines can have one or more “stages”. The order of these stages are important. Each stage acts upon the results of the previous stage. Example Sample Data To demonstrate the use of stages in a aggregation pipeline, we…
-
MongoDB Update Operators
MongoDB Update Operators There are many update operators that can be used during document updates. Fields The following operators can be used to update fields: Array The following operators assist with updating arrays.
-
MongoDB Query Operators
MongoDB Query Operators There are many query operators that can be used to compare and reference document fields. Comparison The following operators can be used in queries to compare values: Logical The following operators can logically compare multiple queries. Evaluation The following operators assist in evaluating documents.
-
MongoDB mongosh Delete
Delete Documents We can delete documents by using the methods deleteOne() or deleteMany(). These methods accept a query object. The matching documents will be deleted. deleteOne() The deleteOne() method will delete the first document that matches the query provided. Example deleteMany() The deleteMany() method will delete all documents that match the query provided. Example
-
MongoDB mongosh Update
Update Document To update an existing document we can use the updateOne() or updateMany() methods. The first parameter is a query object to define which document or documents should be updated. The second parameter is an object defining the updated data. updateOne() The updateOne() method will update the first document that is found matching the provided query. Let’s see what the…
-
MongoDB mongosh Find
Find Data There are 2 methods to find and select data from a MongoDB collection, find() and findOne(). find() To select data from a collection in MongoDB, we can use the find() method. This method accepts a query object. If left empty, all documents will be returned. Example findOne() To select only one document, we can use the findOne() method. This method…
-
MongoDB mongosh Insert
Insert Documents There are 2 methods to insert documents into a MongoDB database. insertOne() To insert a single document, use the insertOne() method. This method inserts a single object into the database. Note: When typing in the shell, after opening an object with curly braces “{” you can press enter to start a new line in the editor…
-
MongoDB mongosh Create Collection
Create Collection using mongosh There are 2 ways to create a collection. Method 1 You can create a collection using the createCollection() database method. Example Method 2 You can also create a collection during the insert process. Example We are here assuming object is a valid JavaScript object containing post data: This will create the “posts” collection if it does not already…
-
MongoDB mongosh Create Database
Create Database using mongosh After connecting to your database using mongosh, you can see which database you are using by typing db in your terminal. If you have used the connection string provided from the MongoDB Atlas dashboard, you should be connected to the myFirstDatabase database. Show all databases To see all available databases, in your terminal type show dbs. Notice…
-
MongoDB Query API
MongoDB Query API The MongoDB Query API is the way you will interact with your data. The MongoDB Query API can be used two ways: MongoDB Query API Uses You can use the MongoDB Query API to perform: