You are on page 1of 2

MongoDB Workshop

05/11/2022

use symbd;
show dbs;

db.createCollection(“users”)
show collections;

insert entry
db.users.insert({ uname: ”amar”})

display all in entries in collection


db.users.find()

db.users.find( {uname: “amar”} )

find only first entry


db.users.findOne({'name':'Devansh'});

limit entries
db.users.find().limit(2).forEach(printjson);

sort ascending
db.users.find().sort({name:1});

sort descending
db.users.find().sort({name:1});

omit columns while finding


db.users.find({'name':'Devansh'}, {'_id':1, 'name':1, 'city':0});
# Size of mongoDB document cannot exceed 16 MB
# Larger files are split into smaller parts using gridfs

# Depth of embedding cannot be more than 100

Remove entries
db.users.remove( {“name”: “Devansh”}, 2);

Upsert: Update if present, else insert


db.users.update(
{‘name’:”Devansh”},
{
“name”:”Devansh”,
“city”:”Delhi”
},

{upsert:true}
)

Inserting embedded doc

db.student.insert({
regNo:"3014",
name:"Test Student",

course: {courseName: "BTech",duration:"4 Years"},


address: {city:"Pune",state:"Maharashtra",country:"India"}
}
);

You might also like