Mongodb aggregation, how to combine or merge fields then count

Vincent

I have property(home) data in a collection. Each home can have multiple owners and is stored as separate fields for example {home: 1, owner1: Fred, owner2: Jason, owner3: Stan} I want to get the list of top X owner names. Being new to mongodb I was able to adapt a basic example to aggregate and count one field (owner1) but I don't see how to combine the names from owner1, owner2, owner3 and then count.

What I have now using pymongo

x = data2009.aggregate([
    { "$group": { "_id": "$OWNER1", "value": { "$sum": 1 } } },
    { "$sort": { "value": -1 } },
    { "$limit": 50 }
])

An example: DATA

{_id:1, home: 1, OWNER1: "Fred", OWNER2: "Stan", OWNER2: ""}
{_id:2, home: 2, OWNER1: "Jason", OWNER2: "Fred", OWNER2: "Stan"}
{_id:3, home: 3, OWNER1: "Fred", OWNER2: "Tim", OWNER2: "Stan"}
{_id:4, home: 4, OWNER1: "Stan", OWNER2: "", OWNER2: ""}

Output would be

{
{_id: Stan, value: 4}
{_id: Fred, value: 3}
{_id: Jason, value: 1}
{_id: Tim, value: 1}
}
Gary Murakami

As far as I know, reshaping multiple fields into an array value is not yet possible with the aggregation framework (one element into an array containing the one element is possible, but not useful). The following New Feature addresses this shortcoming, please vote for it - "$array aggregation expression" - https://jira.mongodb.org/browse/SERVER-8141

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Mongodb aggregation, how to combine or merge fields then count

From Dev

How to count fields in Mongodb Aggregation

From Dev

How to count fields in Mongodb Aggregation

From Dev

Merge object fields aggregation

From Dev

count documents by specific nested fields values with aggregation-framework in MongoDB

From Dev

Spring Data MongoDB: How to describe aggregation $merge with Spring Aggregation?

From Dev

Mongodb aggregation $unwind then count

From Dev

MongoDB aggregation count

From Dev

MongoDB Java API - How to combine sample aggregation with a find query?

From Dev

Combine multiple groups in an aggregation in mongodb

From Dev

Elasticsearch how count values from multiple document fields using aggregation

From Dev

Elasticsearch how count values from multiple document fields using aggregation

From Dev

Aggregation Conditional Count on Present Fields

From Dev

How to combine PIVOT and aggregation?

From Dev

How to include existing fields in mongodb aggregation $project operation

From Dev

How to count from two fields in mongoDB

From Dev

MongoDB Aggregation - Merge arrays of an object

From Dev

MongoDB: Aggregation, group by several fields

From Dev

Merging array fields in MongoDB aggregation

From Dev

MongoDB aggregation $divide computed fields

From Dev

MongoDB Aggregation is slow on Indexed fields

From Dev

How do i count number of referals using aggregation in mongoDB

From Dev

merge (combine) fields from a query

From Dev

MongoDB - Aggregation Framework (Total Count)

From Dev

MongoDB - Aggregation Framework (Total Count)

From Java

How to combine mongodb original output of query with some new fields?

From Dev

Merge a sub document to main document with mongodb aggregation

From Dev

Merge a sub document to main document with mongodb aggregation

From Dev

Mongodb - grouping several fields using aggregation framework

Related Related

  1. 1

    Mongodb aggregation, how to combine or merge fields then count

  2. 2

    How to count fields in Mongodb Aggregation

  3. 3

    How to count fields in Mongodb Aggregation

  4. 4

    Merge object fields aggregation

  5. 5

    count documents by specific nested fields values with aggregation-framework in MongoDB

  6. 6

    Spring Data MongoDB: How to describe aggregation $merge with Spring Aggregation?

  7. 7

    Mongodb aggregation $unwind then count

  8. 8

    MongoDB aggregation count

  9. 9

    MongoDB Java API - How to combine sample aggregation with a find query?

  10. 10

    Combine multiple groups in an aggregation in mongodb

  11. 11

    Elasticsearch how count values from multiple document fields using aggregation

  12. 12

    Elasticsearch how count values from multiple document fields using aggregation

  13. 13

    Aggregation Conditional Count on Present Fields

  14. 14

    How to combine PIVOT and aggregation?

  15. 15

    How to include existing fields in mongodb aggregation $project operation

  16. 16

    How to count from two fields in mongoDB

  17. 17

    MongoDB Aggregation - Merge arrays of an object

  18. 18

    MongoDB: Aggregation, group by several fields

  19. 19

    Merging array fields in MongoDB aggregation

  20. 20

    MongoDB aggregation $divide computed fields

  21. 21

    MongoDB Aggregation is slow on Indexed fields

  22. 22

    How do i count number of referals using aggregation in mongoDB

  23. 23

    merge (combine) fields from a query

  24. 24

    MongoDB - Aggregation Framework (Total Count)

  25. 25

    MongoDB - Aggregation Framework (Total Count)

  26. 26

    How to combine mongodb original output of query with some new fields?

  27. 27

    Merge a sub document to main document with mongodb aggregation

  28. 28

    Merge a sub document to main document with mongodb aggregation

  29. 29

    Mongodb - grouping several fields using aggregation framework

HotTag

Archive