2017-04-02 67 views
1
搜索Node.js的applictaion嵌套JSON

我的JSON文件如下,我的路由器 router.get( '/书/:BOOKID',ctrlbooks.booksReadOne); 返回以下JSON这是预期,但如果我想带指定id的发言权只返回标题为这种API router.get(“/书/:BOOKID /标题/:titleid”,ctrlbooks.titlesReadOne) ; 它不会工作我错过了什么?通过ID

[ 
    { 
    "_id": "58dd21c3cb77090b930b6063", 
    "bookAuthor": "George Orwell", 
    "titles": [ 
     { 
     "title": "Animal Farm", 
     "_id": "58dd3f2701cc081056135dae", 
     "reviews": [ 
      { 
      "author": "Lisa", 
      "rating": 4, 
      "reviewText": "this is a review", 
      "_id": "58dd8e13876c0f16b17cd7dc", 
      "createdOn": "2017-03-30T23:00:35.662Z" 
      } 
     ], 
     "favouredBy": [ 
      "bb, aa, cc" 
     ] 
     }, 
     { 
     "title": "1984", 
     "_id": "58dd42a59f12f110d1756f08", 
     "reviews": [ 
      { 
      "author": "jessy", 
      "rating": 5, 
      "reviewText": "REVIEW FOR SECOND TITLE", 
      "_id": "58dd8ef46d4aaa16e4545c76", 
      "createdOn": "2017-03-30T23:04:20.609Z" 
      } 
     ], 
     "favouredBy": [ 
      "all" 
     ] 
     } 
    ] 
    } 
] 

我控制器,路由器 router.get( '/书/:BOOKID',ctrlbooks.booksReadOne); 是

module.exports.booksReadOne = function(req, res) { 
     console.log('Finding book details', req.params); 
     if (req.params && req.params.bookid) { 
      Bok 
       .findById(req.params.bookid) 
       .exec(function(err, book) { 
        if (!book) { 
         sendJSONresponse(res, 404, { 
          "message": "bookid not found" 
         }); 
         return; 
        } else if (err) { 
         console.log(err); 
         sendJSONresponse(res, 404, err); 
         return; 
        } 
        console.log(book); 
        sendJSONresponse(res, 200, book); 
       }); 
     } else { 
      console.log('No bookid specified'); 
      sendJSONresponse(res, 404, { 
       "message": "No bookid in request" 
      }); 
     } 
    }; 

,这就是我试图用提供的给定标题ID只返回标题,但是当我使用邮递员

module.exports.tilesReadOne = function(req, res) { 
     console.log('Finding book details', req.params); 
     if (req.params && req.params.titleid) { 
      Bok 
       .findById(req.params.titleid) 
       .exec(function(err, book) { 
        if (!book) { 
         sendJSONresponse(res, 404, { 
          "message": "title not found" 
         }); 
         return; 
        } else if (err) { 
         console.log(err); 
         sendJSONresponse(res, 404, err); 
         return; 
        } 
        console.log(book); 
        sendJSONresponse(res, 200, book); 
       }); 
     } else { 
      console.log('No title specified'); 
      sendJSONresponse(res, 404, { 
       "message": "No title in request" 
      }); 
     } 
    }; 
+0

的可能的复制[只检索MongoDB中集合的对象阵列中的查询元件](http://stackoverflow.com/questions/3985214/retrieve-onl Y型的,查询元素功能于一个对象阵列功能于MongoDB中收集) – Veeram

回答

0

或者聚集测试它这个是不行的,你可以筛选应用级别标题:

Bok 
     .findById(req.params.bookid) 
     .exec(function(err, book) { 
      if (book) { 
       var matchingTitles = book.titles.filter(function(title){ 
        return title._id == req.params.bookid 
       }) 
       // return titles, if any 
      } 
     }) 
+0

从上面的JSON什么,我希望能得到这样的(只有一个标题形式给定的ID) { “称号“: ”动物农场“, ”_id“: ”58dd3f2701cc081056135dae“, ”评论“: { ”作者“: ”巴里“, ”评级“:3, ”reviewText“:” 这是一则评论”, “_id”: “58dd8e13876c0f16b17cd7dc”, “createdOn”: “2017-03-30T23:00:35.662Z” } ], “favouredBy”:[ “BB,AA,CC” ] } – ipkiss

+0

'book.titles'是一个数组。它可能包含超过1个标题和所需的ID。如果您很高兴返回第一个匹配,则返回过滤数组的第一个元素,或者使用'for'循环迭代标题以在第一次匹配时退出迭代以优化一点。 –