2

I'm going to make a query to get data with index of subarray which number is even number.

Input value:

{
    "_id" : ObjectId("5b3b206c4a25da19d05f41a2"),
    "numbers" : [ 
        {
            "name" : "Foobar",
            "lines" : [ 
                {
                    "number" : 6,
                }, 
                {
                    "number" : 11,

                },
                {
                    "number" : 15,

                },
                {
                    "number" : 8,
                }

            ]
        }
    ]
}

Desired output:

{
    "_id" : ObjectId("5b3b206c4a25da19d05f41a2"),
    "numbers" : [ 
        {
            "name" : "Foobar",
            "lines" : [ 
                {
                    "index"  : 0,
                    "number" : 6,
                },
                {
                    "index"  : 3,
                    "number" : 8,
                }
            ]
        }
    ]
}

The most important is to get the index of items of "lines".

1 Answer 1

3

You need to use $map to iterate over the first array and then $filter with the nested array.

db.collection.aggregate([
  { "$project": {
    "numbers": {
      "$map": {
        "input": "$numbers",
        "as": "nn",
        "in": {
          "name": "$$nn.name",
          "lines": {
            "$filter": {
              "input": {
                "$map": {
                  "input": "$$nn.lines",
                  "as": "ll",
                  "in": {
                    "number": "$$ll.number",
                    "index": { "$indexOfArray": ["$$nn.lines.number", "$$ll.number"] }
                  }
                }
              },
              "as": "ll",
              "cond": { "$eq": [{ "$mod": ["$$ll.number", 2] }, 0] }
            }
          }
        }
      }
    }
  }}
])

Even if you need some more aggregation trick

db.collection.aggregate([
  { "$project": {
    "numbers": {
      "$map": {
        "input": {
          "$map": {
            "input": "$numbers",
            "as": "nn",
            "in": {
              "name": "$$nn.name",
              "lines": {
                "$filter": {
                  "input": { "$zip": { "inputs": ["$$nn.lines", { "$range": [0, { "$size": "$$nn.lines" }] }] }},
                  "as": "ll",
                  "cond": {
                    "$let": {
                      "vars": { "num": { "$arrayElemAt": ["$$ll", 0] }},
                      "in": { "$eq": [{ "$mod": ["$$num.number", 2] }, 0] }
                    }
                  }
                }
              }
            }
          }
        },
        "as": "nn",
        "in": {
          "name": "$$nn.name",
          "lines": {
            "$map": {
              "input": "$$nn.lines",
              "as": "kk",
              "in": {
                "number": { "$arrayElemAt": ["$$kk.number", 0] },
                "index": { "$arrayElemAt": ["$$kk", 1] }
              }
            }
          }
        }
      }
    }
  }}
])

Finally both will give you similar Output

[
  {
    "_id": ObjectId("5b3b206c4a25da19d05f41a2"),
    "numbers": [
      {
        "name": "Foobar",
        "lines": [
          {
            "index": 0,
            "number": 6
          },
          {
            "index": 3,
            "number": 8
          }
        ]
      }
    ]
  }
]
Sign up to request clarification or add additional context in comments.

7 Comments

Thank you. But can I add field with 'index"? I told that most important is to get index of the items in subarray filed to result.
Yes IndexOfArray can do this. Just a min
Updated my answer
I try it with the mongoplayground.net/p/QkMbpaXlXH0. But there's an small error. MongoError: invalid operator '$indexOfArray' Could you let me know the reason? Thank you. :)
Mongodb version?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.