1
I have a JSON
in this format:
{
"1": {
"cidade":"cidade1",
"name":"nome1"
},
"2": {
"cidade":"cidade2",
"name":"nome2"
}
}
and I need to filter the data of 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 attribute cod
and not by the id generated by Mongoose in a way where I typed /my/1
and he would return to me
{
"1": {
"cidade":"cidade1",
"name":"nome1"
}
}
I tried several ways but 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 ways worked. Any idea what I can do?