Skip to content

Instantly share code, notes, and snippets.

@rolandovillca
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rolandovillca/d5e576cb7f78acda4de8 to your computer and use it in GitHub Desktop.
Save rolandovillca/d5e576cb7f78acda4de8 to your computer and use it in GitHub Desktop.
MONGO DB QUERIES
// Count an array that is embedded into document.
// Document:
{name: 'Pepito', tags: ['A', 'B', 'C', 'D']}
//Mongodb query:
db.employees.aggregate(
{$match: {name : "Pepito"}},
{$unwind: "$tags"},
{$group: {_id: null, number: {$sum: 1 }}}
)
// OR
db.employees.aggregate(
[
{
$project:
{
count_tags: {$size: 'tags'}
}
}
]
)
// How to import a dump?
// 1. Using default location for db '/data/db'
// Go to file where 'dump' directory is available and execute following command:
$ mongorestore
// 2. Using custom location for db (using '/path/to/pets' instead of 'using /data/db'
$ mongorestore -d pets /path/to/pets
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment