Script for Spatial Mongo

698 views
663 views

Published on

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

Published in: Technology, Business
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
698
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
4
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Script for Spatial Mongo

  1. 1. 1 #commands for adding spatial data 2 3 #Take a look at the file first 4 less parkcoord.json 5 #get the file on the server 6 scp parkcoord.json 0b49d9219f2847c6a236820959f9a@parks-spmongo.rhcloud.com:parks/data/ 7 8 #ssh into the machine 9 ssh 0b49d9219f2847c6a236820959f9a@parks-spmongo.rhcloud.com1011 #import into mongo12 mongoimport -d parks -c opencloud --type json --file parks/data/parkcoord.json -h $OPENSHIFT_NOSQL_DB_HOST -u admin -p1314 #open the mongo shell15 mongo -u admin -p $OPENSHIFT_NOSQL_DB_HOST/parks1617 #build the index18 db.opencloud.ensureIndex({"pos":"2d"});1920 #Now some queries21 #simple spatial22 db.opencloud.find({"pos" : { "$near" : [-37, 41]}});2324 #spatial and text query using regex25 db.opencloud.find( { Name : /lincoln/i, pos : { $near : [-37,41] }} );2627 #geonear TODO28 db.runCommand({ geoNear : "opencloud", near : [-37,41], num : 10 });293031 #create a new collection from scratch32 db.createCollection("clouduserloc");33 db.clouduserloc.ensureIndex( { pos : "2d" } );3435 #insert a new record36 db.clouduserloc.insert({ "created" : new Date(), "Notes" : just landed, "pos" : [-76.7302 , 25.5332 ] })37 !38 #quick query to make sure it worked39 db.clouduserloc.find( { pos : { $near : [-37,41] } } )4041 #add a second document closer to query point42 #this is an upsert - since we dont pass in the _id it creates a new record43 db.clouduserloc.save({ created : new Date(), Notes: that was a big step, pos : [-37.7302 , 40.5332 ]});4445 #one way to update original document46 myDoc = db.clouduserloc.findOne({_id : ObjectId("ID for the first object")});47 myDoc.Notes = "really the landing";48 db.clouduserloc.save(myDoc);49

×