MongoDB

MongoDB Basics

Command
Description

mongod

Start MongoDB server

mongo

Open the MongoDB shell

show dbs

Show all the available databases

use db_name

Switch to the specified database or create one if it doesn't exist

db

Display the current database you are using

db.dropDatabase()

Drop the current database

CRUD Operations

Insert

Command
Description

db.collection.insertOne(document)

Insert a new document into the collection

db.collection.insertMany([document1, document2, ...])

Insert multiple documents into the collection

Find

Command
Description

db.collection.find()

Retrieve all documents from the collection

db.collection.find(query)

Retrieve documents that match the specified query

db.collection.findOne()

Retrieve one document from the collection

db.collection.findOne(query)

Retrieve one document that matches the specified query

Update

Command
Description

db.collection.updateOne(query, update)

Update the first document that matches the query

db.collection.updateMany(query, update)

Update all documents that match the query

db.collection.replaceOne(query, replacement)

Replace the first document that matches the query

Delete

Command
Description

db.collection.deleteOne(query)

Delete the first document that matches the query

db.collection.deleteMany(query)

Delete all documents that match the query

Query Operators

Comparison Operators

Operator
Description
Example

$eq

Matches values that are equal to a specified value

{ age: { $eq: 18 } }

$ne

Matches all values that are not equal to a specified value

{ age: { $ne: 18 } }

$gt

Matches values that are greater than a specified value

{ age: { $gt: 18 } }

$gte

Matches values that are greater than or equal to a specified value

{ age: { $gte: 18 } }

$lt

Matches values that are less than a specified value

{ age: { $lt: 18 } }

$lte

Matches values that are less than or equal to a specified value

{ age: { $lte: 18 } }

$in

Matches any of the values specified in an array

{ name: { $in: ['Alice', 'Bob'] } }

$nin

Matches none of the values specified in an array

{ name: { $nin: ['Alice', 'Bob'] } }

Logical Operators

Operator
Description
Example

$and

Joins query clauses with a logical AND

{ $and: [ { name: 'Alice' }, { age: 18 } ] }

$or

Joins query clauses with a logical OR

{ $or: [ { name: 'Alice' }, { name: 'Bob' } ] }

$not

Inverts the effect of a query expression

{ age: { $not: { $gte: 18 } } }

$nor

Joins query clauses with a logical NOR

{ $nor: [ { name: 'Alice' }, { name: 'Bob' } ] }

Element Operators

Operator
Description
Example

$exists

Matches documents that have the specified field

{ age: { $exists: true } }

Create collection in MongoDB

To create a collection in MongoDB, you can use the db.createCollection() method in the MongoDB shell. The syntax for creating a collection is as follows:

db.createCollection("<collection_name>", <options>)

Where <collection_name> is the name of the collection you want to create and <options> are the optional parameters that you can use to configure the collection. Here are some examples of how you can create a collection:

  1. Create a collection without any options:

> db.createCollection("users")

This will create a collection named "users" in the current database.

  1. Create a collection with options:

> db.createCollection("users", {
    capped: true,
    size: 100000,
    max: 100
  })

This will create a capped collection named "users" with a maximum size of 100,000 bytes and a maximum number of documents of 100.

You can also create a collection using the MongoCollection class in a MongoDB driver for your programming language. The specific syntax for doing this will depend on the driver you are using.

Last updated