如何从参考模式中找到平均值?

时间:2018-04-13 10:25:43

标签: node.js mongodb mongoose aggregation-framework

我希望按书名/标题找到书评的平均评分。

图书架构:

const mongoose = require('mongoose');
var mongoosePaginate = require('mongoose-paginate');
const { Schema } = mongoose;

const bookSchema = new Schema({
  title: String,
  thumbnail: String,
  authors: [String],
  price: Number,
  edition: String,
  publisher: {
    type: Schema.Types.ObjectId,
    ref: 'Publisher'
  },
  _createdBy: { type: Schema.Types.ObjectId, ref: 'User' },
  bookReviews: [{
    type: Schema.Types.ObjectId,
    ref: 'BookReview'
  }]
}, { timestamps: true });

bookSchema.plugin(mongoosePaginate);

mongoose.model('books', bookSchema);

BookReview架构:

const mongoose = require('mongoose');
const { Schema } = mongoose;

const bookReviewSchema = new Schema({
  message: String,
  rating: { type: Number, min: 0, max: 5, default: 0 },
  _createdBy : { type: Schema.Types.ObjectId, ref: 'User' },
  _book: { type: Schema.Types.ObjectId, ref: 'Book' }
}, { timestamps: true });

mongoose.model('bookreviews', bookReviewSchema);

预订的示例文档:

{
    "_id": {
        "$oid": "5ac639554bc06e0014b4f214"
    },
    "authors": [
        "L.L. Cheng",
        "Y.W. Faan"
    ],
    "bookReviews": [
        {
            "$oid": "5acce656e4b54b00142cae0c"
        },
        {
            "$oid": "5acce65fe4b54b00142cae0e"
        },
        {
            "$oid": "5acedbbf56bd3c001467672e"
        }
    ],
    "title": "Aristo Exam Success Series",
    "thumbnail": "https://res.cloudinary.com/v1522940244/9789888361175_ao48tu.jpg",
    "_createdBy": {
        "$oid": "5ac61a414d19c30014232777"
    }
}

BookReview 的示例文档:

{
    "_id": {
        "$oid": "5acce656e4b54b00142cae0c"
    },
    "rating": 5,
    "message": "excellent",
    "_book": {
        "$oid": "5ac639554bc06e0014b4f214"
    }
}

{
    "_id": {
        "$oid": "5acce65fe4b54b00142cae0e"
    },
    "rating": 4,
    "message": "good",
    "_book": {
        "$oid": "5ac639554bc06e0014b4f214"
    }
}

{
    "_id": {
        "$oid": "5acedbbf56bd3c001467672e"
    },
    "rating": 5,
    "message": "ok",
    "_book": {
        "$oid": "5ac639554bc06e0014b4f215"
    }
}

对于ID为“5ac639554bc06e0014b4f214”的书籍,有两个等级(5和4),对于ID为“5ac639554bc06e0014b4f215”的书,有一个等级(5)。我希望找到这两本书的平均评分。看起来如下:

[
    {
        "book": "5ac639554bc06e0014b4f214",
        "averageRating": 4.5
    },
    {
        "book": "5ac639554bc06e0014b4f215",
        "averageRating": 5
    }
]

我是Node.js和Mongoose的新手,不知道如何做到这一点。谢谢你的帮助

1 个答案:

答案 0 :(得分:2)

查询非常简单:

db.BookReview.aggregate([
 {$group:{_id:"$_book", averageRating:{$avg:"$rating"}}}
])

输出是:

/* 1 */
{
"_id" : ObjectId("5ac639554bc06e0014b4f215"),
"averageRating" : 5.0
}

/* 2 */
{
"_id" : ObjectId("5ac639554bc06e0014b4f214"),
 "averageRating" : 4.5
}

您的数据集中很少指出:

"_book": {
    "$oid": "5ac639554bc06e0014b4f215"
}

"_id": {
    "$oid": "5ac639554bc06e0014b4f214"
}

这就像在表格中写一样简单:

"_book" : ObjectId("5ac639554bc06e0014b4f215")

"_id" : ObjectId("5ac639554bc06e0014b4f214")

现在,如何在node.js中做,非常简单,因为你已经有了查询。我将把这份工作留给你:refer here