SQL Database Design For Developers at php[tek] 2024
Script for Spatial Mongo
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.com
10
11 #import into mongo
12 mongoimport -d parks -c opencloud --type json --file parks/data/parkcoord.json -h $OPENSHIFT_NOSQL_DB_HOST -u admin -p
13
14 #open the mongo shell
15 mongo -u admin -p $OPENSHIFT_NOSQL_DB_HOST/parks
16
17 #build the index
18 db.opencloud.ensureIndex({"pos":"2d"});
19
20 #Now some queries
21 #simple spatial
22 db.opencloud.find({"pos" : { "$near" : [-37, 41]}});
23
24 #spatial and text query using regex
25 db.opencloud.find( { Name : /lincoln/i, pos : { $near : [-37,41] }} );
26
27 #geonear TODO
28 db.runCommand({ geoNear : "opencloud", near : [-37,41], num : 10 });
29
30
31 #create a new collection from scratch
32 db.createCollection("clouduserloc");
33 db.clouduserloc.ensureIndex( { pos : "2d" } );
34
35 #insert a new record
36 db.clouduserloc.insert({ "created" : new Date(), "Notes" : 'just landed', "pos" : [-76.7302 , 25.5332 ] })
37 !
38 #quick query to make sure it worked
39 db.clouduserloc.find( { pos : { $near : [-37,41] } } )
40
41 #add a second document closer to query point
42 #this is an upsert - since we don't pass in the _id it creates a new record
43 db.clouduserloc.save({ created : new Date(), Notes: 'that was a big step', pos : [-37.7302 , 40.5332 ]});
44
45 #one way to update original document
46 myDoc = db.clouduserloc.findOne({_id : ObjectId("ID for the first object")});
47 myDoc.Notes = "really the landing";
48 db.clouduserloc.save(myDoc);
49