MongoDB import command does not import documents version 42.14

Hi Team,
As below command either syntax correct or not. if it is correct why 0 documents imported after executed the command or if it is incorrect syntax please give correct syntax
The DB has 4 documents below mentioned documents count

H:\Desktop\Downloads\bin>mongoimport.exe --port 27017 --db sample -c test1 --type JSON --file H:\Desktop\Downloads\MongoBD\dumps\test.json
2021-08-04T23:27:25.815+0530 connected to: mongodb://localhost:27017/
2021-08-04T23:27:25.816+0530 0 document(s) imported successfully. 0 document(s) failed to import.

rs1:PRIMARY> use sample
switched to db sample
rs1:PRIMARY> db.test1.find().count()
4
rs1:PRIMARY>

How was the test.json created?
Any log/size of file
Row count you are showing from primary is the DB from where you exported?
Are both DBs on the same box?

Share the content of the file test.json.

And if possible, share the command you used to create it.

Row count you are showing from primary is the DB from where you exported? I did not exported from an where.
Please understand i am going to import DB from my primary server to local

Can you please share Do you know how to take backup by using Mongoimport command ?

what kind of command are you asking

If you know how to mongoimport take single DB single collection
If yes, please share me command line

mongoimport works on the dump created by mongoexport
So take a dump using mongoexport from your source DB and then import into your target DB
How did you create test.json file?That’s why i asked for size/logs
Where is your primary located?

primary is located local server

how to use mongoimport command how can i take backup like: Json , csv, tsv as official mongoDB site mentioned

Please check mongo documentation

After i checked the mongoimport document then i am getting that error

the below command as mentioned in those docs.mongodb.com
mongoimport --db=users --collection=contacts --file=contacts.json

Issue is not with import command but with --file value you are passing
Are you passing a valid file?
We asked thrice how test.json was created but you did not show the size or contents or how it was created
Repeating again
1.Create a dump file using mongoexport from your source DB
2.Import the data by using mongoimport by passing the file created in 1st step

1 Like

I know that file is valid ,
If you know that how to check valid file and let me know

test.json will be created when all documents once imported target file

Please watch the video ,

https://university.mongodb.com/mercury/M001/2021_August_10/chapter/Chapter_1_What_is_MongoDB_/lesson/5f32c8a304e9ffbb03b169ac/lecture

you have asked file size if there is one or more documents consisting in single collection when you start mongoimport all documents will be importing in target file

rs1:PRIMARY> db.test1.find().count()
4
rs1:PRIMARY> db.test1.find().size()
4
rs1:PRIMARY> db.test1.stats()
{
“ns” : “sample.test1”,
“size” : 207,
“count” : 4,
“avgObjSize” : 51,
“storageSize” : 36864,
“capped” : false,
“wiredTiger” : {
“metadata” : {
“formatVersion” : 1

In MongoDB old version 3.0 working mongoimport but new version getting error.

Still not clear with your requirement
If your intention is to import data from your primary server to another DB on your local host first create a dump file using mongoexport
Then import into your target DB

Also not clear what you meant by test.json will be created after import
mongoimport will not create any file.It expects a file created by mongoexport which is what we should pass with --file

How are you sure your test.json is a valid file
I was asking the physical size of this file H:\Desktop\Downloads\MongoBD\dumps\test.json not the size from db.stats
Purpose of asking these is to make sure your file is not empty

steevej also asked you to share how test.json was created and command you used to create it

Hope it clarifies your doubts

1 Like

Yes will be creating after import test.json in command mentioned --type json incase i was mentioned instead of json either csv or tsc then --file H:\Desktop\Downloads\MongoBD\dumps\test.csv or .tsv

Here, i am importing collection from primary server to local host not importing another DB

empty file test.json

If you know how to toke single collection backup as .CSV format and .tsv format.
If yes, let me know full syntax command.

I think you are getting confused with mongoexport and mongoimport
All your posts ask the same question on how to create backup with mongoimport

Have you gone through the links i shared?
When you say import file to local host but not importing into any DB I interpret as creating a dump file
So where is your primary? On same local host or remotely hosted?

Assuming it is running locally
use this

mongoexport --db=test --collection=testcoll --type=csv --fields=x,y,z --out=C:\Users\user_name\testcoll.csv
Where x,y,z are the field_names in your collection

If it is remote host use --host or --uri.Refer to mongo documentation

1 Like

My source file has security file due to security cannot download full completed file from official site.

Thanks for patient replied my queries quick.