1

Click here to load reader

Script for Spatial Mongo

Embed Size (px)

DESCRIPTION

This is the script I use during my into to spatial mongo.

Citation preview

Page 1: Script for Spatial Mongo

#commands for adding spatial data12

#Take a look at the file first3less parkcoord.json4#get the file on the server5scp parkcoord.json [email protected]:parks/data/6

7#ssh into the machine8ssh [email protected]

10#import into mongo11mongoimport -d parks -c opencloud --type json --file parks/data/parkcoord.json -h $OPENSHIFT_NOSQL_DB_HOST -u admin -p 12

13#open the mongo shell14mongo -u admin -p $OPENSHIFT_NOSQL_DB_HOST/parks15

16#build the index17db.opencloud.ensureIndex({"pos":"2d"});18

19#Now some queries20#simple spatial21db.opencloud.find({"pos" : { "$near" : [-37, 41]}});22

23#spatial and text query using regex24db.opencloud.find( { Name : /lincoln/i, pos : { $near : [-37,41] }} );25

26#geonear TODO27db.runCommand({ geoNear : "opencloud", near : [-37,41], num : 10 });28

2930

#create a new collection from scratch31db.createCollection("clouduserloc");32db.clouduserloc.ensureIndex( { pos : "2d" } );33

34#insert a new record35db.clouduserloc.insert({ "created" : new Date(), "Notes" : 'just landed', "pos" : [-76.7302 , 25.5332 ] })36!37#quick query to make sure it worked38db.clouduserloc.find( { pos : { $near : [-37,41] } } )39

40#add a second document closer to query point41#this is an upsert - since we don't pass in the _id it creates a new record42db.clouduserloc.save({ created : new Date(), Notes: 'that was a big step', pos : [-37.7302 , 40.5332 ]});43

44#one way to update original document45myDoc = db.clouduserloc.findOne({_id : ObjectId("ID for the first object")});46myDoc.Notes = "really the landing";47db.clouduserloc.save(myDoc);48

49