Query increment number in array or if the array doesnt exist create it

So I’m trying to increment a number in an array that is situated in an object which is situated in an array Let’s say today’s date is 05/03/2023, which is found in the object so it will just increment the second number in the array

[{
    key: "test",
    numbers: [
        0,
        0,
        0,
        {
            "05/03/2023": [ 0,5,0 ]
        }
    ]
}]

So in the case above the result from the query will be:

[{
    key: "test",
    numbers: [
        0,
        0,
        0,
        {
            "05/03/2023": [ 0,6,0 ]
        }
    ]
}]

But if our collection is:


[{
    key: "test",
    numbers: [
        0,
        0,
        0,
        {}
    ]
}]

In this case it will need to create the array and this is how the result should look

[{
    key: "test",
    numbers: [
        0,
        0,
        0,
        {
            "05/03/2023": [ 0,1,0 ]
        }
    ]
}]

This is what I tried but doesn’t work $addFields sets everything in the numbers array and I’m confused

db.collection.update({},
[
  {
    $addFields: {
      "numbers.3.04/03/2023": {
        $cond: {
          if: { $isArray: "numbers.3.04/03/2023" },
          then: {
            $concatArrays: [ { $slice: [ "numbers.3.04/03/2023", 0,  1 ]  },
              [ { $add: [ { $arrayElemAt: [ "numbers.3.04/03/2023", 1 ] },  1  ] } ],
              {$slice: [ "numbers.3.04/03/2023", 2, 1 ] }  
            ]  
          },
          else: [ 0, 1, 0 ]
        }
      },
      
    }
  },
  
])

I made a mongoplayground link if anyone wants to help: Mongo playground

I want to basically achieve this:

db.collection.update({
  key: 1, "numbers.3.04/03/2023": { $exists: false } 
},
{ $set: { "numbers.3.04/03/2023": [ 0, 0, 0 ]  }
})
db.collection.update({
  key: 1,
},
{
  $inc: { "numbers.3.04/03/2023.1": 1}
})

But all in one query

Hello :wave: @xThe_Alex14,

Welcome to the MongoDB Community forums :sparkles:

Apologies for the late response!

It appears that your current query is functional, but I would appreciate some clarification on certain aspects. Specifically, your query appears to target a specific array index and key name.

However, I am curious about how your query will behave when additional elements are introduced, such as "numbers.3.05/03/2023". Will the query simply append this new element, or will it operate differently in this scenario?

Additionally, I would suggest that you reconsider your schema design. While your current approach may work for this specific example, once data grows, this query will be more difficult to maintain. Therefore, it is worth taking the time to evaluate your schema and explore alternative designs that may better meet your needs and be easier to work with. Please refer to the MongoDB Schema Design Best Practices article on MongoDB DevCenter to learn more.

Best,
Kushagra

2 Likes

Hello,

So numbers.3.04/03/2023 is an array that has only 3 numbers that represents likes, views, downloads. The array won’t grow or anything I just want a query that will increase the view count by 1 so that is the second element in the array. The problem is that if the array doesn’t exist it obviously won’t increment anything so if that array doesn’t exist i need to create it, that’s the first update query and the second one is to increment the actual value.

Hope this makes everything clear, it you need anything else please let me know