MongoDB Tutorial

Introduction

MongoDB is a database that's easy to use and can scale on demand. It's a good choice for large scale data processing, web services, and mobile apps. This tutorial will walk you through the basic principles of MongoDB querying, including how to create database objects, retrieve them using normal queries, indexing and cursors.

Installing MongoDB

You can install MongoDB on your computer or on a cloud computing platform. You need to choose the right installation method for your particular situation, as there are many different ways in which you can do this.

Querying

Querying a collection is one of the most common operations you'll perform with MongoDB. It's also something that can be difficult to get right, especially if you're new to the language and don't know exactly how it works.

To query a collection, you need to know what queries are available for this type of data set and how they work behind-the-scenes.

Indexing and Cursors

Indexing and cursors are a big part of the MongoDB query engine, so we'll start there. Cursors are used to do partial queries on large documents or collections, while indexes allow you to perform more complex queries in one step by reducing the number of documents that must be scanned through your database.

Operators

Insert One Document

Insert a document with one of its fields. You can also specify an array of values for that field, or you can specify a single value to override the default value.

Insert Many Documents

This returns a cursor, which can be used to iterate over the inserted documents. For example:

```javascript

cursor = mongoose.model("documents").insertMany(documentIds);

for (var i = 0; i < documentIds[0].length; ++i) {

// INSERT INTO "documents" (title) VALUES ('New Title')

var newDocs = [{ title: 'New Title 1', text: 'This is my first document!' }];

// INSERT INTO "documents" (title) VALUES ('New Title 2')

var newDocs2 = [{ title: 'New Title 2', text: 'This is also my first document!' }];

Mongoose will automatically populate these documents with default values if they do not exist yet.

Update One Document

In this section, you'll learn how to update one document in MongoDB.

Here's an example of updating a document: $updateOneDoc("myKey", 1)

The Update operation takes three arguments: the key (or path), the value, and any modifiers that may be applicable. The first two arguments are required; however, there can be any number of modifiers applied at once. Modifiers are comma-separated strings with each string being one modifier name plus its associated value separated by commas: ```aws\mongodb\MongoClient->findOne( "myKey" );```

Update Many Documents

The MongoDB Update command allows you to update multiple documents in a single operation. This is useful when you want to change the property values of multiple documents at once, or if you want to use bulk inserts with an array of documents.

The updateOne() method lets you update one document by specifying an existing field on the model and its new value for that field as arguments. For example:

> db.collection("users").updateOne({name: "Sue"}, {$set: {age: 50}});

Delete One Document

Deleting a single document is simple. Just call the removeOne() method on your database, and it will do its thing:

```javascript var mongoose = require('mongoose'); var numDocs = 3; mongoose.connect('mongodb://localhost/test', function(err) { console.log('Connected to MongoDB'); console.log('MongoEngine ready.'); // Create collection with name "users" var collection = new mongoose.Collection("users"); // Add documents from Object literal array collection .insertMany([ {name: 'John Doe'}, {name: 'Jane Doe'}, ]); }); ```

Delete Many Documents

You can delete a collection of documents. To delete an entire collection, use the following command:

Thanks for reading!

I hope you've enjoyed this article and I'd like to thank you for reading. If you want to learn more about MongoDB, please check out the official website at [mongoDB](https://www.mongodb.com/).