Would it be possible to use $ifNull to conditionally pass a field through an aggregation pipeline based on whether or not data is present in the field? I’m using an $project after an $sort, but I want to pass $trim:{input: $causeofdeath} through if data is in the field.
Can I do that, or do I have to pass the field through all the time?
The $$REMOVE will remove the property if the field’s value is null.
Out of the question, I would suggest you insert a trimmed value in the database, so you can avoid these extra operations when you retrieve the data from the database.
Thanks! Unfortunately, I’m working with a “dirty” data set that has been mostly maintained by inexperienced people up to this point.
Would csvkit (csvkit 1.1.1 documentation) be able to trim the values on the CSV before I import them with mongoimport, or would I have to write my own script?
I don’t know more about csvkit, but if you google it you will find plenty of resources/tools to trim the CSV values.
If you want to update your existing document’s value then you can use update with aggregation pipeline with updateMany() method and the solution that I have provided will work in update query.