아래와 같은 방식으로 mongodb 쿼리를 uri를 통해 할 수 있다.
router.get('/', function(req,res) {
myModel.find({name:"test e"}, function(err,data){
if(err) console.log(err)
res.json(data);
});
});
router.get('/', function(req,res) {
console.log(req.query.q)
myModel.find(req.query.q, function(err,data){
if(err) console.log(err)
res.json(data);
});
});
ex) /api/myModel?q={name:”test e”}
각각 post collection에서 name이 test인 결과와 name이 test, bio가 female인 결과를 가져오는 uri이다.
parameter값 외에도 sort, filter 등등 해야할 것이 많다.
나는 https://florianholzapfel.github.io/express-restify-mongoose/를 사용하는데
모든게 완벽한데 단점이 dependency가 심하다는 것이다.
model 외에 수정할 수 있는 hook을 제공하지 않는다.
아래는 이와 비슷한 라이브러리다.
- https://github.com/macalinao/preston
- https://github.com/greenpioneersolutions/express-query-parameters
- https://github.com/lykmapipo/express-mquery
- https://github.com/jupe/mongoose-query
- https://github.com/Turistforeningen/node-mongo-querystring
- https://github.com/loris/api-query-params
- https://github.com/saintedlama/restify-mongoose
모두 비슷한 기능을 가지고 있어 뭐가 좋다고 말하기 어렵지만. 이 중 3, 4번을 추천하고 싶다.
이유는 populate를 지원하고 가장 최근에 updatee되었기 때문이다.
Graphql https://graphql.org/learn/ 에 혹했던 마음을 다잡을 수 있다.