db.runCommand({command: 1}) used to run some commands like listCollections to list collections that are on the db
- db: used to show current database
- show dbs: show all existing databases
- use db, switch or create new
- db.dropDatabase(“df”) to delete
Collections
- db.createCollection(“dd”)
- db.collection.insert() creates during insertion
- db.collection.drop()
- there is also db.collection.renameCollection()
- db.getCollectionNames()
Querying
db.users.find({}, {}) is used to do queries on collections, the first parenthesis is used for query conditions, the second one is used for projections
$allis like contains all of what I AM SPECIFYING and is not exact equal$inis contains at least onegrades: [10], this is the exact equalt
Regex
- Start a regex pattern with
/pattern/,^indicates the word will start with it,$indicates the word will end with it. .*means allow any character in between\b\w+\bmatches words\bmeans word barrier\wis caracther\sis a space character
Projection
Projection is done in the second parenthesis, writing {field: 0} means all except this field, 1 means print this field, using 0 and 1 together in a single query is not allowed. You can also add a field by writing just the field name and value other than 1 or 0 to it, even if this field exists in the document, ex: db.users.find({}, {name: 1, message: "hello"}), this add the name field from the document and writes a new field named message that does not exist in the document.
NOTE: if the
id_is not explicitly suppressed in the projection parenthesis it will always be printed
db.users.find().sort({age: 1, name: 1}) will first sort by age then name, 1 means sort by ascending order. To sort by descending order use -1
Logical operations
- To negate a query use
$not: {}before the query function that we are going to use or as the call it a query document - To use a logical operations like “and” or “or” to get fields, start the query parameter with it, for ex:
db.users.find({$and: [{age: 10}, {age: 12}})alsodb.users.find({age: 10, grade: 20})is an implicit and. - Only
and,nor,orare top level starters the others are called field level operators $notis not a top level function like AND and OR, you use it to negate an a expression example:{name: $not: {$eq: "Ahmed"}}, and its parameter is a function or a regex.$neis the same for$not: {$eq}
NOTE: Implicit ANDs are a bit tricky, if we do
{age: 12, age: 17}the database will only consider the last one, but it an implicit and with function operations is like{$age: {$gte: 15, $lte 17}}here we are looking for ages more than 15 and less than 17
Important Functions
-Doing db.students.find({grades: {$size: 2}}) prints the student that has 2 grades in it array, while writing db.students.find({}, {grades: {$size: "$grades"}) Will write the size of the grades array in the collection.
NOTE: We put
$before fields in our collection in places where we can create fields, like in projection.
Mathematical operations in mongo

Aggregation
The output of the current stage is the input of the next stage

NOTE: to make a complex group, make the group a an object like
_id: {name: $as, t: $smt}
- to output into another collection use the pipeline
{$out: collection name} - to merge into a collection use
{$merge: {into: "name", on: "_id", whenMatched: "replace", whenNotMatched: "insert"}} $countis a pipeline
Geospatial Queries
To operate on data that is in 3d space, create a field that has the following fields:
{
type: <Geospatial object type>,
coordinates: []
}
// You can instalntly put the coordinats and the default will be pointTo do queries on this data with geospatial function an index must be added with the coordinate types: db.students.createIndex({"name": "2dsphere" for earth like sphere coordinates or "2d" for 2d space})
$geoWithin does not require index, but it will improve performance
// geoWithin takes a a shape opertotor or geometry function
{
$geoWithin:
$center: [[], degree] or polygon or $geometry: {type: , coordinates}
same with geoINtersects or nearSphere: [cooeds], $maxDistance
}nearSphere → distance
geoWithin → degrees
Deletion
db.collections.drop()deletes the whole collection and the indices, .remove({}) deletes the documents and can take a query argument. There is no such thing as delete(). Note that in remove we will have to brackets even if they are empty, else it is an error.
NOTE: There is
deleteOneanddeleteManythough
Updating
db.students.update({}, {})updates only the first entry, useupdateManyto update all, there is no function asupdateAll
Indices
- Indices reduce the complexity of the search
- I can’t have more than two text indices in the collection
- to create text indices use db.collection.createIndex({field1: “text”, field2: “text”}), they cannot be created seperately, Ill get an error
- to drop it do db.collection.dropIndex(“field1_text”)
- there is also getIndexes
- to search text use
{$text: {$search: "dsd"}}
From slides
u can also renameCollection and getCollectionNames

I can put {out, query, finalize} in the end
