mongoose-distance
v0.1.16
Published
Mongoose distance is a collection of Schema values for build a recommendation system in base of Mongoose.
Downloads
10
Maintainers
Readme
mongoose-distance
Description
Mongoose distance is a collection of Schema values for build a recommendation system in base of Mongoose. That order the different response document by distance between a session and product coordinates.
Installation
npm install mongoose-distance
Usage
There are two instance to insert the plugin, one for products and other for session:
//product.js
var mongoose = require('mongoose')
var distance = require('mongoose-distance')
var ProductSchema = new mongoose.Schema({
name:String
value:Integer
})
ProductSchema.plugin(distance.coordinatePlugin, {})
module.exports = mongoose.model('Product', ProductSchema)
//session.js
var mongoose = require('mongoose')
var distance = require('mongoose-distance')
var SessionSchema = new mongoose.Schema({
hash:String
})
SessionSchema.plugin(distance.coordinatePlugin, {})
module.exports = mongoose.model('Session', SessionSchema)
Now, we have coordinates documents, now we add out api distance methods. In case of Express router:
//api.js
var express = require('express')
var mongoose = require('mongoose')
var distance = require('mongoose-distance')
var router = express.Router()
// Here we need to connect mongoose with the database
// MAGIC!
var Product = require('./product')
var Session = require('./session')
// Order by minor distance
router.route('/products/')
.get(function (req, res) {
var session = new Session({name: 'session2'})
session.x = 100
session.save(function (err) {
if (err) {
console.log(err)
} else {
Product.find({}, function (err, products) {
if (err) {
console.log(err)
} else {
res.json(distance.order(products, session))
}
})
}
})
})
module.exports = router