0% found this document useful (0 votes)
16 views10 pages

Mongodb Commands

The document outlines various MongoDB commands for creating and managing databases and collections, including inserting, updating, and deleting documents. It provides examples for creating databases, collections with options, querying documents based on criteria, and using projection, limit, skip, and sort methods. Each experiment is presented with specific commands and expected outcomes for effective database manipulation.

Uploaded by

arnildsouza154
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views10 pages

Mongodb Commands

The document outlines various MongoDB commands for creating and managing databases and collections, including inserting, updating, and deleting documents. It provides examples for creating databases, collections with options, querying documents based on criteria, and using projection, limit, skip, and sort methods. Each experiment is presented with specific commands and expected outcomes for effective database manipulation.

Uploaded by

arnildsouza154
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

MONGODB COMMANDS :

EXP 1 : CREATE AND DROP DATABASE:

mongosh

show dbs

use myDatabase

[Link]({name:”john doe”,age:25})

show collections

[Link]()

show dbs (to verify deletion)


EXP 2 : TO CREATE A COLLECTION

mongosh

use myDatabase

1] to create collections explicitly:

[Link](“admin”)

show collections

2] to create collection by inserting document :

[Link]({name:”laptop”,price:”50000”})

show collections
EXP 3 : CREATE A COLLECTION WITH OPTIONS AND DROP THE COLLECTION

mongosh

use myDatabase

1] create a capped collection :

[Link](“logs”,{capped:true,size:5000,max: 100})

2]create a collection with validation rules

[Link](“students”,{
validator:{
$jsonSchema:{
bsonType:”object”,
required:[“name”,”age”],
properties:{
name:{
bsonType:”string”,
description:”name must be a string”

},
Age:{
bsonType:”int”,
minimum:18,
description:”age must be greater than 18”
}
}
}
}
})

[Link]({name:”john doe”, age:25})

[Link]([
{name:”arnil”, age:20},
{name:”afif”, age:30}
])

show collections

[Link]()

show collections
EXP 4 : INSERT A DOCUMENT INTO MONGODB COLLECTION

mongosh

use myDatabase

1] insert single document :

[Link]({name:”john doe” ,age:25, city:”mangalore”})

[Link]().pretty() (to verify if it was inserted)

2] insert many documents :

[Link]([
{ name: "Alice", age: 22, city: "Bangalore" },
{ name: "Bob", age: 30, city: "Delhi" },
{ name: "Charlie", age: 27, city: "Mumbai" }
])

[Link]().pretty() (to verify if it was inserted)


EXP 5 : QUERY ALL DOCUMENTS IN JSON FORMAT AND BASED ON CRITERIA

mongosh

use myDatabase

[Link]([
{ name: "John Doe", age: 25, city: "Mangalore" },
{ name: "Alice", age: 22, city: "Bangalore" },
{ name: "Bob", age: 30, city: "Delhi" },
{ name: "Charlie", age: 27, city: "Mumbai" }
])

1] In json format :

[Link]().pretty()

2] Based on Criteria :

a)Query by exact match :

[Link]({city:”Bangalore”}).pretty()

b)Query using comparison operators :

[Link]({age : { $gt: 25}}).pretty()

c)Query with Logical Operators:

[Link]({ $or: [{ city: "Bangalore" }, { city: "Mumbai" }] }).pretty()

d)Query with specific fields:

[Link]({},{ _id: 0, name:1 ,age:1}).pretty()

e)Query with regex:

[Link]({name : /^A/}).pretty()

f)Query with sorting :

[Link]().sort({age:-1}).pretty()

g)Query with limit and skip :

[Link]().skip(1).limit(2).pretty()
EXP 6 : TO UPDATE DOCUMENT (updateOne() and replaceOne() )

mongosh

use myDatabase

[Link]().pretty()

1] updateOne() method :

[Link]({name:”arnil”} , { $set: {city:”mangalore”}})

[Link]({name:”arnil”}).pretty()

2] replaceOne() method :

[Link]({ name: "arnil" }, { name: "arnil", age: 35, city: "Mumbai" })

[Link]({name:”arnil”}).pretty()
EXP 7 : DELETE DOCUMENTS USING (deleteOne() and deleteMany() )

mongosh

use myDatabase

[Link]([
{ name: "John Doe", age: 25, city: "Mangalore" },
{ name: "Alice", age: 22, city: "Bangalore" },
{ name: "Bob", age: 30, city: "Delhi" },
{ name: "Charlie", age: 27, city: "Bangalore" }
])

[Link]().pretty()

1]deleteOne() :

[Link]({name:”john doe”})

[Link]({name:”john doe”}).pretty()

2]deleteMany() :

[Link]({city:”Bangalore”})

[Link]({city :”Bangalore”}).pretty()

[Link]({})

[Link]()
EXP 8 : MONGODB PROJECTION

mongosh

use myDatabase

[Link]([
{ name: "John Doe", age: 25, city: "Mangalore", email: "[Link]@[Link]" },
{ name: "Alice", age: 22, city: "Bangalore", email: "alice@[Link]" },
{ name: "Bob", age: 30, city: "Delhi", email: "bob@[Link]" },
{ name: "Charlie", age: 27, city: "Bangalore", email: "charlie@[Link]" }
])

[Link]().pretty()

1] projection to incude specific fields :

[Link]({},{name:1,age:1,_id:0}).pretty()

2] exclude specific fields:

[Link]({},{email:0}).pretty()

3] projection with query condition :

[Link]({ city:”Mangalore”}, {name:1, age:1, _id:0}).pretty()

4] projection with sorting :

[Link]({},{name:1,age:1,_id:0}).sort({age:-1}).pretty()
EXP 9 : LIMIT , SKIP AND SORT METHODS

mongosh

use myDatabase

[Link]([
{ name: "John Doe", age: 25, city: "Mangalore", email: "[Link]@[Link]" },
{ name: "Alice", age: 22, city: "Bangalore", email: "alice@[Link]" },
{ name: "Bob", age: 30, city: "Delhi", email: "bob@[Link]" },
{ name: "Charlie", age: 27, city: "Bangalore", email: "charlie@[Link]" }
])

[Link]().pretty()

1] limit() :

[Link]().limit(2).pretty()

2] skip() :

[Link]().skip(2).limit(2).pretty()

3] sort() :

[Link]().sort({age:1}).pretty()

[Link]().sort({ city: 1, age: 1 }).pretty()

4] combined :

[Link]().sort({age:-1}).skip(2).limit(2).pretty()

You might also like