I'm kinda new to the whole NoSQL euphoria and starting on a project with MongoDB as a database.
Goal: Print a list of lodging that has at least 3 grades, most of the grades (half or more) are higher than 6. Inside lodging.
Problem: I have an array of reviews. I would like to merge the objects into an array of grades and exclude the reviewer and date from each object.
Finishing up: Then I will push the objects that are $gt than 5 (so 6 and more) to isGtArray and wice wersa to isLtArray. I can then count if there is realy half the grades that are higher than 6 and project that in isMostOfTheGradesMoreThan_6 and with $match print them out. By now: The fist part of lodging that has at least 3 grades is solved. All thanks for the help.
Code by now:
db.lodging.aggregate([
{$unwind: "$lodging.reviews"},
{$group:{
_id:"$guid",
lodging: { $first: "$lodging"},
reviews: { $push: "$lodging.reviews"}}
},
{$project: {
_id: 1,
lodging: 1,
reviews: 1,
reviewsCount: { $size:"$reviews" }}},
{ $match : { reviewsCount: { $gt: 2 } } },
])Json data
{
"_id" : "5bf31f8c20b3acacc2194faa",
"registered" : "2016-04-22T08:54:50 -02:00",
"host" : {
"name" : "Schultz",
"surname" : "French",
"contact" : {"email" : "schult...@orbean.com"}, },
"lodging" : {
"reviews" : [
{
"reviewer" : "Mariana Ayers",
"date" : "2014-04-13T08:30:02 -02:00",
"cleanliness" : NumberInt(9),
"location" : NumberInt(6),
"food" : NumberInt(4)},
{
"reviewer" : "Cole Simmons",
"date" : "2015-05-07T06:00:09 -02:00",
"cleanliness" : NumberInt(1),
"location" : NumberInt(3),
"food" : NumberInt(2)}]}
}Solution
{
"_id" : "5bf31f8c20b3acacc2194faa",
"registered" : "2016-04-22T08:54:50 -02:00",
"host" : {
"name" : "Schultz",
"surname" : "French",
"contact" : {"email" : "schult...@orbean.com"},},
"lodging" : {
"reviews" : [
{
"reviewer" : "Mariana Ayers",
"date" : "2014-04-13T08:30:02 -02:00",
"cleanliness" : NumberInt(9),
"location" : NumberInt(6),
"food" : NumberInt(4)},
{
"reviewer" : "Cole Simmons",
"date" : "2015-05-07T06:00:09 -02:00",
"cleanliness" : NumberInt(1),
"location" : NumberInt(3),
"food" : NumberInt(2)}]
"reviews_grades" : [
{"cleanliness" : NumberInt(9)},
{"location" : NumberInt(6)},
{"food" : NumberInt(4)},
{"cleanliness" : NumberInt(1)},
{"location" : NumberInt(3)},
{"food" : NumberInt(2)}}]}
}{"registered" : ISODate("2019-05-22T23:46:44.767Z"),"establishment_name" : "Bel Air","host" : {"name" : "Bob","surname" : "French-Smith","contact" : {"email" : "b...@bfs.info"
}},"lodging" : {"reviews" : [{"reviewer" : "Mariana Ayers",
"reviewed_date" : ISODate("2018-05-13T00:00:00Z"),"cleanliness" : 9,"location" : 7,"food" : 6},{"reviewer" : "Porter Tickseed","reviewed_date" : ISODate("2018-06-21T00:00:00Z"),"cleanliness" : 9,"location" : 2,"food" : 1},{"reviewer" : "Sophie Gilrecht","reviewed_date" : ISODate("2018-07-01T00:00:00Z"),    "cleanliness" : 9,"location" : 8,"food" : 0}]}}
db.better1.aggregate( [{ "$addFields" : { "reviewsCount" : { "$size" : "$lodging.reviews" } } },{ "$match" : { "reviewsCount" : { "$gt" : 2 } } },{ "$project" : { "_id" : 0, "establishment_name" : 1, "lodging" : 1 } },{ "$unwind" : "$lodging.reviews" },{ "$group" : { "_id" : "$establishment_name", "reviews" : { "$sum" : 1 }, "avgClean" : { "$avg" : "$lodging.reviews.cleanliness" }, "avgLocation" : { "$avg" : "$lodging.reviews.location" }, "avgFood" : { "$avg" : "$lodging.reviews.food" } } }, { "$sort" : { "avgClean" : -1 } }] )db.better1.find( { "lodging.reviews.2" : { "$exists" : true } }, { "establishment_name" : 1, "lodging.reviews.cleanliness" : 1, "lodging.reviews.location" : 1, "lodging.reviews.food" : 1, "_id" : 0 } ).pretty()