5

I am new to MongoDB and am trying to import a csv file containing programme data to MongoDB. One of the fields in the csv file (tags) contains a list of values as such:

enter image description here

When I import this into mongoDB, the entire field appears as a string:

"tags" : "[ethics.philosophy.plato]"

Is there any way that I can edit this field (either in the import command or manipulate the data in the database) such that the tag field is an array of values like this:

"tags" : ["ethics", "philosophy", "plato"]

I have looked online and through the mongoDB mongoimport documentation but have not found the relevant solution. Thanks in advance!

1 Answer 1

7

After CSV is imported, run this command.

db.whatevercollection.find().snapshot().forEach(function (el) {
   el.tags = el.tags.split('.');  
 db.whatevercollection.save(el); 
});

Edit: Your CSV Contains Square Brackets.

    db.whatevercollection.find().snapshot().forEach(function (el) {
    el.tags=el.tags.substring(1,el.tags.length-1);     
    el.tags = el.tags.split('.');  
         db.whatevercollection.save(el); 
});
Sign up to request clarification or add additional context in comments.

3 Comments

This solutions seems to have only worked partially, after running the command I get results like this one: "tags" : [ "[ancient_rome", "history", "italy]" ] . So the first and last item still contain the square bracket from the original string value. I could remove the square brackets from the csv file and then import and run your command, but is there a way to edit your solution so it removes the square brackets with the current collection I have imported?
Now the output is: TypeError: el.substring is not a function : @(shell):1:69 DBQuery.prototype.forEach@src/mongo/shell/query.js:501:1 @(shell):1:1
@sums22 my bad, sorry it should be el.tags.substring. Edited the answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.