This document contains the slides from a presentation on building spatial back-ends with Node.js and MongoDB. The presentation covers loading spatial data into MongoDB, running spatial queries, and deploying a MongoDB and Node.js powered spatial app to OpenShift in about 30 minutes. It provides code examples and steps for connecting to MongoDB on OpenShift, importing data, adding a 2D index, and performing spatial queries to find points near a location or by name.
MongoDB .local Paris 2020: Upply @MongoDB : Upply : Quand le Machine Learning...
Building spatial back-ends with node.js and MongoDB
1. 3/11/13 reveal.js - The HTML Presentation Framework
BUILDING SPATIAL
BACK-ENDS
with
node.js
and
MongoDB
bit.ly/WUXJWD
ryanjarvinen.com/presentations/parks/#/ 1/45
2. 3/11/13 reveal.js - The HTML Presentation Framework
presented by
ryan jarvinen / @ryanj
ryanjarvinen.com/presentations/parks/#/ 2/45
3. 3/11/13 reveal.js - The HTML Presentation Framework
Open Platform Evangelist
at
Red Hat
ryanj@redhat.com
ryanjarvinen.com/presentations/parks/#/ 3/45
4. 3/11/13 reveal.js - The HTML Presentation Framework
AGENDA
Learn some basic spatial
Load spatial data into MongoDB
Run a query or two
Go live with a Mongo-powered spatial app in ~30 mins
ryanjarvinen.com/presentations/parks/#/ 4/45
5. 3/11/13 reveal.js - The HTML Presentation Framework
CODE DU JOUR
https://github.com/openshift/openshift-mongo-node-
express-example
Signup with Code - MONGOLA2013
ryanjarvinen.com/presentations/parks/#/ 5/45
6. 3/11/13 reveal.js - The HTML Presentation Framework
THE CLOUD
IS
A FREE-MARKET FOR ONLINE SERVICES
ryanjarvinen.com/presentations/parks/#/ 6/45
7. 3/11/13 reveal.js - The HTML Presentation Framework
THE CLOUD LANDSCAPE
ryanjarvinen.com/presentations/parks/#/ 7/45
8. 3/11/13 reveal.js - The HTML Presentation Framework
ryanjarvinen.com/presentations/parks/#/ 8/45
9. 3/11/13 reveal.js - The HTML Presentation Framework
THE BIG PICTURE
ryanjarvinen.com/presentations/parks/#/ 9/45
10. 3/11/13 reveal.js - The HTML Presentation Framework
ryanjarvinen.com/presentations/parks/#/ 10/45
11. 3/11/13 reveal.js - The HTML Presentation Framework
OPENSTACK
IS OPEN
and
FOCUSED ON IAAS
ryanjarvinen.com/presentations/parks/#/ 11/45
12. 3/11/13 reveal.js - The HTML Presentation Framework
PUBLIC CLOUD +
PRIVATE CLOUD =
HYBRID CLOUD
ryanjarvinen.com/presentations/parks/#/ 12/45
13. 3/11/13 reveal.js - The HTML Presentation Framework
OPENSHIFT IS…
OPEN
ryanjarvinen.com/presentations/parks/#/ 13/45
14. 3/11/13 reveal.js - The HTML Presentation Framework
OPEN CARTRIDGE
FORMAT
ryanjarvinen.com/presentations/parks/#/ 14/45
15. 3/11/13 reveal.js - The HTML Presentation Framework
OPEN SOURCE
OPENSHIFT ORIGIN
ryanjarvinen.com/presentations/parks/#/ 15/45
16. 3/11/13 reveal.js - The HTML Presentation Framework
HOSTING OPTIONS
DIY
OPENSHIFT ONLINE
OPENSHIFT ENTERPRISE
ryanjarvinen.com/presentations/parks/#/ 16/45
17. 3/11/13 reveal.js - The HTML Presentation Framework
DEVELOPER PREVIEW
Make sure that you sign up for a free OpenShift account
http://openshift.redhat.com/
Free-as-in-beer
Free-as-in-freedom
512 MB RAM / 1 GB storage per gears (3 free)
Need more resources, just ask!
REALLY a Developer Preview
ryanjarvinen.com/presentations/parks/#/ 17/45
18. 3/11/13 reveal.js - The HTML Presentation Framework
GETTING STARTED WITH OPENSHIFT
PRE-REQUISITES:
ruby, rubygems
git
ryanjarvinen.com/presentations/parks/#/ 18/45
19. 3/11/13 reveal.js - The HTML Presentation Framework
INSTALLING THE 'RHC' CLIENT TOOL
sd gmisalrc
uo e ntl h
and
install node.js
ryanjarvinen.com/presentations/parks/#/ 19/45
20. 3/11/13 reveal.js - The HTML Presentation Framework
HELP WITH CLIENT TOOLS
Openshift Getting Started guide
RHC client tools installation
ryanjarvinen.com/presentations/parks/#/ 20/45
21. 3/11/13 reveal.js - The HTML Presentation Framework
CONFIGURING YOUR DEV ENVIRONMENT
rcstp
h eu
sets your openshift email address
verifies your ssh key configuration
selects your application namespace
ryanjarvinen.com/presentations/parks/#/ 21/45
22. 3/11/13 reveal.js - The HTML Presentation Framework
MONGODB SPATIAL
ryanjarvinen.com/presentations/parks/#/ 22/45
23. 3/11/13 reveal.js - The HTML Presentation Framework
SPATIAL NOTES
MongoDB currently supports the following types of
spatial queries:
1. Near
2. Containment
MongoDB GIS summary:
http://www.mongodb.org/display/DOCS/Geospatial+Indexing
ryanjarvinen.com/presentations/parks/#/ 23/45
24. 3/11/13 reveal.js - The HTML Presentation Framework
MONGODB
ASSUMPTIONS
Assumes coords. are between -180 and 180
Can handle any 2D coordinates
Has methods to handle curvature of the earth
(spherical)
ryanjarvinen.com/presentations/parks/#/ 24/45
25. 3/11/13 reveal.js - The HTML Presentation Framework
MAKING IT WORK
1. Put your data in an array
{lc:[5 ,3 ]}/SGETDOTO
o 0 0 /UGSE PIN
{lc:{x:5 ,y:3 }}
o 0 0
{lc:{fo:5 ,y:3 }}
o o 0 0
{lc:{ln:4.307 lt 7.994}}
o o 0793, a: 3926
2. Add a 2d index to your collection
d.lcsesrIdx {lc:"d })
bpae.nuene( o 2"
ryanjarvinen.com/presentations/parks/#/ 25/45
26. 3/11/13 reveal.js - The HTML Presentation Framework
GEARING UP A NEW APP ON OPENSHIFT
rcapcet APNM APCRRDE
h p rae P_AE P_ATIG
or
rcapcet prsndj mnob22-fo-oehts/gtu
h p rae ak oes ogd-. -rmcd=tp:/ihb
.o/pnhf/pnhf-og-oeepeseapegt
cmoesitoesitmnond-xrs-xml.i
ryanjarvinen.com/presentations/parks/#/ 26/45
27. 3/11/13 reveal.js - The HTML Presentation Framework
ApiainOtos
plcto pin
----------
---------
Nmsae raj
aepc: yn
Crrde:ndj-.,mnob22
atigs oes06 ogd-.
Suc Cd:hts/gtu.o/pnhf/pnhf-og-oee
ore oe tp:/ihbcmoesitoesitmnond-
xrs-xml.i
peseapegt
Ga Sz: dfut
er ie eal
Saig
cln: no
Cetn apiain'ak'..dn
raig plcto prs . oe
Wiigfryu DSnm t b aalbe..dn
atn o or N ae o e vial . oe
DwlaigteapiainGtrpstr ..
onodn h plcto i eoioy .
Coigit 'ak'.
lnn no prs..
ryanjarvinen.com/presentations/parks/#/ 27/45
28. 3/11/13 reveal.js - The HTML Presentation Framework
prs@ht:/ak-yn.hlu.o/(ud 2980b84688
ak tp/prsrajrcodcm ui: 5134fd0e9
0ae4030
21626a)
--------------------------
-------------------------
Cetd
rae: 1:3P
02 M
Ga Sz:sal
er ie ml
GtUL
i R:
sh/2980b8468821626a@ak-yn.hlu.o//
s:/5134fd0e90ae4030prsrajrcodcm~g
i/ak.i/
tprsgt
SH
S: 2980b8468821626a@ak-yn.hlu.
5134fd0e90ae4030prsrajrcod
cm
o
ndj-. (oej 06
oes06 Nd.s .)
------------
------------
RSL:
EUT
Apiainprswscetd
plcto ak a rae.
ryanjarvinen.com/presentations/parks/#/ 28/45
29. 3/11/13 reveal.js - The HTML Presentation Framework
Adn mnob22t apiain'ak'..Sces
dig ogd-. o plcto prs . ucs
mnob22(ogD NSLDtbs 22
ogd-. MnoB oQ aaae .)
--------------------
--------------------
Cneto UL mnob/1761219207
oncin R: ogd:/2..0.2:71/
Dtbs Nm: prs
aaae ae ak
Pswr:
asod TxwiZG
5JCDYn
Uenm:
srae amn
di
RSL:
EUT
Addmnob22t apiainprs
de ogd-. o plcto ak
ryanjarvinen.com/presentations/parks/#/ 29/45
30. 3/11/13 reveal.js - The HTML Presentation Framework
SUCCESS!
You now have a basic node.js app up an running on
OpenShift!
Your gear is now configured with:
it's own git repo
it's own web server
MongoDB
ssh access
logging
publicly accessible DNS
ryanjarvinen.com/presentations/parks/#/ 30/45
31. 3/11/13 reveal.js - The HTML Presentation Framework
BUILDING YOUR APP
c prs
d ak
ryanjarvinen.com/presentations/parks/#/ 31/45
32. 3/11/13 reveal.js - The HTML Presentation Framework
SETTING UP MONGODB ON OPENSHIFT
rccrrdeadmnob22
h atig d ogd-.
ryanjarvinen.com/presentations/parks/#/ 32/45
33. 3/11/13 reveal.js - The HTML Presentation Framework
INSTALLING DEPENDENCIES
Using Node's npm package manager :
nmisalmnos-
p ntl ogj S
Include the - flag in order to save this dependency to
S
your application's p c a e j o file.
akg.sn
http://npmjs.org/package/mongojs/
ryanjarvinen.com/presentations/parks/#/ 33/45
34. 3/11/13 reveal.js - The HTML Presentation Framework
COMMITTING YOUR CHANGES
gtadpcaejo sre.s
i d akg.sn evrj
Commit your changes locally
gtcmi - 'digmnosnmdpnec,iiilzn mnos
i omt m adn ogj p eedny ntaiig ogj
lbay adn abscAI
irr, dig ai P'
ryanjarvinen.com/presentations/parks/#/ 34/45
35. 3/11/13 reveal.js - The HTML Presentation Framework
DEPLOYMENT
gtps
i uh
Done!Your spatial app should now be live IN THE CLOUD!
ryanjarvinen.com/presentations/parks/#/ 35/45
36. 3/11/13 reveal.js - The HTML Presentation Framework
ROCKMONGO ON OPENSHIFT
rccrrdeadrcmno11
h atig d okog-.
(optional)
ryanjarvinen.com/presentations/parks/#/ 36/45
37. 3/11/13 reveal.js - The HTML Presentation Framework
CONNECTING TO MONGODB
vrmnos=rqie'ogj';
a ogj eur(mnos)
vrcneto_tig=btnm;
a oncinsrn o_ae
i(rcs.n.PNHF_OGD_BPSWR)
fpoesevOESITMNOBD_ASOD{
cneto_tig=poesevOESITMNOBD_SRAE+
oncinsrn rcs.n.PNHF_OGD_BUENM
""+
:
poesevOESITMNOBD_ASOD+""+
rcs.n.PNHF_OGD_BPSWR @
poesevOESITMNOBD_OT+''+
rcs.n.PNHF_OGD_BHS /
poesevOESITAPNM;
rcs.n.PNHF_P_AE
}
vrd =mnoscneto_tig [soeor')
a b ogj(oncinsrn, 'crbad];
ryanjarvinen.com/presentations/parks/#/ 37/45
38. 3/11/13 reveal.js - The HTML Presentation Framework
SSH TO THE CLOUD
FIND YOUR APP'S SSH ADDRESS
rcapso prs
h p hw ak
SSH IN TO YOUR OPENSHIFT GEAR
rcsh- APNM
h s a P_AE
ryanjarvinen.com/presentations/parks/#/ 38/45
39. 3/11/13 reveal.js - The HTML Presentation Framework
IMPORTING YOUR DATA
mnomot- prs- prpit -tp jo -fl $PNHF_
ogipr d ak c akons -ye sn -ie OESITR
EODRprcodjo - $PNHF_OGD_BHS - amn-
P_I/akor.sn h OESITMNOBD_OT u di p
$PNHF_OGD_BPSWR
OESITMNOBD_ASOD
ryanjarvinen.com/presentations/parks/#/ 39/45
40. 3/11/13 reveal.js - The HTML Presentation Framework
ADDING A 2D INDEX
CONNECT TO MONGODB CLI
mno
og
SELECT YOUR COLLECTION
ueprs
s ak
ADD THE 2D INDEX
d.akonsesrIdx{ps:2")
bprpit.nuene("o""d};
ryanjarvinen.com/presentations/parks/#/ 40/45
41. 3/11/13 reveal.js - The HTML Presentation Framework
SPATIAL QUERIES
FINDING POINTS NEAR …
d.akonsfn("o":{"na":[3,4]};
bprpit.id{ps $er -7 1})
FIND BY NAME (REGEX)
d.akonsfn({Nm :/icl/,ps:{$er:[3,1
bprpit.id ae lnoni o na -74]
} )
} ;
GEONEAR
d.uCmad{goer:"akons,na :[3,1,nm:1
brnomn( eNa prpit" er -74] u 0
};
)
ryanjarvinen.com/presentations/parks/#/ 41/45
42. 3/11/13 reveal.js - The HTML Presentation Framework
ADDING CHECK-INS
CREATE A NEW COLLECTION FROM SCRATCH
d.raeolcin"hci";
bcetCleto(cekn)
d.hci.nuene({"o":"d })
bceknesrIdx ps 2" ;
INSERT A NEW RECORD
d.hci.net{"rae":nwDt(,"oe":'utlne
bceknisr( cetd e ae) Nts js add
' "o":[7.32,2.32]}
, ps -670 553 )
QUICK QUERY TO MAKE SURE IT WORKED
d.hci.id {ps:{$er:[3,1 }})
bceknfn( o na -74]
ryanjarvinen.com/presentations/parks/#/ 42/45
43. 3/11/13 reveal.js - The HTML Presentation Framework
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
d.hci.ae{cetd:nwDt(,Nts 'htwsabgse
bceknsv( rae e ae) oe: ta a i t
p,ps:[3.32,4.32])
' o -770 053 };
ONE WAY TO UPDATE ORIGINAL DOCUMENT
mDc=d.hci.idn(_d:OjcI(5108af52806
yo bceknfnOe{i betd"03de863e32b
c"};
1))
mDcNts="elytelnig;
yo.oe ral h adn"
d.hci.aemDc;
bceknsv(yo)
ryanjarvinen.com/presentations/parks/#/ 43/45
44. 3/11/13 reveal.js - The HTML Presentation Framework
CONCLUSIONS
1. Spatial on MongoDB is easy and fun
2. You can now build your own FourSquare or other
checkin application.
3. You can also build your own field data entry system.
4. You can build and deploy your application quickly
without having to think about infrastructure.
ryanjarvinen.com/presentations/parks/#/ 44/45
45. 3/11/13 reveal.js - The HTML Presentation Framework
Thanks for following along!
--ryanj
Signup Code: MONGOLA2013 /
ryanjarvinen.com/presentations/parks/#/ 45/45