$sum a field in an array - Query that results documents above a certain value

Jona Becher

I created the following schema :

const videoSchema = createSchema(
    link: Type.string({required: true}),  
    description: Type.string({required: true}),
    answers: Type.array({required: true}).of({
         text: Type.string(), 
         likes: Type.number()
    })
);
export const videoModel = typedModel('video', videoSchema);

Now I want to write a query that only has videos with over 200 likes as a result. I've tried to work with .aggregate, $reduce and $sum, but I can't get it to work.

Does anyone know how to achieve this ? Thank you in advance!

whoami - fakeFaceTrueSoul

You can try below query :

db.collection.aggregate([
/** Add a field which holds count of likes for all answers for a video */
{
    $addFields: {
        likesCount: {
            $reduce: {
                input: "$answers.likes",
                initialValue: 0,
                in: {
                    $add: [
                        "$$value",
                        "$$this"
                    ]
                }
            }
        }
    }
},
/** filter to retain videos whose likes > 200 */
{
    $match: {
        likesCount: {
            $gt: 200
        }
    }
},
/** Remove additional field from documents */
{
    $project: {
        likesCount: 0
    }
  }
])

Test : MongoDB-Playground

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Filter results by the Last Array Entry Field Value

分類Dev

How to make a query using Mongoose that gets N results, but combines any documents it finds that meet certain criteria?

分類Dev

Given an array, use a function to sum up certain values depending on a different value in the array

分類Dev

Ranking a partial field match to a query above a complete match on a different field

分類Dev

print field that contain a certain value

分類Dev

Array.Sum() results in an overflow

分類Dev

How do I loop through certain records, check a query, and conditionally assign field value using VBA?

分類Dev

MongoDB - Find documents matching certain condition for unknown field keys

分類Dev

How to sort documents based on length of an Array field

分類Dev

Find Documents Where a Field Compares with Another in an Array

分類Dev

Extracting continuous values above a certain threshold in a Numpy array

分類Dev

Returning certain amount of documents from elasticsearch query in java

分類Dev

ElasticSearch - Copy one field value to other field for all documents

分類Dev

Angular Ionic - Validating a form only if a certain field is a certain value

分類Dev

Sort query results based desc value of nested subdocument within array Mongoose/Mongodb

分類Dev

Sum of differences over a JSONB array field in CockroachDB

分類Dev

How to find all documents where the value of one field matches that of another

分類Dev

How pull only 6 documents and with the value true in a field?

分類Dev

Mongo aggregation: Count documents based on array field matching condition

分類Dev

Finding common values of array field of two documents in ElasticSearch

分類Dev

Filtering out documents with a specific item in an array field using mongoid

分類Dev

Oracle - How to change field value based on certain value

分類Dev

MYSQLI Query sum column duplicates value on rows

分類Dev

How to save all query results into an array

分類Dev

Query Mongodb Sum of firsts element of an array of objects

分類Dev

Sql - SELECT rows until the sum of a row is a certain value

分類Dev

NetLogo - plot sum of all turtles with value in certain range

分類Dev

How to find some arrays with certain value and sum their values?

分類Dev

Filter query on array of embedded documents(3 levels) in mongoDb

Related 関連記事

  1. 1

    Filter results by the Last Array Entry Field Value

  2. 2

    How to make a query using Mongoose that gets N results, but combines any documents it finds that meet certain criteria?

  3. 3

    Given an array, use a function to sum up certain values depending on a different value in the array

  4. 4

    Ranking a partial field match to a query above a complete match on a different field

  5. 5

    print field that contain a certain value

  6. 6

    Array.Sum() results in an overflow

  7. 7

    How do I loop through certain records, check a query, and conditionally assign field value using VBA?

  8. 8

    MongoDB - Find documents matching certain condition for unknown field keys

  9. 9

    How to sort documents based on length of an Array field

  10. 10

    Find Documents Where a Field Compares with Another in an Array

  11. 11

    Extracting continuous values above a certain threshold in a Numpy array

  12. 12

    Returning certain amount of documents from elasticsearch query in java

  13. 13

    ElasticSearch - Copy one field value to other field for all documents

  14. 14

    Angular Ionic - Validating a form only if a certain field is a certain value

  15. 15

    Sort query results based desc value of nested subdocument within array Mongoose/Mongodb

  16. 16

    Sum of differences over a JSONB array field in CockroachDB

  17. 17

    How to find all documents where the value of one field matches that of another

  18. 18

    How pull only 6 documents and with the value true in a field?

  19. 19

    Mongo aggregation: Count documents based on array field matching condition

  20. 20

    Finding common values of array field of two documents in ElasticSearch

  21. 21

    Filtering out documents with a specific item in an array field using mongoid

  22. 22

    Oracle - How to change field value based on certain value

  23. 23

    MYSQLI Query sum column duplicates value on rows

  24. 24

    How to save all query results into an array

  25. 25

    Query Mongodb Sum of firsts element of an array of objects

  26. 26

    Sql - SELECT rows until the sum of a row is a certain value

  27. 27

    NetLogo - plot sum of all turtles with value in certain range

  28. 28

    How to find some arrays with certain value and sum their values?

  29. 29

    Filter query on array of embedded documents(3 levels) in mongoDb

ホットタグ

アーカイブ