Skip to content

Instantly share code, notes, and snippets.

@lcomino
Last active August 29, 2015 14:03
Show Gist options
  • Save lcomino/8dce9737f64a31e69a12 to your computer and use it in GitHub Desktop.
Save lcomino/8dce9737f64a31e69a12 to your computer and use it in GitHub Desktop.
Learning MongoDB
Insert data into a collection:
db.collection.save({a:value}); //This say save an document '{a:value}' to the collection
Find inserted values:
db.collection.find(); //This say show me all values into collection
//The shell result only displays 20 results at time, press 'it' for more results
db.collection.findOne(); //This say show me one document from this collection
db.collection.find().limit(n) // This say show me 'n' documents from this collection
db.collection.find({a:value}); //Find especifc value
db.collection.find({a:{$gt:5}})//Greater than
db.collection.find({a:{$lt:5}})//Less then
db.collection.find({a:{$ne:5}})//Not Equal to
db.collection.find({a:{$gte:5}})//Greater than or equal to
db.collection.find({a:{$in:[value1, value2, value3....]}}); //Exists in array..
db.collection.insert({name: value, array: [value1, value2]});
db.collection.update({name: value}, {name: newValue, array: [newValueArray]}); //overwriting method
db.collection.update({name: 'Sue'}, { $addToSet: {languages: ['ruby']}}); // without overwriting
db.collection.update({name: 'Cash'}, {'$set': {'age': 50} }); //add new field to cash
db.collection.update({name: 'Sue'}, {'$push': {'languages': 'ruby'} }); // push in array
db.collection.update({name: 'Sue'}, {'$pull': {'languages': 'scala'} }); // pull in array
db.collection.remove({name : 'Sue'}) //remove document from collection
db.collection.remove() //remove all documents from collection
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment