How delete a projection whith a $filter?

Hello . I have this kind of data ( dates an values )
I know how to select and return them but how to delete my selection in an aggregation /$project like this ??? no solution anywhere :disappointed_relieved: thanks a lot for your help

/**
 [
  { date: '2021-05-12T03:00:00+02:00', v: 19.81 },
  { date: '2021-05-12T04:00:00+02:00', v: 19.59 },
  { date: '2021-05-12T05:00:00+02:00', v: 19.31 },
  { date: '2021-05-12T06:00:00+02:00', v: 19.14 },
  { date: '2021-05-12T07:00:00+02:00', v: 18.02 },
  { date: '2021-05-12T08:00:00+02:00', v: 20.81 },
  { date: '2021-05-12T09:00:00+02:00', v: 24.91 },
 { date: '2021-05-12T10:00:00+02:00', v: 26.62 },
...
*/
collection.aggregate([
{ $match: {_id: ObjectID("xxxxxxxxxxxxxxxxxxx")}},
{ 
	$project: {
		 [`mydataArray`]: {
			$filter: {
				input: `$mydataArray`,   // array i  filterering
				as   : "item",         
				cond : { 
					$and: 
						[
							{ $gte :["$$item.date", moment(from).format()  ] },
							{ $lte  :["$$item.date",  moment(to).format() ] } 
						]
				}
			}
		}
	}
}
]) // then   how to delete ?????

Check the example from the below link will be helpful.

1 Like

Hello @Upsylon_Developpemen, Welcome to MongoDB Community Forum,

It does not required aggregation if you want to update documents, as per your aggregation i can understand you have to delete array of object by combination of dates, so you can use update method with $pull operator,

collection.updateOne(
  { _id: ObjectID("xxxxxxxxxxxxxxxxxxx") },
  {
    $pull: {
      mydataArray: {
        date: {
          $gte: moment(from).format(),
          $lte: moment(to).format()
        }
      }
    }
  }
)
1 Like

Many thanks for this solution! so simple like that!
I really regret that the documentation only skims over things, The CRUD is only a small part of the job generally. It is really painful, that time wasted. luckily this forum is there.
Thanks again.

1 Like

Helle thanks but remove is dont work with node driver :wink:

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.