skip to Main Content

I have the MongoDb Document describe in the following playground
https://mongoplayground.net/p/7unPX_abybK

I desperately try to add an element in the links of a targetedEvent when the name of the targetedEvent is "RdvPris" and that it has no link whith targetType:"missions"

So for the playground example , it should add an element under
"targetType": "t4"
and "targetType": "t5"

please help it is a real puzzle for me

Has anyone a solution ?
Thanx by advance

2

Answers


  1. In the MongoDB shell, add a link to the "RdvPris" event’s "links" array if no link with targetType: "missions" exists.

    db.collection.update(
      {
        "targetedEvents.name": "RdvPris",
        "targetedEvents.links": { $not: { $elemMatch: { targetType: "missions" } } }
      },
      {
        $push: {
          "targetedEvents.$.links": {
            "targetId": ObjectId("yourTargetId"),
            "targetType": "missions"
          }
        }
      }
    )
    
    Login or Signup to reply.
  2. To add an element in the "links" array of a "targetedEvent" when the name of the "targetedEvent" is "RdvPris" and it has no link with "targetType" set to "missions", you can use this mongo query, it will update all matching document in collection.

    db.collection.update({
      "targetedEvents.name": "RdvPris",
      "targetedEvents.links.targetType": {
        $ne: "missions"
      }
    },
    {
      $push: {
        "targetedEvents.$.links": {
          "targetType": "t30"
        }
      }
    },
    {
      multi: true
    })
    

    Here is the playground link with example.

    Feel free to add _id field accordingly.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search