raghavginfocus
raghavginfocus

Reputation: 343

Inserting data in mongo collection through cmd command

When I am inserting data in MongoDb database using cmd command

mongo mongodb://localhost:27017/DummyDatabase --eval "db.Dummy.insert({a:12345,b:asd})"

I am getting following error -

MongoDB shell version v4.2.0

connecting to: mongodb://localhost:27017/Ontologies?compressors=disabled&gssapiServiceName=mongodb

Implicit session: session { "id" : UUID("6da84ed8-8bf0-4a6c-9d38-7c70cbfc8c7e") }

MongoDB server version: 4.2.0 2019-11-07T13:49:15.316+0530 E QUERY [js] uncaught exception: ReferenceError: asd is not defined : @(shell eval):1:38 2019-11-07T13:49:15.320+0530 E - [main] exiting with code -4

But when I give this command -

mongo mongodb://localhost:27017/DummyDatabase --eval "db.Dummy.insert({a:12345})"

This works, what can be the issue?

Upvotes: 0

Views: 819

Answers (1)

Mahesh Nayak
Mahesh Nayak

Reputation: 104

The issue with failing command is that the value provided to the field with key 'b' is not a legal JSON value type. asd is a string that needs to be enclosed in quotes so that the document being inserted can be a valid JSON object. Refer to json.org for details.

Upvotes: 0

Related Questions