Last active
April 1, 2022 15:43
-
-
Save Andaeiii/853efef8907eb34d05c75c8295fea968 to your computer and use it in GitHub Desktop.
Mongo DB short Commands on how to go about the basics in CRUD operations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// mongoDB Shell Commands... | |
// show dbs - show databases. | |
// use mycustomDatabase - create database and switch | |
// db - show me the current database being used... | |
db.createUser({ | |
user:"antyrii", | |
pwd: "99wcl34", | |
roles:["readWrite", "dbAdmin" ] | |
}) | |
//collections - tables in Relational DB | |
db.createCollection('customers'); //create table.. | |
db.show collections // show tables... | |
db.customers.find(); //no need of creating an ID field... | |
db.customers.insert({firstname:"amin", lastname:"man"}); //one record | |
//for multiple records and extra field on diff. columns.. | |
db.customers.insert([{firstname:"mochacho", lastname:"ocholi"}, {firstname:"linda",lastname:"eno",gender:"female"}, {firstname:"okwuchi", lastname:"ugozie"},{firstname:"senora",lastname:"kme"}]); | |
//you can have an extra field on a column... | |
db.customers.find().pretty() //helper function - to objects.. | |
//updating fields... update(criteriaKey, newObject) | |
db.customers.update({firstname:"linda", {firstname:"andrea", gender:"male"}) //replaces the entire object. | |
db.customers.update({firstname:"okwuchi", {$set:{gender:"female"}}) //keep previous keys and update record. | |
//for interger values... | |
db.customers.update({firstname:"senora", {$inc:{age:5}}) //find the record and increment the age by 5 | |
//to remove a column... | |
db.customers.update({firstname:"mochacho"}, {$unset:{lastname:1}}) //use unset:field:1 // one | |
//to update a record that isnt there... | |
db.customers.update({firstname:"mary"}, {firstname:"Marianne", gender:"male"}) | |
// -- returns 0,0,0 - unmatched... | |
// -- so if it isnt found - then insert it... | |
db.customers.update({firstname:"mary"}, {firstname:"Marianne", gender:"male"}, {upsert:true}) //to updateInsert it. | |
// To Remove Documents... | |
db.customers.remove({firstname:"Andrea"}) | |
db.customers.remove({firstname:"Andrea"}, {justOne:true) //just the first one.. | |
db.customers.find({$or:[{firstname:"okwuchi}, {firstname:"senora"}]}) //multiple fields & criteria.. | |
db.customers.find({age:{$gt:20}}) //>20 < 20, ~ gte, lte, gt, lt. | |
db.customers.find({"address.city":"boston"}) // for object properties.. | |
//for array properties... | |
db.customers.find({membership:"mem1"}) //where membership is an array ["mem1","mem2","mem3"] | |
db.customers.find(~).sort({lastname:1}) //ascending | |
db.customers.find(~).sort({lastname:-1}) //descending | |
db.customers.find(~).count(); // count documents... | |
db.customers.find(~).limit(4).sort({firstname:-1}); //limit 4 | |
db.customers.find().forEach(doc => print("Customer :: " + doc.firstname)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment