SlideShare ist ein Scribd-Unternehmen logo
1 von 24
MongoDB Bangalore Conference
                                               Oct 26th 2012




Building a Location-based platform

    with MongoDB from Zero




            Raviteja Dodda
    Co-Founder & CTO, Pipal Tech Ventures
DelightCircle

• Offers, New Arrivals & Rewards
from 250 + brands and 12,000 +
stores across the country.


• Discover what’s nearby, Engage
with your favorite brands, and Earn
real rewards.




SMS: ‘@delight’ to 92431-92431

Website: DelightCircle.com
Some highlights
“DelightCircle is an app to help you pick out the best deals in the shops around
you, and also hand you points for just walking into them. A must-have for all
ye Android and iOS shopaholics.”

ThinkDigit, Sep 2012


“DelightCircle is featured as one of the top 6 mobile apps across the country.”

NASSCOM Emerge AppFame Aug 2012



“DelightCircle is featured by Samsung App Store & Blackberry App World.”

Sep, Oct 2012 - present
MongoDB & MongoEngine power the
 data storage & processing parts of
  DelightCircle backend platform
What is Geo-spatial/Location
           data?

Any data which has a location
       (2D – lat/lng)
     associated with it.
Some Examples

• The data can be of users.
User = {‘loc’:[12.97,72.023], ‘name’:’xyz’, ‘email’:’..’, ..}


• The data can be of local businesses.
Place = {‘loc’:[12.97,72.023], ‘name’:’mnz’, ‘categories’:’..’, ..}


• The data can be of Photos.
Photo = {‘loc’:[12.97,72.023], ‘file’: File(), ‘taken_by’:’User()’, ..}

• and can be many more types,
leading to the next generation of location-based applications.
Let’s Dive in !
Build the backend for a check-in based rewards app
Requirements

• Users can check-in to places using the app, and earn
rewards based on check-ins.

Ex: GET 10% OFF on every 2nd check-in.


• Rewards can be created by the merchant, and can be assigned
to specific places of his business.


• Rewards should be searchable by users based on the
location and some keywords.
Why MongoDB ?
• Fast, Easy and Schema-less.

• Multiple Language Support & Faster development cycle.

• Document Object store – maps much more cleanly to OOP.

• Replica sets/Automatic failover of failed nodes.

• Support for Geo-spatial Indexes and Queries.

It cheats to make the math easier/faster by assuming a flat earth
(where 1 degree of latitude or longitude is always the same).

Has methods to handle curvature of Earth (Spherical – if needed)
MongoDB – a database with real
 world solutions for real world
           problems


   Simple to use & flexible.
MongoEngine


        Python                   MongoDB

                 MongoEngine
• Document Object Mapper for Python and MongoDB

•Built on top of PyMongo &Very similar to popular Django
ORM

• Add all your validation stuff in MongoEngine.
 $easy_install mongoengine
Structuring & indexing your data
 depends on the way you want to
         query your data


Data modeling differs between use-
             cases.
Place                             Reward

                                        Id
      Id
                 [Place-ref]           title
    name
                                   [{Place-ref,
location (2d-       1 to n
                                 Loc}, {Place-ref,
   index)
                                    Loc}, ….. ]
  tags, etc.
                                    tags, etc.
                     Place-ref

                1 to 1             Check-in

    User
                                       Id
     Id                             User-ref
   name          User-ref           Place-ref
email (index)                     time (index)
  mobile            1 to 1           action
  Gender
  age, etc.


                                 Schema Design
Let’s start with Places
Place = {
   _id: ObjectId(),
   loc: [12.97,72.23], //suggested option
   name: ‘A’,
   tags: [‘food’, ‘restaurant’]
}
Place= {
   _id: ObjectId(),
   loc: {x:12.97, y:72.23},
   name: ‘A’,
   tags: [‘electronics’, ‘mobiles’]
}
loc: {lon: 40.739, lat:73.992}, ensure that you follow
the same order for consistency.
Place Model - MongoEngine
import mongoengine
connect(‘my_database’) // connecting to DB

Class Place(Document):
loc= GeoPointField() // Geo2D index is
automatically created
   name = StringField(max_length=50)
   tags = ListField(StringField(max_length=300))


P = Place(name=‘Pizza Hut’, loc=[12.97,77.9],
tags=[‘pizza’, ‘restaurant’, ..])

p.save() // saves the document to Mongo
Some Geospatial Queries
Find the closest 20 places to a given location.

JavaScript Shell

> db.places.find({‘loc’: {‘$near’: [12.62, 72.323]}}).limit(20)

Python (MongoEngine)

Place.objects(loc__near = [12.62, 72.323]).limit(20)

Results are sorted by distance, no need of an additional
sort.
Bounds Queries
Find the closest 20 places to a given location that are
within a distance of 5 km.

JavaScript Shell

> db.places.find( { loc : { $near : [12.62,72.323] ,
$maxDistance : 5/earth_radius } } ).limit(20)

Python (MongoEngine)

Place.objects(loc__within__distance =[ [12.62, 72.323],
5/earth_radius]).limit(20)

Results are not sorted by distance, hence faster
User & Check-ins data model
User = {
   _id: ObjectId(),
name: ‘A’,
   email: ‘xyz@domain.com’, etc.
}

Check-in = {
   _id: ObjectId(),
   place: { "$ref" : "Places", "$id" : ObjectId()},
   user: { "$ref" : ”Users", "$id" : ObjectId()},
   time: ISODate(),
   etc.
}
Check-ins - MongoEngine
Class Check-in(Document):
   user = ReferenceField(‘Users’, required=True),
   place = ReferenceField(‘Places’, required=True),
   time = DateTimeField(required = True)

   meta = {
     ‘ordering’: [‘-time’],
     ‘indexes’: [(‘user’, ‘place’)]
   }

A Compound Index is created on user and place, since
most of our queries will contain both.
Rewards data model
Reward = {
    _id: ObjectId(),
    name: ‘GET 10% OFF .. ‘,
places: [{ "$ref" : "Places", "$id" : ObjectId()}, { "$ref"
: “Places", "$id" : ObjectId()}, .. ]
    etc.
}
If you want to query the rewards based on location
Reward = {
   _id: ObjectId(),
   places: [ { place: { "$ref" : "Places", "$id" :
ObjectId()}, loc: {x:0, y:0}} , .. ]
   etc.
}
Rewards - MongoEngine
Class Reward(Document):
    name = StringField(required=True),
    places =
ListField(EmbeddedDocumentField(PlaceLocation)
)
    tags = ListField(StringField())
    num_checkins_required = IntField(default=1)



Class PlaceLocation(EmbeddedDocument):
   place = ReferenceField(‘Places’)
   loc = GeoPointField()
Find the rewards nearalocation which have tags –
               ‘food’ and ‘pizza’.

JavaScript Shell

db.Rewards.find({‘places.loc’:{‘near’:[12.62, 72.3]}, ‘tags’:
{‘$all’: [‘food’, ‘pizza’]}}).limit(20);

Python (MongoEngine)

Reward.objects(places__loc__near =[12.62,72.323],
tags__all = [‘food’, ‘pizza’]).limit(20)

You need to have a compound geo-spatial index defined on
both keys together for better performance of the query.
Conclusion
1. Spatial is easy and fun on MongoDB !!
2. You can now build your own check-in application (or)
   also build your own field data entry system.
3. Running MongoDB on any cloud infrastructure is quite
   easy.




 PS: WE ARE HIRING – looking for smart hackers
 Roles: Backend, iOS Developer, UI/UX Designer, etc.

 Mail me at                           (or) come talk to
 me.
Questions?

      Thank You 

 Stay Hungry, Stay Foolish !!!
                - Steve Jobs



   raviteja@delightcircle.com
Twitter, Facebook - @raviteja2007
     www.DelightCircle.com

Weitere ähnliche Inhalte

Was ist angesagt?

Webinar: Schema Design
Webinar: Schema DesignWebinar: Schema Design
Webinar: Schema DesignMongoDB
 
Mongo db – document oriented database
Mongo db – document oriented databaseMongo db – document oriented database
Mongo db – document oriented databaseWojciech Sznapka
 
Schema Design with MongoDB
Schema Design with MongoDBSchema Design with MongoDB
Schema Design with MongoDBrogerbodamer
 
Practical Ruby Projects With Mongo Db
Practical Ruby Projects With Mongo DbPractical Ruby Projects With Mongo Db
Practical Ruby Projects With Mongo DbAlex Sharp
 
Back to Basics Webinar 2: Your First MongoDB Application
Back to Basics Webinar 2: Your First MongoDB ApplicationBack to Basics Webinar 2: Your First MongoDB Application
Back to Basics Webinar 2: Your First MongoDB ApplicationMongoDB
 
MongoDB : The Definitive Guide
MongoDB : The Definitive GuideMongoDB : The Definitive Guide
MongoDB : The Definitive GuideWildan Maulana
 
Back to Basics Webinar 3: Schema Design Thinking in Documents
 Back to Basics Webinar 3: Schema Design Thinking in Documents Back to Basics Webinar 3: Schema Design Thinking in Documents
Back to Basics Webinar 3: Schema Design Thinking in DocumentsMongoDB
 
Back to Basics: My First MongoDB Application
Back to Basics: My First MongoDB ApplicationBack to Basics: My First MongoDB Application
Back to Basics: My First MongoDB ApplicationMongoDB
 
Intro To Mongo Db
Intro To Mongo DbIntro To Mongo Db
Intro To Mongo Dbchriskite
 
Webinar: Building Your First App with MongoDB and Java
Webinar: Building Your First App with MongoDB and JavaWebinar: Building Your First App with MongoDB and Java
Webinar: Building Your First App with MongoDB and JavaMongoDB
 
MongoDB Schema Design
MongoDB Schema DesignMongoDB Schema Design
MongoDB Schema DesignAlex Litvinok
 
Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDBJustin Smestad
 
OSCON 2012 MongoDB Tutorial
OSCON 2012 MongoDB TutorialOSCON 2012 MongoDB Tutorial
OSCON 2012 MongoDB TutorialSteven Francia
 
2014 bigdatacamp asya_kamsky
2014 bigdatacamp asya_kamsky2014 bigdatacamp asya_kamsky
2014 bigdatacamp asya_kamskyData Con LA
 
Building web applications with mongo db presentation
Building web applications with mongo db presentationBuilding web applications with mongo db presentation
Building web applications with mongo db presentationMurat Çakal
 
The Fine Art of Schema Design in MongoDB: Dos and Don'ts
The Fine Art of Schema Design in MongoDB: Dos and Don'tsThe Fine Art of Schema Design in MongoDB: Dos and Don'ts
The Fine Art of Schema Design in MongoDB: Dos and Don'tsMatias Cascallares
 
Dealing with Azure Cosmos DB
Dealing with Azure Cosmos DBDealing with Azure Cosmos DB
Dealing with Azure Cosmos DBMihail Mateev
 

Was ist angesagt? (20)

Webinar: Schema Design
Webinar: Schema DesignWebinar: Schema Design
Webinar: Schema Design
 
MongoDB 101
MongoDB 101MongoDB 101
MongoDB 101
 
Mongo db – document oriented database
Mongo db – document oriented databaseMongo db – document oriented database
Mongo db – document oriented database
 
Schema Design with MongoDB
Schema Design with MongoDBSchema Design with MongoDB
Schema Design with MongoDB
 
Practical Ruby Projects With Mongo Db
Practical Ruby Projects With Mongo DbPractical Ruby Projects With Mongo Db
Practical Ruby Projects With Mongo Db
 
MongoDB
MongoDBMongoDB
MongoDB
 
Back to Basics Webinar 2: Your First MongoDB Application
Back to Basics Webinar 2: Your First MongoDB ApplicationBack to Basics Webinar 2: Your First MongoDB Application
Back to Basics Webinar 2: Your First MongoDB Application
 
MongoDB : The Definitive Guide
MongoDB : The Definitive GuideMongoDB : The Definitive Guide
MongoDB : The Definitive Guide
 
Back to Basics Webinar 3: Schema Design Thinking in Documents
 Back to Basics Webinar 3: Schema Design Thinking in Documents Back to Basics Webinar 3: Schema Design Thinking in Documents
Back to Basics Webinar 3: Schema Design Thinking in Documents
 
Back to Basics: My First MongoDB Application
Back to Basics: My First MongoDB ApplicationBack to Basics: My First MongoDB Application
Back to Basics: My First MongoDB Application
 
Mongo DB 102
Mongo DB 102Mongo DB 102
Mongo DB 102
 
Intro To Mongo Db
Intro To Mongo DbIntro To Mongo Db
Intro To Mongo Db
 
Webinar: Building Your First App with MongoDB and Java
Webinar: Building Your First App with MongoDB and JavaWebinar: Building Your First App with MongoDB and Java
Webinar: Building Your First App with MongoDB and Java
 
MongoDB Schema Design
MongoDB Schema DesignMongoDB Schema Design
MongoDB Schema Design
 
Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDB
 
OSCON 2012 MongoDB Tutorial
OSCON 2012 MongoDB TutorialOSCON 2012 MongoDB Tutorial
OSCON 2012 MongoDB Tutorial
 
2014 bigdatacamp asya_kamsky
2014 bigdatacamp asya_kamsky2014 bigdatacamp asya_kamsky
2014 bigdatacamp asya_kamsky
 
Building web applications with mongo db presentation
Building web applications with mongo db presentationBuilding web applications with mongo db presentation
Building web applications with mongo db presentation
 
The Fine Art of Schema Design in MongoDB: Dos and Don'ts
The Fine Art of Schema Design in MongoDB: Dos and Don'tsThe Fine Art of Schema Design in MongoDB: Dos and Don'ts
The Fine Art of Schema Design in MongoDB: Dos and Don'ts
 
Dealing with Azure Cosmos DB
Dealing with Azure Cosmos DBDealing with Azure Cosmos DB
Dealing with Azure Cosmos DB
 

Andere mochten auch

Messaging Architectures with NoSQL Databases as Message Stores
Messaging Architectures with NoSQL Databases as Message StoresMessaging Architectures with NoSQL Databases as Message Stores
Messaging Architectures with NoSQL Databases as Message StoresSrini Penchikala
 
Progressive web apps with Angular 2
Progressive web apps with Angular 2Progressive web apps with Angular 2
Progressive web apps with Angular 2Manfred Steyer
 
What Open Source and Open Data Mean for Tomorrow's Transportation Agencies
What Open Source and Open Data Mean for Tomorrow's Transportation AgenciesWhat Open Source and Open Data Mean for Tomorrow's Transportation Agencies
What Open Source and Open Data Mean for Tomorrow's Transportation AgenciesOpenPlans
 
Open 311: A Platform for a Participatory Civic Infrastructure
Open 311: A Platform for a Participatory Civic InfrastructureOpen 311: A Platform for a Participatory Civic Infrastructure
Open 311: A Platform for a Participatory Civic InfrastructureOpenPlans
 
RestMQ - HTTP/Redis based Message Queue
RestMQ - HTTP/Redis based Message QueueRestMQ - HTTP/Redis based Message Queue
RestMQ - HTTP/Redis based Message QueueGleicon Moraes
 

Andere mochten auch (6)

Messaging Architectures with NoSQL Databases as Message Stores
Messaging Architectures with NoSQL Databases as Message StoresMessaging Architectures with NoSQL Databases as Message Stores
Messaging Architectures with NoSQL Databases as Message Stores
 
Progressive web apps with Angular 2
Progressive web apps with Angular 2Progressive web apps with Angular 2
Progressive web apps with Angular 2
 
What Open Source and Open Data Mean for Tomorrow's Transportation Agencies
What Open Source and Open Data Mean for Tomorrow's Transportation AgenciesWhat Open Source and Open Data Mean for Tomorrow's Transportation Agencies
What Open Source and Open Data Mean for Tomorrow's Transportation Agencies
 
Data into Action
Data into ActionData into Action
Data into Action
 
Open 311: A Platform for a Participatory Civic Infrastructure
Open 311: A Platform for a Participatory Civic InfrastructureOpen 311: A Platform for a Participatory Civic Infrastructure
Open 311: A Platform for a Participatory Civic Infrastructure
 
RestMQ - HTTP/Redis based Message Queue
RestMQ - HTTP/Redis based Message QueueRestMQ - HTTP/Redis based Message Queue
RestMQ - HTTP/Redis based Message Queue
 

Ähnlich wie Building a Location-based Platform with MongoDB from Zero

OSDC 2012 | Building a first application on MongoDB by Ross Lawley
OSDC 2012 | Building a first application on MongoDB by Ross LawleyOSDC 2012 | Building a first application on MongoDB by Ross Lawley
OSDC 2012 | Building a first application on MongoDB by Ross LawleyNETWAYS
 
Fast REST APIs Development with MongoDB
Fast REST APIs Development with MongoDBFast REST APIs Development with MongoDB
Fast REST APIs Development with MongoDBMongoDB
 
Marc s01 e02-crud-database
Marc s01 e02-crud-databaseMarc s01 e02-crud-database
Marc s01 e02-crud-databaseMongoDB
 
Webinarserie: Einführung in MongoDB: “Back to Basics” - Teil 3 - Interaktion ...
Webinarserie: Einführung in MongoDB: “Back to Basics” - Teil 3 - Interaktion ...Webinarserie: Einführung in MongoDB: “Back to Basics” - Teil 3 - Interaktion ...
Webinarserie: Einführung in MongoDB: “Back to Basics” - Teil 3 - Interaktion ...MongoDB
 
Mongo Web Apps: OSCON 2011
Mongo Web Apps: OSCON 2011Mongo Web Apps: OSCON 2011
Mongo Web Apps: OSCON 2011rogerbodamer
 
MongoDB, PHP and the cloud - php cloud summit 2011
MongoDB, PHP and the cloud - php cloud summit 2011MongoDB, PHP and the cloud - php cloud summit 2011
MongoDB, PHP and the cloud - php cloud summit 2011Steven Francia
 
What do you mean, Backwards Compatibility?
What do you mean, Backwards Compatibility?What do you mean, Backwards Compatibility?
What do you mean, Backwards Compatibility?Trisha Gee
 
Building your first application w/mongoDB MongoSV2011
Building your first application w/mongoDB MongoSV2011Building your first application w/mongoDB MongoSV2011
Building your first application w/mongoDB MongoSV2011Steven Francia
 
Dev Jumpstart: Build Your First App with MongoDB
Dev Jumpstart: Build Your First App with MongoDBDev Jumpstart: Build Your First App with MongoDB
Dev Jumpstart: Build Your First App with MongoDBMongoDB
 
Advanced Analytics & Statistics with MongoDB
Advanced Analytics & Statistics with MongoDBAdvanced Analytics & Statistics with MongoDB
Advanced Analytics & Statistics with MongoDBJohn De Goes
 
Building Your First MongoDB App ~ Metadata Catalog
Building Your First MongoDB App ~ Metadata CatalogBuilding Your First MongoDB App ~ Metadata Catalog
Building Your First MongoDB App ~ Metadata Cataloghungarianhc
 
Introduction to MongoDB at IGDTUW
Introduction to MongoDB at IGDTUWIntroduction to MongoDB at IGDTUW
Introduction to MongoDB at IGDTUWAnkur Raina
 
Webinar: General Technical Overview of MongoDB for Dev Teams
Webinar: General Technical Overview of MongoDB for Dev TeamsWebinar: General Technical Overview of MongoDB for Dev Teams
Webinar: General Technical Overview of MongoDB for Dev TeamsMongoDB
 
Mongodb intro
Mongodb introMongodb intro
Mongodb introchristkv
 
Building Your First Application with MongoDB
Building Your First Application with MongoDBBuilding Your First Application with MongoDB
Building Your First Application with MongoDBMongoDB
 
MongoDB.local DC 2018: Tutorial - Data Analytics with MongoDB
MongoDB.local DC 2018: Tutorial - Data Analytics with MongoDBMongoDB.local DC 2018: Tutorial - Data Analytics with MongoDB
MongoDB.local DC 2018: Tutorial - Data Analytics with MongoDBMongoDB
 
Building Your First MongoDB Application
Building Your First MongoDB ApplicationBuilding Your First MongoDB Application
Building Your First MongoDB ApplicationRick Copeland
 
Pyconie 2012
Pyconie 2012Pyconie 2012
Pyconie 2012Yaqi Zhao
 
Building your first app with MongoDB
Building your first app with MongoDBBuilding your first app with MongoDB
Building your first app with MongoDBNorberto Leite
 

Ähnlich wie Building a Location-based Platform with MongoDB from Zero (20)

OSDC 2012 | Building a first application on MongoDB by Ross Lawley
OSDC 2012 | Building a first application on MongoDB by Ross LawleyOSDC 2012 | Building a first application on MongoDB by Ross Lawley
OSDC 2012 | Building a first application on MongoDB by Ross Lawley
 
Fast REST APIs Development with MongoDB
Fast REST APIs Development with MongoDBFast REST APIs Development with MongoDB
Fast REST APIs Development with MongoDB
 
Marc s01 e02-crud-database
Marc s01 e02-crud-databaseMarc s01 e02-crud-database
Marc s01 e02-crud-database
 
Webinarserie: Einführung in MongoDB: “Back to Basics” - Teil 3 - Interaktion ...
Webinarserie: Einführung in MongoDB: “Back to Basics” - Teil 3 - Interaktion ...Webinarserie: Einführung in MongoDB: “Back to Basics” - Teil 3 - Interaktion ...
Webinarserie: Einführung in MongoDB: “Back to Basics” - Teil 3 - Interaktion ...
 
ActiveRecord vs Mongoid
ActiveRecord vs MongoidActiveRecord vs Mongoid
ActiveRecord vs Mongoid
 
Mongo Web Apps: OSCON 2011
Mongo Web Apps: OSCON 2011Mongo Web Apps: OSCON 2011
Mongo Web Apps: OSCON 2011
 
MongoDB, PHP and the cloud - php cloud summit 2011
MongoDB, PHP and the cloud - php cloud summit 2011MongoDB, PHP and the cloud - php cloud summit 2011
MongoDB, PHP and the cloud - php cloud summit 2011
 
What do you mean, Backwards Compatibility?
What do you mean, Backwards Compatibility?What do you mean, Backwards Compatibility?
What do you mean, Backwards Compatibility?
 
Building your first application w/mongoDB MongoSV2011
Building your first application w/mongoDB MongoSV2011Building your first application w/mongoDB MongoSV2011
Building your first application w/mongoDB MongoSV2011
 
Dev Jumpstart: Build Your First App with MongoDB
Dev Jumpstart: Build Your First App with MongoDBDev Jumpstart: Build Your First App with MongoDB
Dev Jumpstart: Build Your First App with MongoDB
 
Advanced Analytics & Statistics with MongoDB
Advanced Analytics & Statistics with MongoDBAdvanced Analytics & Statistics with MongoDB
Advanced Analytics & Statistics with MongoDB
 
Building Your First MongoDB App ~ Metadata Catalog
Building Your First MongoDB App ~ Metadata CatalogBuilding Your First MongoDB App ~ Metadata Catalog
Building Your First MongoDB App ~ Metadata Catalog
 
Introduction to MongoDB at IGDTUW
Introduction to MongoDB at IGDTUWIntroduction to MongoDB at IGDTUW
Introduction to MongoDB at IGDTUW
 
Webinar: General Technical Overview of MongoDB for Dev Teams
Webinar: General Technical Overview of MongoDB for Dev TeamsWebinar: General Technical Overview of MongoDB for Dev Teams
Webinar: General Technical Overview of MongoDB for Dev Teams
 
Mongodb intro
Mongodb introMongodb intro
Mongodb intro
 
Building Your First Application with MongoDB
Building Your First Application with MongoDBBuilding Your First Application with MongoDB
Building Your First Application with MongoDB
 
MongoDB.local DC 2018: Tutorial - Data Analytics with MongoDB
MongoDB.local DC 2018: Tutorial - Data Analytics with MongoDBMongoDB.local DC 2018: Tutorial - Data Analytics with MongoDB
MongoDB.local DC 2018: Tutorial - Data Analytics with MongoDB
 
Building Your First MongoDB Application
Building Your First MongoDB ApplicationBuilding Your First MongoDB Application
Building Your First MongoDB Application
 
Pyconie 2012
Pyconie 2012Pyconie 2012
Pyconie 2012
 
Building your first app with MongoDB
Building your first app with MongoDBBuilding your first app with MongoDB
Building your first app with MongoDB
 

Building a Location-based Platform with MongoDB from Zero

  • 1. MongoDB Bangalore Conference Oct 26th 2012 Building a Location-based platform with MongoDB from Zero Raviteja Dodda Co-Founder & CTO, Pipal Tech Ventures
  • 2. DelightCircle • Offers, New Arrivals & Rewards from 250 + brands and 12,000 + stores across the country. • Discover what’s nearby, Engage with your favorite brands, and Earn real rewards. SMS: ‘@delight’ to 92431-92431 Website: DelightCircle.com
  • 3. Some highlights “DelightCircle is an app to help you pick out the best deals in the shops around you, and also hand you points for just walking into them. A must-have for all ye Android and iOS shopaholics.” ThinkDigit, Sep 2012 “DelightCircle is featured as one of the top 6 mobile apps across the country.” NASSCOM Emerge AppFame Aug 2012 “DelightCircle is featured by Samsung App Store & Blackberry App World.” Sep, Oct 2012 - present
  • 4. MongoDB & MongoEngine power the data storage & processing parts of DelightCircle backend platform
  • 5. What is Geo-spatial/Location data? Any data which has a location (2D – lat/lng) associated with it.
  • 6. Some Examples • The data can be of users. User = {‘loc’:[12.97,72.023], ‘name’:’xyz’, ‘email’:’..’, ..} • The data can be of local businesses. Place = {‘loc’:[12.97,72.023], ‘name’:’mnz’, ‘categories’:’..’, ..} • The data can be of Photos. Photo = {‘loc’:[12.97,72.023], ‘file’: File(), ‘taken_by’:’User()’, ..} • and can be many more types, leading to the next generation of location-based applications.
  • 7. Let’s Dive in ! Build the backend for a check-in based rewards app
  • 8. Requirements • Users can check-in to places using the app, and earn rewards based on check-ins. Ex: GET 10% OFF on every 2nd check-in. • Rewards can be created by the merchant, and can be assigned to specific places of his business. • Rewards should be searchable by users based on the location and some keywords.
  • 9. Why MongoDB ? • Fast, Easy and Schema-less. • Multiple Language Support & Faster development cycle. • Document Object store – maps much more cleanly to OOP. • Replica sets/Automatic failover of failed nodes. • Support for Geo-spatial Indexes and Queries. It cheats to make the math easier/faster by assuming a flat earth (where 1 degree of latitude or longitude is always the same). Has methods to handle curvature of Earth (Spherical – if needed)
  • 10. MongoDB – a database with real world solutions for real world problems Simple to use & flexible.
  • 11. MongoEngine Python MongoDB MongoEngine • Document Object Mapper for Python and MongoDB •Built on top of PyMongo &Very similar to popular Django ORM • Add all your validation stuff in MongoEngine. $easy_install mongoengine
  • 12. Structuring & indexing your data depends on the way you want to query your data Data modeling differs between use- cases.
  • 13. Place Reward Id Id [Place-ref] title name [{Place-ref, location (2d- 1 to n Loc}, {Place-ref, index) Loc}, ….. ] tags, etc. tags, etc. Place-ref 1 to 1 Check-in User Id Id User-ref name User-ref Place-ref email (index) time (index) mobile 1 to 1 action Gender age, etc. Schema Design
  • 14. Let’s start with Places Place = { _id: ObjectId(), loc: [12.97,72.23], //suggested option name: ‘A’, tags: [‘food’, ‘restaurant’] } Place= { _id: ObjectId(), loc: {x:12.97, y:72.23}, name: ‘A’, tags: [‘electronics’, ‘mobiles’] } loc: {lon: 40.739, lat:73.992}, ensure that you follow the same order for consistency.
  • 15. Place Model - MongoEngine import mongoengine connect(‘my_database’) // connecting to DB Class Place(Document): loc= GeoPointField() // Geo2D index is automatically created name = StringField(max_length=50) tags = ListField(StringField(max_length=300)) P = Place(name=‘Pizza Hut’, loc=[12.97,77.9], tags=[‘pizza’, ‘restaurant’, ..]) p.save() // saves the document to Mongo
  • 16. Some Geospatial Queries Find the closest 20 places to a given location. JavaScript Shell > db.places.find({‘loc’: {‘$near’: [12.62, 72.323]}}).limit(20) Python (MongoEngine) Place.objects(loc__near = [12.62, 72.323]).limit(20) Results are sorted by distance, no need of an additional sort.
  • 17. Bounds Queries Find the closest 20 places to a given location that are within a distance of 5 km. JavaScript Shell > db.places.find( { loc : { $near : [12.62,72.323] , $maxDistance : 5/earth_radius } } ).limit(20) Python (MongoEngine) Place.objects(loc__within__distance =[ [12.62, 72.323], 5/earth_radius]).limit(20) Results are not sorted by distance, hence faster
  • 18. User & Check-ins data model User = { _id: ObjectId(), name: ‘A’, email: ‘xyz@domain.com’, etc. } Check-in = { _id: ObjectId(), place: { "$ref" : "Places", "$id" : ObjectId()}, user: { "$ref" : ”Users", "$id" : ObjectId()}, time: ISODate(), etc. }
  • 19. Check-ins - MongoEngine Class Check-in(Document): user = ReferenceField(‘Users’, required=True), place = ReferenceField(‘Places’, required=True), time = DateTimeField(required = True) meta = { ‘ordering’: [‘-time’], ‘indexes’: [(‘user’, ‘place’)] } A Compound Index is created on user and place, since most of our queries will contain both.
  • 20. Rewards data model Reward = { _id: ObjectId(), name: ‘GET 10% OFF .. ‘, places: [{ "$ref" : "Places", "$id" : ObjectId()}, { "$ref" : “Places", "$id" : ObjectId()}, .. ] etc. } If you want to query the rewards based on location Reward = { _id: ObjectId(), places: [ { place: { "$ref" : "Places", "$id" : ObjectId()}, loc: {x:0, y:0}} , .. ] etc. }
  • 21. Rewards - MongoEngine Class Reward(Document): name = StringField(required=True), places = ListField(EmbeddedDocumentField(PlaceLocation) ) tags = ListField(StringField()) num_checkins_required = IntField(default=1) Class PlaceLocation(EmbeddedDocument): place = ReferenceField(‘Places’) loc = GeoPointField()
  • 22. Find the rewards nearalocation which have tags – ‘food’ and ‘pizza’. JavaScript Shell db.Rewards.find({‘places.loc’:{‘near’:[12.62, 72.3]}, ‘tags’: {‘$all’: [‘food’, ‘pizza’]}}).limit(20); Python (MongoEngine) Reward.objects(places__loc__near =[12.62,72.323], tags__all = [‘food’, ‘pizza’]).limit(20) You need to have a compound geo-spatial index defined on both keys together for better performance of the query.
  • 23. Conclusion 1. Spatial is easy and fun on MongoDB !! 2. You can now build your own check-in application (or) also build your own field data entry system. 3. Running MongoDB on any cloud infrastructure is quite easy. PS: WE ARE HIRING – looking for smart hackers Roles: Backend, iOS Developer, UI/UX Designer, etc. Mail me at (or) come talk to me.
  • 24. Questions? Thank You  Stay Hungry, Stay Foolish !!! - Steve Jobs raviteja@delightcircle.com Twitter, Facebook - @raviteja2007 www.DelightCircle.com

Hinweis der Redaktion

  1. Implications and Philoshophy of Client-side rendering
  2. Why am I talking about location-based platforms ? Because we built one – DelightCircleGet some white color images for Android, iphone, bb, website, and SMSFor businesses, we provide a location-based marketing platform with a powerful CRM backend, and access to Analytics.
  3. Let’s start with the location data.
  4. Who are the python developers here ? Can I have a raise of hands ?MongoEngine provides the ability to define your documents well, and will validate the document while saving, as mongodb is schema-less.
  5. You can embed the document some times, sometimes you need to refer. It all depends on the use-cases. Don’t worry about the normalization pieces in Mongo.
  6. Define the Schema for Users, Rewards as well.
  7. Document is a class of MongoEngine library, and our Place class inherits the Document class. This is the definition of our document.
  8. Place.objects(loc__within_distance = [[12.62, 72.323], 1]).limit(20)Place.objects(loc__near = [12.62, 72.323]).limit(20)Find the closest 20 places to a given location that are within 1 degree of the location.Place.objects(name= ‘Pizza Hut’, loc__near = [12.62, 72.323]).limit(20)Find the closest 20 pizza hut’s near to a given location.
  9. Place.objects(loc__within_distance = [[12.62, 72.323], 1]).limit(20)Place.objects(loc__near = [12.62, 72.323]).limit(20)Find the closest 20 places to a given location that are within 1 degree of the location.Place.objects(name= ‘Pizza Hut’, loc__near = [12.62, 72.323]).limit(20)Find the closest 20 pizza hut’s near to a given location.
  10. Define the Schema for Users, Rewards as well.
  11. Define the Schema for Users, Rewards as well.