Written by Mohammed Q. Sattar
show dbs
use databaseName
Example: use admin
Note: If you write "use database" which does not exist, it will create this database.
example use school
Note: "school" was not listed in the database but now it has been created.
To fill a database when it is empty:
db.createCollection("Students")
db.dropDatabase()
Note: Database should be used first to be dropped.
Note: If the collection is not created yet, once you write an insert command to insert data, the collection will be created.
db.students.insertOne({name: "Mohammed", age: 30, gpa: 3.2})
cls
db.students.find()
Insert many documents into a collection:
db.students.insertMany([{name: "john", age: 33, gpa: 1.5}, {name: "lilan", age: 27, gpa: 3.5}, {name: "aldeno", age: 27, gpa: 4.5}])
String name: "Larry" Integer age: 32 Double gpa: 2.7 Boolean fullTime: false Date registerDate: new Date() NoValue graduationDate: null Array courses: ["Biology", "Chemistry", "Calculus"] address: {street: "123 Fake St.", city: "Bikini Bottom", zip: 12345}
db.students.find().sort({name: 1}) // 1 means alphabetical order
db.students.find().sort({name: -1}) // -1 means reverse alphabetical order
db.students.find().sort({gpa: 1})
db.students.find().sort({gpa: -1})
db.students.find().limit(2) // 2 means number of documents returned
db.students.find().sort({gpa: -1}).limit(1) // Highest GPA, limit 1