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

时间:2021-01-19 02:54:24

I hope to find the average rating score of book reviews by book id/title.

我希望通过图书ID /标题找到书评的平均评分。

Book schema:

书籍架构:

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 schema:

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);

Sample documents for Book:

Book的示例文档:

{
    "_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"
    }
}

Sample document for BookReview:

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"
    }
}

There are two ratings (5 and 4) for book with id "5ac639554bc06e0014b4f214" and one rating (5) for book with id "5ac639554bc06e0014b4f215". I hope to find the average rating of the two books. Something looks like the following:

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

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

I am new to Node.js and Mongoose and have no idea how to do this. Thank you for your help

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

1 个解决方案

#1


2  

The query is as simple as:

查询非常简单:

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

And the output is:

输出是:

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

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

Few things to point out in your data set:

您的数据集中很少指出:

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

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

This is as simple as writing in the form:

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

"_book" : ObjectId("5ac639554bc06e0014b4f215") or

“_book”:ObjectId(“5ac639554bc06e0014b4f215”)或

"_id" : ObjectId("5ac639554bc06e0014b4f214")

“_id”:ObjectId(“5ac639554bc06e0014b4f214”)

Now, how to do in node.js, pretty much simple since you already have the query. I will leave that job to you: refer here

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

#1


2  

The query is as simple as:

查询非常简单:

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

And the output is:

输出是:

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

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

Few things to point out in your data set:

您的数据集中很少指出:

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

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

This is as simple as writing in the form:

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

"_book" : ObjectId("5ac639554bc06e0014b4f215") or

“_book”:ObjectId(“5ac639554bc06e0014b4f215”)或

"_id" : ObjectId("5ac639554bc06e0014b4f214")

“_id”:ObjectId(“5ac639554bc06e0014b4f214”)

Now, how to do in node.js, pretty much simple since you already have the query. I will leave that job to you: refer here

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