1

My JSON file is as follows and my router router.get('/books/:bookid', ctrlbooks.booksReadOne); returns the following JSON which is as expected but if I want to return just the title for say for api of this kind with given id router.get('/books/:bookid/titles/:titleid', ctrlbooks.titlesReadOne); it wont work am i missing something?

[
  {
    "_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"
        ]
      }
    ]
  }
]

my controller for router router.get('/books/:bookid', ctrlbooks.booksReadOne); is

    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"
            });
        }
    };

and this is what I tried to return only the title with a given title id provided but this doesn't work when I tested it using postman

    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"
            });
        }
    };
ipkiss
  • 57
  • 1
  • 8
  • Possible duplicate of [Retrieve only the queried element in an object array in MongoDB collection](http://stackoverflow.com/questions/3985214/retrieve-only-the-queried-element-in-an-object-array-in-mongodb-collection) – s7vr Apr 02 '17 at 21:35

1 Answers1

0

Alternatively to aggregation, you can filter titles on application level:

    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
            }
        })
Alex Blex
  • 25,038
  • 5
  • 33
  • 63
  • from the above JSON what i am hoping to get something like this (only one title form the given id) { "title": "Animal Farm", "_id": "58dd3f2701cc081056135dae", "reviews": [ { "author": "Barry", "rating": 3, "reviewText": "this is a review", "_id": "58dd8e13876c0f16b17cd7dc", "createdOn": "2017-03-30T23:00:35.662Z" } ], "favouredBy": [ "bb, aa, cc" ] } – ipkiss Apr 02 '17 at 21:56
  • `book.titles` is an array. It may contain more than 1 title with requested id. If you are happy to return the first match, return first element of the filtered array, or iterate titles with `for` loop to exit iteration on first match to optimise it a bit. – Alex Blex Apr 02 '17 at 22:14