如何在猫鼬中找到恒星评级的平均值
我已经在Mern Stack上从事我的最后一年大学项目。 我的问题提到了波纹管,请帮助我。
const Organization = mongoose.Schema({
name: {
type: String,
required: true
},
email: {
type: String,
required: true
},
current_rating: {
type: Number,
default: 0
},
rating: [{
user: {
type: 'ObjectId',
ref:'user'
},
user_name:String,
experience: String,
feedback: String,
star: Number,
date: {
type: Date,
default:Date.now
}
}]
});
例如,组织集合将看起来像这样
[
{
name: 'abc.com'
email: '[email protected]'
current_rating: 0,
rating: [
{
user: new ObjectId("623cb8bec55d003c2d9dd3da"),
user_name: 'rupesh',
experience: 'very good',
feedback: 'In publishing and graphic design, Lorem ipsum',
star: 3.5,
_id: new ObjectId("6267d9301c4ceac2d4e6956a"),
date: 2022-04-26T11:36:16.656Z
},
{
user: new ObjectId("623cb8bec55d003c2d9dd3da"),
user_name: 'rupesh',
experience: 'very good',
feedback: 'In publishing and graphic design, Lorem ipsum',
star: 4,
_id: new ObjectId("6267d9d6e6296ebf5e289155"),
date: 2022-04-26T11:39:02.918Z
},
{
// some content
},
.
.
.
//and many more rating for this organization
},
{
// data for other organization
}
]
但它在猫鼬soserertionerror中不起作用
const updatedCurrentRating = await Organization.findOneAndUpdate({ _id: req.params.org_id }, {
$set: { current_rating: { $avg: 'rating.$.star' } }
}, {
new: true,
useFindAndModify: true
});
console.log(updatedCurrentRating);
,我正在查询中获得该查询
CastError: Cast to Number failed for value "{ '$avg': 'rating.$.star' }" (type Object) at path "current_rating"
at model.Query.exec (D:\react\project For Inter\server\node_modules\mongoose\lib\query.js:4650:21)
at model.Query.Query.then (D:\react\project For Inter\server\node_modules\mongoose\lib\query.js:4749:15)
at runMicrotasks (<anonymous>)
at processTicksAndRejections (internal/process/task_queues.js:93:5) {
messageFormat: undefined,
stringValue: `"{ '$avg': 'rating.$.star' }"`,
kind: 'Number',
value: { '$avg': 'rating.$.star' },
path: 'current_rating',
reason: AssertionError [ERR_ASSERTION]: The expression evaluated to a falsy value:
assert.ok(!isNaN(val))
at castNumber (D:\react\project For Inter\server\node_modules\mongoose\lib\cast\number.js:27:10)
at SchemaNumber.cast (D:\react\project For Inter\server\node_modules\mongoose\lib\schema\number.js:380:12)
at SchemaNumber.SchemaType.applySetters (D:\react\project For Inter\server\node_modules\mongoose\lib\schematype.js:1179:12)
at SchemaNumber.SchemaType._castForQuery (D:\react\project For Inter\server\node_modules\mongoose\lib\schematype.js:1613:15)
at SchemaNumber.castForQuery (D:\react\project For Inter\server\node_modules\mongoose\lib\schema\number.js:434:14)
at SchemaNumber.SchemaType.castForQueryWrapper (D:\react\project For Inter\server\node_modules\mongoose\lib\schematype.js:1580:20)
at castUpdateVal (D:\react\project For Inter\server\node_modules\mongoose\lib\helpers\query\castUpdate.js:527:19)
at walkUpdatePath (D:\react\project For Inter\server\node_modules\mongoose\lib\helpers\query\castUpdate.js:260:22)
at castUpdate (D:\react\project For Inter\server\node_modules\mongoose\lib\helpers\query\castUpdate.js:96:7)
at model.Query._castUpdate (D:\react\project For Inter\server\node_modules\mongoose\lib\query.js:4859:10) {
generatedMessage: true,
code: 'ERR_ASSERTION',
actual: false,
expected: true,
operator: '=='
},
valueType: 'Object'
}
我正在尝试此查询, 进入current_rating
谢谢。
I have working on my final year college project On MERN stack.
my problem is mentioned bellow please help me.
const Organization = mongoose.Schema({
name: {
type: String,
required: true
},
email: {
type: String,
required: true
},
current_rating: {
type: Number,
default: 0
},
rating: [{
user: {
type: 'ObjectId',
ref:'user'
},
user_name:String,
experience: String,
feedback: String,
star: Number,
date: {
type: Date,
default:Date.now
}
}]
});
for example Organization collection will look like this
[
{
name: 'abc.com'
email: '[email protected]'
current_rating: 0,
rating: [
{
user: new ObjectId("623cb8bec55d003c2d9dd3da"),
user_name: 'rupesh',
experience: 'very good',
feedback: 'In publishing and graphic design, Lorem ipsum',
star: 3.5,
_id: new ObjectId("6267d9301c4ceac2d4e6956a"),
date: 2022-04-26T11:36:16.656Z
},
{
user: new ObjectId("623cb8bec55d003c2d9dd3da"),
user_name: 'rupesh',
experience: 'very good',
feedback: 'In publishing and graphic design, Lorem ipsum',
star: 4,
_id: new ObjectId("6267d9d6e6296ebf5e289155"),
date: 2022-04-26T11:39:02.918Z
},
{
// some content
},
.
.
.
//and many more rating for this organization
},
{
// data for other organization
}
]
i was trying this query but it is not working in mongoose
const updatedCurrentRating = await Organization.findOneAndUpdate({ _id: req.params.org_id }, {
$set: { current_rating: { $avg: 'rating.$.star' } }
}, {
new: true,
useFindAndModify: true
});
console.log(updatedCurrentRating);
AssertionError, which i am geting in this query
CastError: Cast to Number failed for value "{ '$avg': 'rating.$.star' }" (type Object) at path "current_rating"
at model.Query.exec (D:\react\project For Inter\server\node_modules\mongoose\lib\query.js:4650:21)
at model.Query.Query.then (D:\react\project For Inter\server\node_modules\mongoose\lib\query.js:4749:15)
at runMicrotasks (<anonymous>)
at processTicksAndRejections (internal/process/task_queues.js:93:5) {
messageFormat: undefined,
stringValue: `"{ '$avg': 'rating.$.star' }"`,
kind: 'Number',
value: { '$avg': 'rating.$.star' },
path: 'current_rating',
reason: AssertionError [ERR_ASSERTION]: The expression evaluated to a falsy value:
assert.ok(!isNaN(val))
at castNumber (D:\react\project For Inter\server\node_modules\mongoose\lib\cast\number.js:27:10)
at SchemaNumber.cast (D:\react\project For Inter\server\node_modules\mongoose\lib\schema\number.js:380:12)
at SchemaNumber.SchemaType.applySetters (D:\react\project For Inter\server\node_modules\mongoose\lib\schematype.js:1179:12)
at SchemaNumber.SchemaType._castForQuery (D:\react\project For Inter\server\node_modules\mongoose\lib\schematype.js:1613:15)
at SchemaNumber.castForQuery (D:\react\project For Inter\server\node_modules\mongoose\lib\schema\number.js:434:14)
at SchemaNumber.SchemaType.castForQueryWrapper (D:\react\project For Inter\server\node_modules\mongoose\lib\schematype.js:1580:20)
at castUpdateVal (D:\react\project For Inter\server\node_modules\mongoose\lib\helpers\query\castUpdate.js:527:19)
at walkUpdatePath (D:\react\project For Inter\server\node_modules\mongoose\lib\helpers\query\castUpdate.js:260:22)
at castUpdate (D:\react\project For Inter\server\node_modules\mongoose\lib\helpers\query\castUpdate.js:96:7)
at model.Query._castUpdate (D:\react\project For Inter\server\node_modules\mongoose\lib\query.js:4859:10) {
generatedMessage: true,
code: 'ERR_ASSERTION',
actual: false,
expected: true,
operator: '=='
},
valueType: 'Object'
}
please tell me how i can get Average star
for organization and store it into current_rating
Thank You.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
$ avg
是一个聚合操作员,您不能在普通更新上使用它。但是您可以这样进行管道更新(它具有
[{$ set ...]
)另外,您不能在聚合操作员上使用
$。star
,因此路径也会更改。playmongo
$avg
is an aggregate operator you cant use it on an normal update.But you can do a pipeline update like this (it has
[ {$set ...]
)Also you cant use the
$.star
on aggregation operators so the path is changed also.Playmongo