Mikyas TADESSE Mikyas TADESSE - 14 days ago 8
Node.js Question

How to query mongodb on a specific field

I have a collection of objects with the following schema :

var Meetup = new Schema({
name: String,
text:String,

});


I would like to get all of the meetups whom name contain a string.

Here is my api :

module.exports.list = function (req, res) {
Meetup.find({}, function (err, results) {
res.json(results);
});
}


and in my angular controller i have :

var Meetup = $resource('/api/meetups');
$scope.meetups = []

Meetup.query(function (results) {
$scope.meetups = results;
});


can anyone help

Answer

Query on specific field

ModelName.find({fieldName: value}, function (err, results) {
    //...
  });

so for your case query will be like:

exports.list = function (req, res) {
  Meetup.find({name: req.query.name}, function (err, results) {
    res.json(results);
  });
};

and angular controller like

var Meetup = $resource('/api/meetups', {}, {
  query: {method: 'get', isArray: true}
});

$scope.meetups = []

Meetup.query({name: 'yourName'}).$promise.then(function(results) {
   // console.log(results);
   $scope.meetups = results;
}, function(error) {
  // console.log(error);
   $scope.meetups = [];
});