Mongo
Mongo
Graph database
Document-oriented
Column family
Graph Database
Databases
In MongoDB, databases hold one or more collections of documents.
To select a database to use, in mongosh, issue the use <db>
statement, as in the following example:
use myDB
If you want to check your databases list, use the command show
dbs.
>show dbs
Databases and Collections
Explicit Creation
MongoDB provides the db.createCollection() method to explicitly
create a collection with various options, such as setting the
maximum size or the documentation validation rules.
If you are not specifying these options, you do not need to explicitly
create the collection since MongoDB creates new collections when
you first store data for the collections.
db.createCollection(name, options)
If you want to check your Collections list, use the command show
collections.
>show collections
Databases and Collections
Create a Collection
If a collection does not exist, MongoDB creates the collection when
you first store data for that collection.
db.myNewCollection2.insertOne( { x: 1 } )
db.myNewCollection3.createIndex( { y: 1 } )
Both the insertOne() and the createIndex() operations create their
respective collection if they do not already exist.
Be sure that the collection name follows MongoDB Naming
Restrictions.
Databases and Collections
Create a Database
If a database does not exist, MongoDB creates the database when
you first store data for that database.
As such, you can switch to a non-existent database and perform the
following operation in mongosh:
use myNewDB
db.myNewCollection1.insertOne( { x: 1 } )
The insertOne() operation creates both the database myNewDB
and the collection myNewCollection1 if they do not already exist.
Be sure that both the database and collection names follow
MongoDB Naming Restrictions.
CRUD operations
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.
insertMany()
• To insert multiple documents at once, use the insertMany()
method.
• This method inserts an array of objects into the database.
Insert Documents
insertOne()
In MongoDB, insert operations target a single collection. All write
operations in MongoDB are atomic on the level of a single
document.
Insert Multiple
Documents
• db.collection.insertMany() can insert multiple documents into a
collection. Pass an array of documents to the method.
• If the documents do not specify an _id field, MongoDB adds the _id
field with an ObjectId value to each document.
db.inventory.insertMany([
{ item: "journal", qty: 25, tags: ["blank", "red"], size: { h: 14, w: 21,
uom: "cm" } },
{ item: "mat", qty: 85, tags: ["gray"], size: { h: 27.9, w: 35.5, uom:
"cm" } },
{ item: "mousepad", qty: 25, tags: ["gel", "blue"], size: { h: 19, w:
22.85, uom: "cm" } }
])
Read Operations
• You can specify query filters or criteria that identify the documents to
return.
Read Operations
https://www.mongodb.com/docs/manual/reference/operator/query/
Read Operations
Specify OR Conditions
• Using the $or operator, you can specify a compound query that joins
each clause with a logical OR conjunction so that the query selects the
documents in the collection that match at least one condition.
Query an Array
• To specify equality condition on an array, use the query document
{ <field>: <value> } where <value> is the exact array to match,
including the order of the elements.
• The following example queries for all documents where the field tags
value is an array with exactly two elements, "red" and "blank", in the
specified order:
• If, instead, you wish to find an array that contains both the elements
"red" and "blank", without regard to order or other elements in the
array, use the $all operator:
Read Operations
Query an Array
• the dim_cm array contains elements that in some combination satisfy
the query conditions; e.g., one element can satisfy the greater than 15
condition and another element can satisfy the less than 20 condition,
or a single element can satisfy both:
db.inventory.updateMany(
{ "qty": { $lt: 50 } },
{
uses the $set operator to update the value
$set: { "size.uom": "in", status: "P" },
of the size.uom field to "in" and the value of
$currentDate: { lastModified: true }the status field to "P",
}
uses the $currentDate operator to update
) the value of the lastModified field to the
current date.
Update Operations
Replace One Documents
db.inventory.replaceOne(
{ item: "paper" },
{ item: "paper", instock: [ { warehouse: "A", qty: 60 },
{ warehouse: "B", qty: 40 } ] }
)
Delete Operations
• Delete operations remove documents from a collection. MongoDB
provides the following methods to delete documents of a collection:
• db.collection.deleteOne() New in version 3.2
• db.collection.deleteMany() New in version 3.2
• In MongoDB, delete operations target a single collection. All write
operations in MongoDB are atomic on the level of a single document.
Connection
MongoClient
• Add the MongoDB Node.js Driver to your Node project dependencies
npm install mongodb
MongoClient
new MongoClient(url, options)
Creates a new MongoClient instance
• MongoClient.connect(url, options)
• connect(callback)
• db(dbName, options)
• close(force, callback)
• isConnected(options)
Connect to MongoDB from
NodeJS
const { MongoClient, ServerApiVersion } = require('mongodb');
const uri
="mongodb+srv://psaikiran:[email protected]/";