Last active
January 22, 2018 19:59
-
-
Save JaykeOps/8ca97216feb86e9b9baa46c7b1de38ce to your computer and use it in GitHub Desktop.
Basic examples of MongoDB Queries
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
db.createUser({ | |
user: "Kusken", | |
pwd: ***, | |
roles: ["readWrite", "dbAdmin"] | |
}); | |
db.createCollection("customers"); | |
show collections | |
db.customers.insert({first_name: "John", last_name: "Doe"}); | |
db.customers.insert([ | |
{first_name: "Scubba", last_name: "Steve", Occupation: "Scubba Commando"}, | |
{first_name: "Tiger", last_name: "Mafia", Occuptation: "Commander", Location: "Unknown"} | |
]); | |
//Quries the customer collection | |
db.customers.find(); | |
db.customers.find().pretty(); | |
//The the second argument will overwrite the whole document | |
db.customers.update( | |
{first_name:"John"}, {first_name: "John", last_name: "Doe", occupation: "Vagabond", Location: "Seattle"} | |
); | |
//The second argument will patch the document | |
db.customers.update({first_name: "Tiger"}, {$set:{location: "Uganda"}}); | |
db.customers.update({first_name: "Tiger"}, {$set:{age: 33}}); | |
//Increment age | |
db.customers.update({first_name: "Tiger"}, {$inc:{age: 1}}); | |
//Removes the age field from the selected document | |
db.customers.update({first_name: "Tiger"}, {$unset:{age:1}}); | |
//When running update and not found - if upsert is true a new record will be inserted | |
db.customers.update({first_name: "Shrek"},{first_name: "Shrek", location: "Green Swamp"},{upsert:true}); | |
//Rename a field | |
db.customers.update({first_name: "Scubba"}, {$rename: {"last_name":"sure_name"}}); | |
//Remove all fields with the specified field value | |
db.customers.remove({first_name: "Shrek"}); | |
db.customers.find({first_name: "Scubba"}); | |
db.customers.find({$or:[{first_name: "Scubba"}, {last_name: "Mafia"}]}); | |
db.customers.insert( | |
[ | |
{first_name: "Marco", last_name: "Polo", age: 33}, | |
{first_name: "Nelson", last_name: "Mandela", age: 88}, | |
{first_name: "Donald", last_name: "Trump", age: 72}, | |
{first_name: "Barack", last_name: "Obama", age: 60} | |
] | |
); | |
db.customers.find({age:{$lt:70}}); | |
db.customers.find({age:{$gt:70}}); | |
//Sort by ascending | |
db.customers.find().sort({last_name:1}); | |
//Sort by descending | |
db.customers.find().sort({last_name:-1}); | |
//Count result | |
db.customers.find().count(); | |
//Limits returned results | |
db.customers.find().limit(3); | |
//forEach + print function | |
db.customers.find().forEach(function(doc){print("Customer Name: " + doc.first_name)}); | |
//Counts all documents were "Sweden" occupies the second index of the array | |
db.movieDetails.find({"countries.1": "Sweden"}).count() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment