MongoDB shell find query - mongodb

I am running a simple find query from unix shell.
mongo <<DB NAME>> -u <<USERNAME>> -p <<PASSWORD>> <<HOST>>:<<PORT>> --authenticationDatabase <<AUTHENTICATION DB NAME>> --eval db[<<COLLECTION NAME>>].find({"name":"Harsha"})
I am getting below error.
[js] SyntaxError: missing ; before statement #(shell):1:6
I also tried by changing DB name place (ex: host:port/db_name)
mongo -u <<USERNAME>> -p <<PASSWORD>> <<HOST>>:<<PORT>>/<<DB NAME>> --authenticationDatabase <<AUTHENTICATION DB NAME>> --eval db[<<COLLECTION NAME>>].find({"name":"Harsha"})
Still getting same error.
Am I missing something? Can anyone please help?

MongoDB expects the argument of --eval to be passed as string like in this example:
--eval "db[<<COLLECTION NAME>>].find({'name':'Harsha'})"

Related

remove document from mongodb collection with curl

I want to remove document from mongodb collection in script linux(with curl).
like elastic commands ::
curl -X GET "localhost:9200/my-index-000001/_search?pretty"
AS #Wernfried mentioned mongoDB do not provide HTTP service so you cannot use curl , however you can do something like this from your linux shell script:
echo 'db.getCollection("myCollection").remove({_id:"documentIdforRemoval"})'| mongo --quiet --host myDBhost --port myDBport myDB --AuthenticationDatabase=admin -u myuser -p mypass

Mongodb authentication error when selecting database from command line

I am trying to delete a mongo database inside a shell script. The server requries authentication with a password. When I type the following into the command line:
mongo -u mun -p 'themongopassword'
I am able to connect to the database and then when run the following commands:
use dbname
db.dropDatabase()
The database is successfully deleted. However I would like to do the following:
mongo -u mun -p 'themongopassword' --eval 'db.dropDatabase()' sigma
I get the following output:
MongoDB shell version v4.2.6
connecting to: mongodb://127.0.0.1:27017/sigma?compressors=disabled&gssapiServiceName=mongodb
2020-05-06T13:13:53.419+0300 E QUERY [js] Error: Authentication failed. :
connect#src/mongo/shell/mongo.js:341:17
#(connect):2:6
2020-05-06T13:13:53.420+0300 F - [main] exception: connect failed
2020-05-06T13:13:53.420+0300 E - [main] exiting with code 1
Removing the eval option like so:
mongo -u mun -p 'themongopassword' sigma
also results in an authentication failure with the same output.
Did you tried this:
mongo <dbname> -u mun -p 'themongopassword' --eval "db.dropDatabase()"
If you want to get a readable result try this:
mongo <dbname> -u mun -p 'themongopassword' --eval "printjson(db.dropDatabase())"
Try adding option "--authenticationDatabase admin", i.e.:
mongo <dbname> -u mun -p 'themongopassword' --authenticationDatabase admin --eval "printjson(db.dropDatabase())"

getting error while exporting mongodb using mongo cmd in windows

I am trying to export mongodb database using this command:
mongoexport -d db_name
But I am getting this error:
SyntaxError: missing ; before statement #(shell):1:15
What will be the command to export db?
You can use mongodump it's faster:
mongodump -d <database_name> -o <directory_backup>
And to restore/import that, i used (from directory_backup/dump/):
mongorestore -d <database_name> <directory_backup>

mongodb database don't export from mLab cloud

I have try to export my old mongodb database from mLab cloud, but it's show an SyntaxError. Please see my query and SyntaxError below :
query: mongoexport -h ds02575.mlab.com:25752 -d heroku_g2dn9rd9 -c accounts -u adam -p dino -file accounts.json
And SyntaxError: 2017-12-28T10:36:37.340+0600 E QUERY [thread1] SyntaxError: missing ; before statement #(shell):1:15
Please tell me how can i export it, It's really important for me now
Thanks
Mongoexport should be run from the system command line, not from the mongo shell.

unexpected identifier error while bulk insert into mongodb

I have a json file and am trying to insert in bulk to mongodb
I do
mongoimport --host localhost --db testdb --collection testdbjson --username user -- password pass --type json --file /home/pet/mng/json_test.json
It gives the following error
SyntaxError: Unexpected identifier
Please help!
the document mongoimport the correct syntax
mongoimport -host 127.0.0.1 -port 27017 -u user -p pass -collection testdbjson --db testdb --file /home/pet/mng/json_test.json
or
mongoimport -host 127.0.0.1:27017 -u user -p pass -collection testdbjson --db testdb --file /home/pet/mng/json_test.json
I had the exact same error now - I figured it out. I think you have connected to your mongo instance on mongolab first before you wanted to import.
You must not connect to mongolab first - disconnect by typing in "exit" in the shell to get back to command prompt. Then issue the mongoimport command again, the import should work this time.