0

I am importing a CSV file into my Mongo collection and it is getting imported successfully. This is my Windows Command Prompt execution:

mongoimport --host localhost --db testing --collection test1 --type csv --headerline --file Desktop\try.csv
2016-10-12T23:25:10.464+0530    connected to: localhost
2016-10-12T23:25:10.561+0530    imported 1 document

My collection shows the following:

> db.test1.find().pretty()
{
        "_id" : ObjectId("57fe78fece1854fc74ad37f0"),
        "class" : "7th",
        "school_id" : 1
}
{
        "_id" : ObjectId("57fe7975912ee1426d6d94c6"),
        "school_id" : "1",
        "class" : "5th"
}
{
        "_id" : ObjectId("57fe7980912ee1426d6d94c7"),
        "school_id" : "1",
        "class" : "6th"
}

Here I have only imported the document with class 7th. The documents with class 5th and 6th have been inserted through Mongo Shell and not imported.

Now in PHP when I try to retrieve these documents, and display the "class" of each document, I am only able to display classes 5th and 6th which I inserted through Mongo Shell. The imported document data is not displayed i.e. class 7th.

I tried something with "writeConcern":

mongoimport --host localhost --db testing --collection test1 --type csv --headerline --file Desktop\try.csv --writeConcern {w:"majority"}

Is this something related to read or write concerns of MongoDB? Or something concerning the PHP Mongo driver?

I have a lot of data and I cannot afford to insert it through shell.

(I am using XAMPP Windows)

TylerH
  • 20,799
  • 66
  • 75
  • 101
rogue12x
  • 3
  • 3
  • try FORWARD slash with Absoulte Path as well, C:/Users/username/Desktop/try.csv – Digital Alchemist Oct 12 '16 at 18:38
  • Tried although it didnt work. However I converted my CSV file to JSON and it worked. I could retrieve it on my web page. No idea why it would ignore data from a CSV though. – rogue12x Oct 13 '16 at 16:30
  • Hi , kindly review this http://stackoverflow.com/a/17265858 , and this http://stackoverflow.com/a/28463511 might help you if u want to try csv option. – Digital Alchemist Oct 14 '16 at 14:34
  • Thanks, I have looked through those earlier. Actually the problem is not in my importing data. I can "find" all the data I imported from my mongo shell but when I retrieve it in php, to display, php just ignores that data which I imported via CSV. (I am using XAMPP Windows) – rogue12x Oct 15 '16 at 09:31

1 Answers1

1

There is a difference between the data. The school_id is a number in 7th and a string in 5th and 6th. Is this causing a problem in your query?

Matthew Wilcoxson
  • 3,432
  • 1
  • 43
  • 48
  • If that is not the problem please post you php code. – Matthew Wilcoxson Oct 15 '16 at 09:46
  • That was the problem, yes, thank u. However cannot find a way to import the number as a string. Double quotes won't work. I am currently running an update command to change the type. Is there any direct approach? – rogue12x Oct 15 '16 at 18:27
  • There's no way to import it as a string (see this answer http://stackoverflow.com/a/10303303/266375 ). You'll have to write some additional code, I'd suggest editting the database after import. – Matthew Wilcoxson Oct 16 '16 at 11:44