user2761286 user2761286 - 1 month ago 14
Bash Question

mongoimport imports csv file disordered

I have a 23k rows csv file. When i use mongoimport from shell or import from mongochef somehow it imports with wrong order.

for example i have

a;b;c;(header)
1;1;1;
2;2;2;
3;3;3;


csv file. when i import it from shell or mongochef and then .find() it result is ;

a|b|c
1;1;1;
3;3;3;
2;2;2;


any help would be great. Here is my shell command for import ;

mongoimport -d local -c test --type csv --file "C:\Program Files\MongoDB\Example Datasets\abc.csv" --headerline --ignoreBlanks

Answer

You could try using --maintainInsertionOrder option.

As the docs say:

If specified, mongoimport inserts the documents in the order of their appearance in the input source, otherwise mongoimport may perform the insertions in an arbitrary order

Also notice that the find default ordering is the natural order that doesn't guarantee returning results by the insertion order. So what I usually do is sorting by the _id field.