04 - MongoDB Data Structures Flashcards
Explain in human terms
MONGO_HOST = “172.31.90.91”
replace with your server’s private IP address
Explain in human terms
MONGO_DB = "my_db"
name of database in the MongoDB server that you want to use
db = client[MONGO_DB]
- If the database with the specified name does not exist, it gets created.
- Note that the database gets created after data is added to it
movies = db[“movies”]
- If the collection does not exist yet, this will create a collection named movies
- in the database that is currently being used
Explain in human terms
result = movies.insert_one( { “title” : “Jaws” } )
- inserts one document in the movies collection
- will return an InsertOneResult object
result.inserted_id
- to get the ObjectID
- the inserted_id property holds the id of the inserted document
What happens when you don’t assign an id to a document?
MongoDB will create one automatically
Will this work? Why/why not?
movies.insert_one( { "_id" : [ "Star Wars", "The Empire Strikes Back", "Return of the Jedi" ] } )
No_id
shouldn’t be an array
Will this work? Why/why not?
movies.insert_one( { “Star Wars” } )
no
wrong format
Will this work? Why/why not?
movies.insert_one( { "_id" : "Star Wars" } )
Yes
* not an array
* follows format
When will this not work?
result = movies.insert_many( [ { "_id" : "Batman", "year" : 1989 }, { "_id" : "Home Alone", "year" : 1990 }, { "_id" : "Ghostbusters", "year" : 1984 }, { "_id" : "Ghostbusters", "year" : 1984 }, { "_id" : "Lord of the Rings", "year" : 2001} ] ) result.inserted_ids
the second ghostbusters
What happens when an insert fails?
Ordered vs Unordered
Ordered
* will stop processing inserts upon encountering an error.
Unordered
* will still attempt all of the others.
* inserts may be executed in a different order than you specified.
How to specify an insert is unordered?
ordered = False
Define
Field
key-value pair, a MongoDB document can have several fields
Differentiate
Field name and key
same
List
ways to delete
4
movies.delete_one() movies.delete_many() movies.drop() or db.drop_collection("movies")
What will be the result?
movies.find({"_id" : ObjectId("5b963e71175f8feddc46f1c3")}, {"title":0})
movie with specified id, no title
What will be the result?
movies.find({}, {"title":1})
gets all movies and shows title
What will the results of these be?
movies.find() movies.find({})
both the same, gets all
When you use find(), MongoDB returns a pointer to the result set called?
cursor
Is cursor.hasNext() used in both mongosh and PyMongo?
mongosh only
How to sort
testcol.find().sort("a",1)
1 for ascending, -1 for descending
How to sort multiple keys?
testcol.find().sort([("a",1),("b",-1)])
Limit result & skip some results
testcol.find().sort("a",1).limit(5).skip(5)
Define
$lt
Exists and is less than
Define
$lte
Exists and is less than or equal to
Define
$gt
Exists and is greater than
Define
$gte
Exists and is greater than or equal to
Define
$eq
Equal to
Define
$ne
Does not exist or does but is not equal to
Define
$in
Exists and is in a set
$nin
Does not exist or is not in a set