MongoDB project into an array

JBone

I have array of objects (nested), and I want to pull a couple of the elements from those nested objects, and return these fields in an array. Below is my document structure (mocked it from original data)

"detailLine": [
    {
        "name": "first",
        "value": {
            "lineNumber": 1,
            "subLineCode": " ",
            "detailLineCharges": {
                "allowedAmount": {
                    "amount": "11111",
                    "reasonCode": “aaaah”
                }
            }
        }
    },
    {
        "name": "first",
        "value": {
            "detailLineCharges": {
                "allowedAmount": {
                    "amount": "22222",
                    "reasonCode": “BBBB”
                }
            }
        }
    }
]

I would like to see my results something like this

details: [
  {
    amount:”11111”,
    reasonCode : “aaaah”
  },
  {
    amount : “22222”,
    reasonCode : “BBBB”
  }
]

I tried

 db.collection.aggregate([
   {
     $match: {
       _id: "123456"
     }
   }, 
   {
     $project: {
       details: {
         $push {
           amount: "$detailLine.value.detailLineCharges.allowedAmount.amount",
           reasoncode: "$detailLine.value.detailLineCharges.allowedAmount.reasonCode"
         }
       }
     }
   }
])

but I am getting this error

Error: command failed: { "ok" : 0, "errmsg" : "invalid operator '$push'", "code" : 15999 } : aggregate failed :

I want to use $project because I am using it to pull too many other fields so that is why I did not try $group for this one particular scenario.

Can anyone help how to get those two fields in an Array?

styvane

Well, all you need is $project your documents and "reshape" them using the $map operator.

db.coll.aggregate( 
    [ 
        { "$match": { "_id": "123456" } },
        { "$project": { 
            "detailLine": { 
                "$map": { 
                    "input": "$detailLine", 
                    "as": "dline", 
                    "in": { 
                        "amount": "$$dline.value.detailLineCharges.allowedAmount.amount", 
                        "reasonCode": "$$dline.value.detailLineCharges.allowedAmount.reasonCode" 
                    } 
                } 
           } 
        }} 
    ]
)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Project array content with mongodb

From Dev

Project index array mongodb

From Dev

MongoDB - Project array subfield with aggregation

From Dev

MongoDB: Project using array fields

From Dev

MongoDB project extra field in array

From Dev

mongodb $project results in an array of array values

From Dev

MongoDB - Project only the matching element in an array

From Dev

Project the size of a set type array in mongodb

From Dev

MongoDb query into poco project a single array item

From Dev

How to project array index after unwinding an array with MongoDB aggregation framework

From Dev

Project first item in an array to new field (MongoDB aggregation)

From Dev

Mongodb aggregation $project get array position element field value

From Dev

mongodb aggregation pipeline, fold an array property in `$project`ion

From Dev

MongoDB project updated record in a nested array in "findAndModify" query

From Dev

mongodb aggregation, is it possible to add entries to an array during $project

From Dev

MongoDB aggregation: Project separate document fields into a single array field

From Dev

mongodb aggregation, is it possible to add entries to an array during $project

From Dev

MongoDB aggregation: Project separate document fields into a single array field

From Dev

Mongodb: project to return the field in array of object without using unwind

From Dev

MongoDB - Project specific element from array (big data)

From Java

$project in $lookup mongodb

From Dev

MongoDB $project and index usage

From Dev

MongoDB aggregate selective project

From Dev

Mongodb query project

From Dev

Transform dates with $project in MongoDB

From Dev

$Project inside find mongodb

From Dev

mongodb - $lookup not working with $project

From Dev

$project query in MongoDB with mongoose

From Dev

MongoDB: updating an array in array

Related Related

  1. 1

    Project array content with mongodb

  2. 2

    Project index array mongodb

  3. 3

    MongoDB - Project array subfield with aggregation

  4. 4

    MongoDB: Project using array fields

  5. 5

    MongoDB project extra field in array

  6. 6

    mongodb $project results in an array of array values

  7. 7

    MongoDB - Project only the matching element in an array

  8. 8

    Project the size of a set type array in mongodb

  9. 9

    MongoDb query into poco project a single array item

  10. 10

    How to project array index after unwinding an array with MongoDB aggregation framework

  11. 11

    Project first item in an array to new field (MongoDB aggregation)

  12. 12

    Mongodb aggregation $project get array position element field value

  13. 13

    mongodb aggregation pipeline, fold an array property in `$project`ion

  14. 14

    MongoDB project updated record in a nested array in "findAndModify" query

  15. 15

    mongodb aggregation, is it possible to add entries to an array during $project

  16. 16

    MongoDB aggregation: Project separate document fields into a single array field

  17. 17

    mongodb aggregation, is it possible to add entries to an array during $project

  18. 18

    MongoDB aggregation: Project separate document fields into a single array field

  19. 19

    Mongodb: project to return the field in array of object without using unwind

  20. 20

    MongoDB - Project specific element from array (big data)

  21. 21

    $project in $lookup mongodb

  22. 22

    MongoDB $project and index usage

  23. 23

    MongoDB aggregate selective project

  24. 24

    Mongodb query project

  25. 25

    Transform dates with $project in MongoDB

  26. 26

    $Project inside find mongodb

  27. 27

    mongodb - $lookup not working with $project

  28. 28

    $project query in MongoDB with mongoose

  29. 29

    MongoDB: updating an array in array

HotTag

Archive