简体   繁体   中英

MongoDB average of a field for DBRef Rating

I have a Document named Rating to which other documents Video & User are attached.

I am trying to get an avg rating for a supplied video id.

My Documents definitions are as follows

@Document(value = "rating")
@NoArgsConstructor
public class Rating extends Auditable<String> {

    @Getter
    @Setter
    @DBRef
    private Video video;

    @Getter
    @Setter
    @DBRef
    private User user;

    @Getter
    @Setter
    private int rating;

}



@Document(collection = "videos")
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Video extends Auditable<String> {
//Field omitted
}

@Document(collection = "users")
@NoArgsConstructor
public class User extends Auditable<String> {

    @Getter
    @Setter
    private String username;
}


@NoArgsConstructor
@AllArgsConstructor
public class RatingResponse {

    @Getter
    @Setter
    private String videoId;

    @Getter
    @Setter
    private Integer rating;
}

My Rating Code block looks like this

Aggregation aggregation = Aggregation.newAggregation(
                Aggregation.match(Criteria.where("video.$id").is(video.getId())),
                Aggregation.group(Fields.fields("id"))
                        .avg("rating").as("rating")
        );

        AggregationResults<RatingResponse> results = mongoTemplate.aggregate(aggregation, Rating.class, RatingResponse.class);


        RatingResponse uniqueMappedResult = results.getUniqueMappedResult();

When I execute this in the test, I get following JSON representation:

{ "aggregate" : "__collection__", "pipeline" : [{ "$match" : { "video.$id" : "5dbe5ce141bb7d2e137eee0b" } }, { "$group" : { "_id" : "$id", "rating" : { "$avg" : "$rating" } } }] }

I am executing these commands from spring boot framework, if there exists a better option, please kindly let me know.

I don't know if this is the right way to query, please help me solve this query. Thank you.

Edited Added Image for reference

在此处输入图像描述

Video documents are stored as ObjectId("5dbe5ce141bb7d2e137eee0b") , while you are searching as String ( "video.$id": "5dbe5ce141bb7d2e137eee0b" )

{
  "_id": ObjectId("5a934e000102030405000002"),
  "video": {
    "$ref": "video",
    "$id": ObjectId("5dbe5ce141bb7d2e137eee0b")
  },
  user: {},
  rating: 100
},
{
  "_id": ObjectId("5a934e000102030405000004"),
  "video": {
    "$ref": "video",
    "$id": ObjectId("5dbe5ce141bb7d2e137eee0b")
  },
  user: {},
  rating: 40
}

db.rating.aggregate([
  {
    "$match": {
      "video.$id": {
        "$oid": "5dbe5ce141bb7d2e137eee0b"
      }
    }
  },
  {
    "$group": {
      "_id": "$id",
      "rating": {
        "$avg": "$rating"
      }
    }
  }
])

[
  {
    "_id": null,
    "rating": 70
  }
]

MongoPlayground

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM