Home > Net >  Updating with unique object values in MongoDB
Updating with unique object values in MongoDB

Time:07-29

Collection in MongoDB looks like the following:

{
_id:"Some random ID",
name:"ABC",
profiles:[
    {
      profileId: '123',
      attachedOn: 2022-07-28T15:08:11.072Z
    },
    {
      profileId: '456',
      attachedOn: 2022-07-28T15:08:11.072Z
    },
    {
      profileId: '789',
      attachedOn: 2022-07-28T15:08:11.072Z
    },
    {
      profileId: '101',
      attachedOn: 2022-07-28T15:08:11.072Z
    }
  ]
}

Now I am receiving data from the front end like this

data.profiles = [
    {
      profileId: '123',
      attachedOn: 2022-07-28T15:08:11.072Z
    },
    {
      profileId: '456',
      attachedOn: 2022-07-28T15:08:11.072Z
    },
    {
      profileId: '789',
      attachedOn: 2022-07-28T15:08:11.072Z
    },
    {
      profileId: '202',
      attachedOn: 2022-07-28T15:08:11.072Z
    }
  ]

So I want to insert the object in the profiles field if the profileId is unique so in the above case only the object with profileId 202 gets Inserted in the profiles field for achieving the desired output so far I'd tried this structure

query = [
            {
              name:"ABC",
            },
            { $addToSet: { profiles: { $each: data.profiles } } },
            {
              $set: {
                ...record,
              },
            },
        ];

CodePudding user response:

Here's one way to do it.

db.collection.update({
  "name": "ABC"
},
[
  {
    "$set": {
      "profiles": {
        "$concatArrays": [
          "$profiles",
          {
            "$filter": {
              // your received data goes here
              "input": [
                {
                  profileId: "123",
                  attachedOn: ISODate("2022-07-28T15:08:11.072Z")
                },
                {
                  profileId: "456",
                  attachedOn: ISODate("2022-07-28T15:08:11.072Z")
                },
                {
                  profileId: "789",
                  attachedOn: ISODate("2022-07-28T15:08:11.072Z")
                },
                {
                  profileId: "202",
                  attachedOn: ISODate("2022-07-28T15:08:11.072Z")
                }
              ],
              "as": "profile",
              "cond": {
                "$not": {"$in": ["$$profile.profileId", "$profiles.profileId"]}
              }
            }
          }
        ]
      }
    }
  }
])

Updated document:

[
  {
    "_id": "Some random ID",
    "name": "ABC",
    "profiles": [
      {
        "attachedOn": ISODate("2022-07-28T15:08:11.072Z"),
        "profileId": "123"
      },
      {
        "attachedOn": ISODate("2022-07-28T15:08:11.072Z"),
        "profileId": "456"
      },
      {
        "attachedOn": ISODate("2022-07-28T15:08:11.072Z"),
        "profileId": "789"
      },
      {
        "attachedOn": ISODate("2022-07-28T15:08:11.072Z"),
        "profileId": "101"
      },
      {
        "attachedOn": ISODate("2022-07-28T15:08:11.072Z"),
        "profileId": "202"
      }
    ]
  }
]

Try it on mongoplayground.net.

  • Related