对于NoSQL的兴高采烈我还是有点陌生,我从一个以MongoDB作为数据库的项目开始。
目标:打印至少有3个等级的住宿清单,大多数等级(半个或更多)都高于6。 内部住宿。
问题:我有很多评论。我想将这些对象合并成一个等级数组,并从每个对象中排除审阅者和日期。
整理完毕:然后,我将$ gt大于5的对象(所以6个或更多)推到isGtArray,并将wersa推到isLtArray。然后,我可以算出是否确实有一半以上的分数高于6,并在isMostOfTheGradesMoreThan_6中进行投影,并使用$ match将它们打印出来。 到现在为止:解决了住宿等级至少为3 的第一部分。 非常感谢您的帮助。
现在输入代码:
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数据
{
"_id" : "5bf31f8c20b3acacc2194faa",
"registered" : "2016-04-22T08:54:50 -02:00",
"host" : {
"name" : "Schultz",
"surname" : "French",
"contact" : {"email" : "schultzfrench@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)}]}
}
解决方案
{
"_id" : "5bf31f8c20b3acacc2194faa",
"registered" : "2016-04-22T08:54:50 -02:00",
"host" : {
"name" : "Schultz",
"surname" : "French",
"contact" : {"email" : "schultzfrench@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)}}]}
}