Skip to content

Instantly share code, notes, and snippets.

@anandof28
Created April 22, 2013 07:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anandof28/5433086 to your computer and use it in GitHub Desktop.
Save anandof28/5433086 to your computer and use it in GitHub Desktop.
Mongodb Find for Retrieving the Closest Larger / Closest Smaller Values from a List when there is No Exact Match?
var mongoose = require('mongoose')
, Schema = mongoose.Schema;
var CloudSchema = new Schema({
'Cloud_Provider' : {type : String, required : true, trim: true},
'Lat' : {type: Number, required : true},
'Long' : {type: Number, required : true},
'Server_Type' : {type : String, required : true, trim: true},
'Memory' : {type : Number, required : true, trim: true},
'Storage' : {type : Number, required : true, trim: true},
'CPU' : {type : Number, required : true, trim: true},
'Region' : {type : String, required : true, trim: true},
'Linux_Price' : {type : Number, required : true, trim: true},
'Windows_Price' : {type : Number, required : true, trim: true}
});
var Cloud = mongoose.model('Cloud', CloudSchema);
exports.new = function(req, res) {
cloud = Cloud(req.body);
cloud.save(function(err) {
if(err) {
res.send({'code': 400, 'error': 'Some issue'}, 400);
} else {
res.send(cloud);
}
})
}
exports.search = function(req,res){
var platform = req.query.plat;
var memory = req.query.mem;
var storage = req.query.strg;
var cpu = req.query.cpu;
Cloud.find({CPU : { $gt: cpu-2, $lt: cpu+2 } ,Memory : { $gt: memory-1, $lt: memory+1 },
Storage : { $gt: storage-50, $lt: storage+50 }}
, function(err, result){
if(err) {
res.send({'code': 400, 'error': 'Some issue'}, 400)
} else {
res.send(result);
}
});
}
@anandof28
Copy link
Author

line 36 need a perfect query that would return Closest Larger / Closest Smaller Values from a List when there is No Exact Match

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment