Skip to content

Instantly share code, notes, and snippets.

@greatsharma
Created June 22, 2020 05:14
Show Gist options
  • Save greatsharma/2571d95eaaef4c5160f4686c94d4c210 to your computer and use it in GitHub Desktop.
Save greatsharma/2571d95eaaef4c5160f4686c94d4c210 to your computer and use it in GitHub Desktop.
Some important mongo commands
* docker run --name mongo-container -d -p 27017:27017 -v data:/data/db mongo
* mongo remote_ip [connect to a remote monogodb using mongoshell]
* mongo --host mongodb0.example.com --port 27017
* sql:table::nosql:collection
* sql:datarow::nosql:document
* show dbs [show all dbs]
* db [db currently connected to]
* show collections
* use db_name [create new_db]
* db.dropDatabase() [drop database]
* db.creatCollection("collection_name")
* db.stats() [db details]
* db.collectionName.drop() [delete collection from db]
* db.collectionName.insert() [creates collection if not exists]
* db.collectionName.find().pretty() [search collection]
* db.collectionName.updateOne(
{ "item" : "paper" }, // specifies the document to update
{
$set: { "size.uom" : "cm", "status" : "P" },
$currentDate: { "lastModified": true }
}
)
* db.collectionName.find({"released": {$lt:2010}}).pretty()
* db.movies.find({"released": {$gt:2010}}).pretty()
* db.movies.find({"released": 2008}).pretty()
* db.collectionName.find({}, {"_id": 0, "title": 1, "language": 1}).pretty()
* db.collectionName.count() [count # documents]
* db.collectionName.count(
... {"released": {$gt: 2010}}
... )
* db.collectionName.deleteMany(
... {"released": {$lt: 2010}}
... )
* db.collectionName.deleteMany() [delete all documents]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment