Skip to content

Instantly share code, notes, and snippets.

@javikalsan
Last active July 23, 2016 19:58
Show Gist options
  • Save javikalsan/bf892f213d6e4f2ae442 to your computer and use it in GitHub Desktop.
Save javikalsan/bf892f213d6e4f2ae442 to your computer and use it in GitHub Desktop.
Mongodb queries for mine twitter collections
# general #
use database; db.createCollection( 'collection' ); # create database with name database with empty collection named collection
use database; db.runCommand( { dropDatabase: 1 } ); or use database; db.dropDatabase(); # remove database
db.twits.remove(); # empty collection
db.twits.find().count(); # count all twits in the collection
mongodump -d database -c collection -o collection.dump # dump entire collection
mongorestore -d database -c collection myfoo.dump/database/collection.bson # restore dumped collection
mongoexport --db database --collection collection -q "{}{ "_id":0, "date":1, "lon":1, "lat":1 }" --csv -f "date,lon,lat" --out results_query.csv # export to CSV a query
# basic search and display #
db.twits.find( { "coordinates" : { $ne: null } } ).count(); # count all twits with coordinates
db.twits.find( { "coordinates" : null } ).count(); # count all twits with coordinates null
db.twits.find( {},{ "coordinates":1, "geo":1 } ); # show all twits but only display coordinates and geo fields
# geo search #
db.twits.find( { "coordinates" : { $ne: null } }, { "_id":0, "coordinates.coordinates": 1, "text": 1, "entities.hashtags.text" : 1 } ).pretty(); # search twits with coords and display them, the text and hashtags used
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment