CRUD Flashcards
How do you add a document into a collection?
db.collection_name.insertOne({obj});
How do you add multiple documents in a collection?
db.collection_name.insertMany([{obj}, {obj}]);
How do you query for a document or documents?
db.collection_name.find({attr: val});
How do you do an AND query?
db.collection_name.find({attr: val, attr: val});
How do you do an OR query?
db.collection_name.find({$or: [{attr: val}, {attr: val}]});
How do you do a query containing less than or greater than parameters?
db.collection_name.find({attr: {$param: val}});
Name the parameters for comparisons in MongoDB?
lte => less than or equal to, lt => less than, gt => greater than, gte => greater than or equal to
How do you return only specific attributes for a document in a query?
db.collection_name.find({query}, {attr: 1, attr: 1});
How do you update a single document?
db.collection_name.updateOne({find_query}, {obj_to_update});
How do you update multiple documents?
db.collection_name.updateMany({find_query}, {obj_to_update});
How do you replace a document(s)?
db.collection_name.replaceOne/Many({find_query}, {obj_to_update});
How do you delete a document(s)?
db.collection_name.deleteOne/Many({find_query});