Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Add Spatial Data to MongoDB
1. #commands for adding spatial data
#Take a look at the file first
#less parkcoord.json
#get the file on the server
scp parkcoord.json 30bb1262b1364026855e358a649f171b@parks-spmongo.rhcloud.com:app-root/data
#ssh into the machine
ssh 30bb1262b1364026855e358a649f171b@parks-spmongo.rhcloud.com
#import into mongo
mongoimport -d parks -c parkpoints --type json --file app-root/data/parkcoord.json -h
$OPENSHIFT_NOSQL_DB_HOST -u admin -p $OPENSHIFT_NOSQL_DB_PASSWORD
#open the mongo shell
mongo
#build the index
db.parkpoints.ensureIndex({"pos":"2d"});
#Now some queries
##simple spatial
db.parkpoints.find({"pos" : { "$near" : [-37, 41]}});
db.parkpoints.find({"pos" : { "$nearSphere" : [-37, 41]}});
##bbox query
db.parkpoints.find( { "pos": { "$within": { "$box": [ [-70, 30] , [-82, 40] ] } } } )
#spatial and text query using regex
db.parkpoints.find( { Name : /lincoln/i, pos : { $near : [-37,41] }} );
#geonear
db.runCommand({ geoNear : "parkpoints", near : [-37,41], num : 10 });
db.runCommand({ geoNear : "parkpoints", spherical: true, near : [-37,41], num : 10 });
db.runCommand({ geoNear : "parkpoints", spherical: true, distanceMultiplier: 3963.192, near : [-
37,41], num : 10 });
https://github.com/openshift/openshift-mongo-node-express-example
http://nodewsos-spdemo.rhcloud.com/ws/parks
2. #create a new collection from scratch
db.createCollection("checkin");
db.checkin.ensureIndex( { "pos" : "2d" } );
#insert a new record
db.checkin.insert({ "created" : new Date(), "Notes" : 'just landed', "pos" : [-76.7302 , 25.5332 ] })
#quick query to make sure it worked
db.checkin.find( { pos : { $near : [-37,41] } } )
#add a second document closer to query point
#this is an upsert - since we don't pass in the _id it creates a new record
db.checkin.save({ created : new Date(), Notes: 'that was a big step', pos : [-37.7302 , 40.5332 ]});
#one way to update original document
myDoc = db.checkin.findOne({_id : ObjectId("50130d8ea8f6532e83026bc1")});
myDoc.Notes = "really the landing";
db.checkin.save(myDoc);