I have imported a csv file in mongo using mongoimport and I want to remove leading and trailing white spaces from my string value.
Is it possible directly in mongo to use a trim function for all collection or do I need to write a script for that?
My collection contains elements such as:
{
"_id" : ObjectId("53857680f7b2eb611e843a32"),
"category" : "Financial & Legal Services "
}
I want to apply trim function for all the collection so that "category"
should not contain any leading and trailing spaces.
You can execute javascript in an MongoDB update command when it's in a cursor method:
If you have a ton of records and need to batch process, you might want to look at the other answers here.
It is not currently possible for an update in MongoDB to refer to the existing value of a current field when applying the update. So you are going to have to loop:
Noting the use of the
$set
operator there and the projected "category" field only in order to reduce network traffic"You might limit what that processes with a
$regex
to match:Or even as pure
$regex
without the use of$and
which you only need in MongoDB where multiple conditions would be applied to the same field. Otherwise$and
is implicit to all arguments:Which restricts the matched documents to process to only those with leading or trailing white-space.
If you are worried about the number of documents to look, bulk updating should help if you have MongoDB 2.6 or greater available:
Or even with the bulk operations API for MongoDB 2.6 and above:
Best done with
bulkWrite()
for modern API's which uses the Bulk Operations API ( technically everything does now ) but actually in a way that is safely regressive with older versions of MongoDB. Though in all honesty that would mean prior to MongoDB 2.6 and you would be well out of coverage for official support options using such a version. The coding is somewhat cleaner for this:Which all only send operations to the server once per 1000 documents, or as many modifications as you can fit under the 64MB BSON limit.
As just a few ways to approach the problem. Or update your CSV file first before importing.
Small correction to the answer from Neil for bulk operations api
it is
not
also you missed to
inside the forEach, so in summary
Note: I don't have enough reputation to comment, hence adding an answer
Starting
Mongo 4.2
,db.collection.update()
can accept an aggregation pipeline, finally allowing the update of a field based on its own value.Starting
Mongo 4.0
, the$trim
operator can be applied on a string to remove its leading/trailing white spaces:Note that:
The first part
{}
is the match query, filtering which documents to update (in this case all documents).The second part
[{ $set: { category: { $trim: { input: "$category" } } } }]
is the update aggregation pipeline (note the squared brackets signifying the use of an aggregation pipeline):$set
is a new aggregation operator which in this case replaces the value for"category"
.$trim
we modify and trim the value for"category"
.$trim
can take an optional parameterchars
which allows specifying which characters to trim.Don't forget
{ multi: true }
, otherwise only the first matching document will be updated.