-1

Explanation: How I can remove the 0 elements from the array the output should be > 0 values.

{
"data1": [
    0,
    1,
    1,
    0,
    0,
    1,
    1,
    0
  ]
}

The output will be :

{
"data1": [
    1,
    1,
    1,
    1
  ]
}
bc110402922
  • 457
  • 2
  • 14
  • you can use [$filter](https://docs.mongodb.com/manual/reference/operator/aggregation/filter/) operator to filter array. see similar question [How to filter array in subdocument with MongoDB](https://stackoverflow.com/questions/15117030/how-to-filter-array-in-subdocument-with-mongodb) – turivishal Sep 10 '21 at 13:07
  • thanks for referring. I try to understand but do not succeed.! – bc110402922 Sep 10 '21 at 13:09
  • can you please write the query for array needed > 0 values – bc110402922 Sep 10 '21 at 13:13
  • 1
    check this [answer](https://stackoverflow.com/a/42876427/8987128), just need to put 0 instead of 3 and use "list" instead of "list.a". – turivishal Sep 10 '21 at 13:18
  • thanks for your valuable comment its solved, do you have any idea how I can use the `and` operator in between arrays [&&ARRAY](https://stackoverflow.com/questions/69132514/remove-0-values-from-the-array-in-mongodb?noredirect=1#comment122186507_69132514) and EDGE CASE: if the element of the array is not matched, the last element considers it as 0. – bc110402922 Sep 10 '21 at 13:38
  • If it's solved you shouldn't ask new questions in a comment. – Gert Arnold Sep 11 '21 at 08:30

1 Answers1

0
{
  "$project": {
    "data1": {$filter: {
        input: "$data1",
        as : "p",
        cond: {$gt: ["$$p", 0]} //<-- filter sub-array based on condition
    }}
}}

MongoPlayground

bc110402922
  • 457
  • 2
  • 14