Skip to content

Instantly share code, notes, and snippets.

@isaklafleur
Last active April 25, 2017 11:21
Show Gist options
  • Save isaklafleur/6950a5fad0ed08922968b54126bc6480 to your computer and use it in GitHub Desktop.
Save isaklafleur/6950a5fad0ed08922968b54126bc6480 to your computer and use it in GitHub Desktop.
MongoDB Cheat Sheet
// use mongoimport to import a data in JSON format into the database
$ mongoimport --db pacman --collection enemies --file pacman.json
$ mongoimport --db restaurants --collection restaurants --drop --file ~/downloads/primer-dataset.json
// Insert one document in the database
$ db.enemies.insertOne({ name: 'pacman' });
// can use javascript in MongoDB
$ const t = { name: 'Isak' }
$ db.enemies.insertOne(t);
// Insert many documents in the database, by passing an array of objects.
$ db.enemies.insertMany([{ name: 'Isak1' },{ name: 'Isak2' }]);
// find documents and only display part of the document
$ db.collection.find( query_filter, projection );
// find the restaurant by an id
$ db.restaurants.find({ restaurant_id: '30112340' }).pretty();
// find a restaurant by a name
db.restaurants.find({ name: 'May May Kitchen' }).pretty();
// find a restaurant by type of cuisine
$ db.restaurants.find({ cuisine: 'Tapas' }).pretty();
// find all restaurants with zipcode (inside the object address)
db.restaurants.find({ 'address.zipcode': '11208' }).pretty();
// find all restaurants with score >= 70
db.restaurants.find({ grades: { $elemMatch: { score: { $gte: 70 } } } }).pretty();
// find all restaurants in Brooklyn that have a score greater than 80
db.restaurants.find({ $and: [{grades: { $elemMatch: { score: { $gte: 80 } } } }, { borough: 'Brooklyn' }] }).pretty();
// find all restaurants with Chilean or Czech cuisine.
db.restaurants.find({ $or: [ { cuisine: 'Chilean' }, { cuisine: 'Czech' }]});
// find all restaurants with grade A in second position of the array.
db.restaurants.find({ "grades.1.grade": "A" });
// find all restaurants with grades A or B.
db.restaurants.find({ $or: [{ grades: { $elemMatch: { grade: 'A' } } }, { grades: { $elemMatch: { grade: 'B' } } }] });
// find all restaurants that have a review made in 16-09-2014.
db.restaurants.find({ grades: { $elemMatch: { date: ISODate('2014-09-16T00:00:00.000Z') } } });
// find all restaurant their cousine is tapas ordered by name in ascending order.
db.restaurants.find({ cuisine: 'Tapas' }).sort({ cuisine: 1 });
// How many restaurants have been graded after 01-01-2015?
db.restaurants.count({ grades: { $elemMatch: { date: { $gte: ISODate('2015-01-01T00:00:00.000Z') } } } });
// examples using projection to only display a part of the document
$ db.enemies.find({ name: 'Clyde' }, { name:1, _id:0 });
$ db.employees.find({}, { name: 1, _id:0 });
$ db.employees.find({ name: 'Steve' }, { name: 1, _id:0 });
$ db.employees.find({ age: { $gt: 30 } }, { name: 1, _id:0 });
$ db.employees.find({ 'phone.ext': '2143' }, { name: 1, _id:0 });
// Update ONE document
$ db.employees.updateOne({ name: "Martin"}, { $set: { "phone.ext": 1234 }});
$ db.employees.updateOne({ name: "John"}, { $set: {age: 100 }});
// Update many documents
$ db.employees.updateMany( { "age": { $gte: 30 }}, { $set: { "favorites.writer": "Cervantes" }});
// Replace one document with another document
const susan = {
"name" : "Susan",
"age" : 25,
"phone" : { "personal" : "555-223-223", "work" : "555-421-426", "ext" : 4240 },
"privileges" : "user",
}
$ db.employees.replaceOne({ "name": "Martin"}, susan);
// delete ONE document by its _id
$ db.employees.deleteOne( { _id: ObjectId("583ea82c58613b64d5df8405") });
// delete many documents based on a search criteria (all employees with the age above 30).
$ db.employees.deleteMany({ age : { $gte: 30 }});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment