MongoDB Flashcards
What is MongoDB
No SQL or non-relational database
Collection in MongoDB
A group of documents in MongoDB. Do not enforce a rigid schema
Document in MongoDB
a record stored in JSON-like format, and each document in the collection has a different structure
Visualize a collection in MongoDB
[
{
“name”: “Bob”,
“age”: 30
},
{
“name”: “Charlie”,
“age”: 35,
“hobbies”: [“cycling”, “hiking”]
}
]
Visualize a document in MongoDB
{“name”: “Alice”, “age”:25}
documents are stored ({..})
Query using .find() in MongoDB
db.collection.find()
General querying; returns matching documents.
$gt in MongoDB
db.collection.find({“field_name”: {“$gt”: data}})
Matches values greater than a specified value.
Update a single document in a collection in MongoDB
$set
db.collection.updateOne({“filter_by_fieldname”:”filter_value”},{“$set”:{“age”:modified_data}})
Delete a single document in MongoDB
.deleteOne
db.collection.deleteOne({“filter_name”:”condition”})
Delete multiple documents in MongoDB
.deleteMany
db.users.deleteMany({“age”:{“$lt”:30}}) // Deletes users younger than 30
Delete a collection in MongoDB
.drop()
db.collection.drop()
Import mongoDB into python in MongoDB
Connect to MongoDB
from pymongo import MongoClient
client = MongoClient(“mongodb://localhost:27017/”)
Connect to or access a database in MongoDB
Insert a single document
db = client.name_of_database
#create or access the collection
my_collection = db.name_of_collection
Insert a document in MongoDB
name_of_collect.insert_one({“name”: “Lorraine”, “age”: “Forever Young”})
in MongoDB .find() vs. .find_one()
.find() - Retrieves all documents that match a query.
.find_one() - Retrieves only the first matching document.
Get the field names present in a collection in MongoDB
myCollection_fields = mycollection.keys()
print(myCollection_fields)
Save a list of the fields of the collection in MongoDB
myCollection_fields = list(mycollection.keys())
print(myCollection_fields)
How do you filter? in MongoDB
db.collection.find(
{“field_name”: {“$operator”: “value”}}, # Filter condition
{“field1”: 1, “field2”: 1, “_id”: 0} # Projection (optional)
).sort(“field_to_sort”, 1).limit(10) #create a filter variable with constraints
filter_doc = {
‘gender’:’male’,
‘surname’: ‘%A’
}
Value in a range in MongoDB
$in: <list></list>
db.myCollection.count_documents({
‘Countries’: {
‘$in’: [‘France’,’USA’]
}
})
Create a filter criteria to count laureates who died (“diedCountry”) in the USA (“USA”). Save the document count as count. in MongoDB
Save the count of these laureates
Create a filter for laureates who died in the USA
criteria = {“diedCountry”: “USA”}
count = db.laureates.count_documents(criteria)
print(count)
$ne in MongoDB
criteria = { “diedCountry”: “USA”,
“bornCountry”: { “$ne”: “USA”},
}
How many laureates were born in “USA”, “Canada”, or “Mexico”? Save a filter as criteria and your count as count. in MongoDB
Count them and save the count
Save a filter for laureates born in the USA, Canada, or Mexico
criteria = { “bornCountry”:
{ “$in”: [“Canada”, “Mexico”, “USA”]}
}
count = db.laureates.count_documents(criteria)
print(count)
in MongoDB $exists
Check for the existence of an field regardless of content
db.collection_name.count_documents(“prizes.0”: {“$exists”:True}})
fields.# in MongoDB
“field_name.0”
References a field and the documents and the position of the element with dot notation