admin管理员组文章数量:1424079
I have two models: User
and Track
.
User
can plete and get points for every Track
.
User schema:
let userSchema = new mongoose.Schema({
name: {type: String, required: true},
pletedTracks: [{
type: mongoose.Schema.ObjectId,
ref: 'Track',
}],
});
Track schema:
let trackSchema = new mongoose.Schema({
points: {
type: Number,
default: 0,
}
});
I am storing references to pleted tracks in User
model.
What I want to do is to query for multiple users, display only _ids of tracks pleted by each of them and add information about total points scored within a new score
property.
Example of a result I would like to achieve:
[
{
"pletedTracks": [
"5a9f14f18e04b3225953954c",
"5aa5a8fa7367661c03f57a99",
"5aa1aa5a7da9fd2946b01c04"
],
"name": "Person 1",
"score": 42,
"id": "5aaab2c63d1a9d182b183a01"
},
{
"pletedTracks": [],
"name": "Person 2",
"score": 0,
"id": "5aaab602c5bdc411d3cef3bd"
}
]
I tried to create virtual that would return the score but it does not work as userpletedTracks
are just ObjectIds
userSchema.virtual('score').get(function() {
return thispletedTracks.reduce((acc, doc) => acc + doc.points, 0);
});
I have two models: User
and Track
.
User
can plete and get points for every Track
.
User schema:
let userSchema = new mongoose.Schema({
name: {type: String, required: true},
pletedTracks: [{
type: mongoose.Schema.ObjectId,
ref: 'Track',
}],
});
Track schema:
let trackSchema = new mongoose.Schema({
points: {
type: Number,
default: 0,
}
});
I am storing references to pleted tracks in User
model.
What I want to do is to query for multiple users, display only _ids of tracks pleted by each of them and add information about total points scored within a new score
property.
Example of a result I would like to achieve:
[
{
"pletedTracks": [
"5a9f14f18e04b3225953954c",
"5aa5a8fa7367661c03f57a99",
"5aa1aa5a7da9fd2946b01c04"
],
"name": "Person 1",
"score": 42,
"id": "5aaab2c63d1a9d182b183a01"
},
{
"pletedTracks": [],
"name": "Person 2",
"score": 0,
"id": "5aaab602c5bdc411d3cef3bd"
}
]
I tried to create virtual that would return the score but it does not work as user.pletedTracks
are just ObjectIds
userSchema.virtual('score').get(function() {
return this.pletedTracks.reduce((acc, doc) => acc + doc.points, 0);
});
Share
Improve this question
edited Mar 16, 2018 at 21:21
umat
asked Mar 16, 2018 at 21:08
umatumat
7252 gold badges13 silver badges27 bronze badges
1 Answer
Reset to default 6You can try below aggregation in 3.4.
$lookup
to look up the points for pleted tracks followed by $addFields
to return a score field summing the points and $project
with exclusion to drop the lookup-data field from response.
UserModel.aggregate([
{"$lookup":{
"from":"tracks", // name of the foreign collection
"localField":"pletedTracks",
"foreignField":"_id",
"as":"lookup-data"
}},
{"$addFields":{
"score":{
"$sum":"$lookup-data.points"
}
}},
{"$project":{"lookup-data":0}}
])
本文标签: javascriptMongoose sum fields from populated documentsStack Overflow
版权声明:本文标题:javascript - Mongoose sum fields from populated documents - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745380148a2656121.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论