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

1.4k views Asked by At

I have run into a strange problem with the groupBy and sum methods when used with populate. I have a set of associated models: User, Source (books, etc), Recommendation, and Discipline (i.e. academic discipline).

I attempted the following waterline query:

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

This worked well, returning:

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

I then tried it with populate on the source because I wanted to have the full source data linked to the each recommendation. So I tried:

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

This yielded 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's 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 source id with the full source properties, it added the source as an object in the returned array, and then added another object with a new source id reference, and the summed rating amount. Strangely, it also attached just one of the rating values to the source object it appended to the results.

Is this what should be happening. I can't follow the logic and it feels a bit buggy. In any case, what I would like is to have the source within each returned row, alongside the summed total. Can anyone explain what's going on here, and if I made an error perhaps?

0

There are 0 answers