Skip to content

Instantly share code, notes, and snippets.

@nicokosi
Last active August 6, 2020 08:34
Show Gist options
  • Save nicokosi/08ffe9389068683632c1f5e1c3db7635 to your computer and use it in GitHub Desktop.
Save nicokosi/08ffe9389068683632c1f5e1c3db7635 to your computer and use it in GitHub Desktop.
My own MongoDB query cheat sheet
// Connect *quietly* to a database with user/password authentication:
mongo $host/$database --quiet --username $user --password $password
// List databases:
show databases
// Change database:
use $database
// List collections for current database:
show collections
// Number of documents in a collection:
db.$collection.count()
// Return a raw document in a collection:
db.$collection.findOne()
// Return a formatted ("pretty") document from a collection
db.$collection.findOne().pretty()
// Display the last document per a given field, in a collection:
db.$collection.find().sort({$field: -1}).limit(1)
// Filter output fields using a projection:
db.$collection.find({}, {$field: 1})
// Filter data by field value:
db.$collection.find({$field: $value})
// Combine filter on data and fields:
db.drvs.find(
// Only return documents having this field:
{ linkedIds: ["h015n001", "h015n003"] },
// Only return two fields
{ _id: 1, name: 1 } )
// find "like"
db.monographies.find({ nom_complet: /dopliprane/ })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment