Complexity using `populate` with` groupBy` and `sum` using Waterline query in Sails.js

I am having a strange problem with methods groupBy

and sum

when using with populate

. I have a set of related models: User, Source (books, etc.), Recommendation, and Discipline (i.e. academic discipline).

I tried the following waterline request:

Recommendation.find({
    where: {discipline: '5559ea07dfa9bd0499f9f799'}, 
    groupBy: ['source'], sum: ['rating']
}).exec(console.log);

      

This worked well, returning:

[
    {
        "source": "5571e72ab50f0c3c49fe19f6",
        "rating": 4
    },
    {
        "source": "5571b88c51861950360fac1c",
        "rating": 12
    }
]

      

Then I tried it with populate

in source because I wanted to have complete raw data associated with each recommendation. So I tried:

Recommendation.find({
    where: {discipline: '5559ea07dfa9bd0499f9f799'}, 
    groupBy: ['source'], sum: ['rating']
}).populate('source').exec(console.log);

      

This gave something strange:

[
    {
        "source": {
            "type": "book",
            "identifiers": [
                {
                    "type": "ISBN_10",
                    "identifier": "1400823226"
                },
                {
                    "type": "ISBN_13",
                    "identifier": "9781400823222"
                }
            ],
            "title": "Patterns for America",
            "subtitle": "Modernism and the Concept of Culture",
            "publisher": "Princeton University Press",
            "year": 1999,
            "language": "en",
            "categories": [
                "Literary Criticism"
            ],
            "abstract": "In recent decades, historians and social theorists have given much thought to the concept of \"culture,\" its origins in Western thought, and its usefulness for social analysis. In this book, Susan Hegeman focuses on the term history in the United States in the first half of the twentieth century. She shows how, during this period, the term \"culture\" changed from being a technical term associated primarily with anthropology into a term of popular usage. She shows the connections between this movement of \"culture\" into the mainstream and the emergence of a distinctive \"American culture,\" with its own patterns, values, and beliefs. Hegeman points to the significant similarities between the conceptions of culture produced by anthropologists Franz Boas, Edward Sapir, Ruth Benedict, and Margaret Mead, and a diversity of other intellectuals, including Randolph Bourne, Van Wyck Brooks, Waldo Frank, and Dwight Macdonald. Hegeman reveals how relativist anthropological ideas of human culture--which stressed the distance between modern centers and \"primitive\" peripheries--came into alliance with the evaluating judgments of artists and critics. This anthropological conception provided a spatial awareness that helped develop the notion of a specifically American \"culture.\" She also shows the connections between this new view of \"culture\" and the artistic work of the period by, among others, Sherwood Anderson, Jean Toomer, Thomas Hart Benton, Nathanael West, and James Agee and depicts in a new way the richness and complexity of the modernist milieu in the United States.",
            "imageLinks": {
                "smallThumbnail": "http://bks3.books.google.de/books/content?id=OwYWU2H3me4C&printsec=frontcover&img=1&zoom=5&edge=curl&source=gbs_api",
                "thumbnail": "http://bks3.books.google.de/books/content?id=OwYWU2H3me4C&printsec=frontcover&img=1&zoom=1&edge=curl&source=gbs_api"
            },
            "createdAt": "2015-06-05T14:56:12.714Z",
            "updatedAt": "2015-06-05T14:56:12.724Z",
            "id": "5571b88c51861950360fac1c"
        },
        "rating": 4
    },
    {
        "source": {
            "_bsontype": "ObjectID",
            "id": "Uq¸Q\u0019P6\u000f¬\u001c"
        },
        "rating": 12
    }
]

      

As you can see, rather than replacing the original ID with the full properties of the source, it added the source as an object in the returned array and then added another object with a new reference to the original ID and the sum of the rating summed. Oddly enough, he also bound only one of the rating values ​​to the original object, which he attached to the results.

This is what should be happening. I can't follow logic and it feels a bit buggy. Anyway, I would like to have a source in each returned row, along with a grand total. Can someone please explain what is going on here and if I make a mistake, maybe?

+3


source to share





All Articles