Last active
August 29, 2015 14:16
-
-
Save rolandovillca/d5e576cb7f78acda4de8 to your computer and use it in GitHub Desktop.
MONGO DB 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
// 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