mongoimport csv file that contains a column with array values
Asked Answered
E

1

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!

Elinoreeliot answered 29/4, 2017 at 18:24 Comment(0)
I
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); 
});
Indiscretion answered 30/4, 2017 at 15:53 Comment(3)
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?Elinoreeliot
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:1Elinoreeliot
@Elinoreeliot my bad, sorry it should be el.tags.substring. Edited the answerIndiscretion

© 2022 - 2024 — McMap. All rights reserved.