I have JSON
in this format:
{
"1": {
"cidade":"cidade1",
"name":"nome1"
},
"2": {
"cidade":"cidade2",
"name":"nome2"
}
}
I need to filter the data from this json. For this I made a model.js
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var MySchema = new Schema({cod: Number, cidade: String, nome: String});
module.exports = mongoose.model('My', MySchema);
and a routes.js
module.exports = function(router, My) {
router.route('/my')
.get(function(req, res) {
My.find(function(err, my) {
if (err)
res.send(err);
res.json(my);
});
});
router.route('/my/:my_id')
.get(function(req, res) {
My.findById(req.params.my_id, function(err, my) {
if (err)
res.send(err);
res.json(my);
});
})
}
My problem is this: I want to filter by my cod
attribute and not by the generated mongoose id in a way where I type /my/1
and it returns me
{
"1": {
"cidade":"cidade1",
"name":"nome1"
}
}
I have tried in many ways but I could not. I tried via query:
router.route('/my/:cod')
.get(function(req, res) {
My.find(function(err, my) {
if (err)
res.send(err);
res.json(my);
}).where('my.cod').equals(req.params.cod);
})
I tried to set a Custom Id, but none of these worked. Any idea what I can do?