Mongo findOneAndUpdate - How to not push objects into array if already exist?

51 Views Asked by At

A property in document is an array of object. If object already exists, I dont want to push same object. I want to check it based on the date and time. I have no idea how to do it and not sure how to apply what I have seen so far.

{
    "_id" : ObjectId("637b99b36cc39a1fee0a4977"),
    "name" : "product1",
      "reports" : [
        {
            "_id" : ObjectId("63f60fe2029fcd31b93870d3"),
            "name" : "product1",
            "title" : "hi",
            "dateTime" : ISODate("2023-02-22T12:51:31.993+0000"),
            "createdAt" : ISODate("2023-02-22T12:51:46.351+0000")
        },
       ],
    ....
}
...
       const reports = [{name: "product2", dateTime: ...}, {},...];
       for (let key in reports) {
            await Station.findOneAndUpdate({name: key}, {
                    $push: {
                        reports: {
                            $cond:{
                                if: {$inc: {"reports.$[elem].dateTime" - reports[key].dateTime == 0}}, // --> doesnt work
                                if: {$in: reports.map(function(x) { return x.dateTime ===reports[key].dateTime } )}, // __> doesnt work
                                then: "$reports",
                                else: {
                                 '$each': [...reports[key]],
                            '$sort': {
                                createdAt: -1
                            }
                                }
                            },
                        }
                    }, $set: {someotherProp: true}
                }, {new: true}
            );
     }
...

EDIT:

the reports I want to add depending on condition:

const reports = {
  'xyz-xyz': [
    {
      name: 'xyz-xyz',
      title: 'good morning',
      dateTime: '2022-11-09T07:18:05.000Z',
      ...
    },
     {
      name: 'xyz-xyz',
      title: 'hi',
      dateTime: '2022-11-09T07:18:06.000Z',
       ...
    },
   ],
   xxx: [
     {
      name: 'xxx',
      title: 'hello',
      dateTime: '2022-11-09T07:19:05.000Z',
       ...
    },
     ]
 }

I find first the product with findOneAndUpdate and then I check dateTime Prop. If I already have a report for that product with SAME datetime, dont update/save. If not, save. Additionally, I want to also check for a second condition such as same title.

EDIT:

     for (let key in reports) {
            await Station.findOneAndUpdate({
                    "name": key
                },
                [
                    {
                        "$set": {
                            "reports": {
                                "$cond": [
                                    {
                                        "$in": [
                                            new Date(reports[key].dateTime),
                                            "$reports.dateTime"
                                        ]
                                    },
                                    "$reports",
                                    {
                                        "$concatArrays": [
                                            "$reports",
                                          reports[key]
                                        ]
                                    }
                                ]
                            }
                        }
                    }
                ], {new: true})
        };
0

There are 0 best solutions below