IDE
Download
- Download MongoDB for VSCode in Visual Studio Code
Create Cluster
- Create Cluster on MongoDB Web
Connection
- Click Connection button on Mongo DB web and check your cluster connection string.
- Click MongoDb Playground on side bar.
- Connect your cluster in VSCode with your connection string.
- Change the name
Playground
- Create new Playground to add your script.
IRUD
Insert
useswitchs current database.db.{collection}accesses to the collection.insertOneinserts one document on your collection.
use('video');
var movie = {"title" : "Star Wars: Episode IV - A New Hope" ,
"director" : "George Lucas",
"year" : 1977};
db.movies.insertOne(movie);
insertManyinserts many document on your collection.
db.movies.insertMany([{"title" : "Ghostbusters"},
{"title" : "E.T"},
{"title" : "Blade Runner"}]);
Read
findshows all documents.
db.movies.find().pretty();
findOneshows only one document.
db.movies.findOne();
Update
updateOneupdates one document.
db.movies.updateOne({title : "Star Wars: Episode IV - A New Hope"},
{$set : {reviews:[]}});
Delete
deleteOnedeletes one document.
db.movies.deleteOne({title : "Star Wars: Episode IV - A New Hope"});
deleteManydeletes many documents.
deleteMany({})deletes all documents.
Drop
dropdrops the collection.
Blocking
db.dropDatabase = DB.prototype.dropDatabase = no; // block to delete database
DBCollection.prototype.drop = no; // block to delete collection
DBCollection.prototype.dropIndex = no; // block to delete Index
DBCollection.prototype.dropIndexes = no; // block to delete IndexesDocument
Replace
replaceOnechanges database version.
Modifier
$incincrease field value.
$setset field value.$unsetremove field from the document.
$pushadds element or create new array.$eachadds elements at once.$slicelimits the array size.$sortsorts elements with the field value.$eachis always needed when you use$pushwith$sliceor$sort.
$nefinds documents which don’t have not specific value.
$addToSetadd specific value without duplication.
$ne/$pushcan add only one value and$addToSet/$eachcan add several values at once.
$popremoves array elements like stack or queue.{"$pop" : {"key" : 1}}removes elements from last.{"$pop" : {"key" : -1}}removes elements from first.
$pullremoves array elements with specific conditions.