I want to pull only relevant data from mongo.db. Let's say I have a collection "library" which looks like:
[
{
"section": "Fantasy",
"series": {
"ChroniclesOfNarnia": {
"books": [
{
"title": "The Lion, the Witch, and the Wardrobe",
"author": "CS Lewis",
"bookNum": 1
},
{
"title": "Prince Caspian",
"author": "CS Lewis",
"bookNum": 2
}
]
}
},
},
//a whole bunch of other records
];
I'd like to fetch ONLY the data for the book "Prince Caspian". So:
{
"title": "Prince Caspian",
"author": "CS Lewis",
"bookNum": 2
}
My query looks like so:
db.getCollection('Library').find({"section.series.ChroniclesOfNarnia.books.title": "Prince Caspian"});
This returns the entire record - and not "a whole bunch of other records" and indeed the record contains the data I specified but I dont want all the other data - just the data for that one book. I looked at the mongo docs as well as more docs but I do not see anything that helps. I do see the "projection" option but that seems to deal with fields of objects, not array items...
**NOTE: I am new to mongo and database operations in general... It occurs to me that I may need to make many small collections, or write some service that takes the entire collection and gives me only the bit I need. If so, let me know but I'd favor a less complicated approach