Reputation: 41
I am trying to get the count of the review object within the episode object in Python. I have tried a few days: example included. But I can't get it to work. What I am looking to do is to find the episode object first, and each time when a review is found increase the counter by 1.
dataset:
{
"_id": "5df808570a83c1af4da13230",
"airdate": "2017-11-02",
"airtime": "20:30",
"image": "http://static.tvmaze.com/uploads/images/original_untouched/133/334409.jpg",
"name": "Derek",
"number": 8,
"reviews": [
{
"_id": "5dfc548b759dd951ac61ab6e",
"comment": "koijoij",
"rating": "2 stars",
"username": "k"
},
{
"_id": "5dfc551852101d027d229e0a",
"comment": "hiki",
"rating": "3 stars",
"username": "jk"
}
],
"runtime": 30,
"season": 2,
"summary": "Janet creates a big problem for Michael. Meanwhile, Eleanor lets Chidi in on a secret, and Jason puts Tahani in a difficult position.",
"url": "http://www.tvmaze.com/episodes/1215818/the-good-place-2x08-derek"
}
python method:
@app.route("/api/v1.0/episodes/<string:episode_id>/pageCounterReviews", methods=["GET"])
def getPageCountReview(episode_id):
count=0;
episode = episodes.find_one({"_id":ObjectId(episode_id)},{"reviews":1,"_id":0})
for review in episode["reviews"]:
count=count+1;
return make_response(jsonify(count), 200)
Upvotes: 0
Views: 1788
Reputation: 10912
You can just use len
:
>>> data = {"reviews": [{"_id": "1"}, {"_id": "2"}]}
>>> print(len(data['reviews']))
2
Upvotes: 1