0

I try to create a Database in MongoDB. It should have seven collections and that one collection has another collection. I created this in terminal

db.Nothern province.insert({_id:1}{Distric:{Jaffna:{Zones:{Jaffna:},
{Valikamam:},{Thenmaradchy:},{Vadamarachchy:},{Islands:}}},{Kilinochchi:
{Zones:{Kilinochchi:}}},{Mullaitivu:{Zones:{Mullaitivu:}}},{Vavuniya:{Zones:
{Thunukkai:},{Vavuniya North:},{Vavuniya South:}}},{Mannar:{Zones:{Mannar:},
{Madhu:}}}})

but I received this error

[thread1] SyntaxError: missing ; before statement @(shell):1:11

Is this code correct or wrong?

How can I solve this?

Iman Bahrampour
  • 6,180
  • 2
  • 41
  • 64
TPath
  • 9
  • 6
  • 1
    The space in 'Nothern province' throws the error. 1:11 means: first line position 11. I don't know how to handle spaces in MongoDB collections. Sorry. – Phillip K Dec 15 '17 at 07:24
  • Possible duplicate of [MongoDB SyntaxError: missing ; before statement @(shell)](https://stackoverflow.com/questions/38772594/mongodb-syntaxerror-missing-before-statement-shell) – sgDysregulation Dec 15 '17 at 07:30

2 Answers2

0

Just use getCollection function.

Example:

db.getCollection("Nothern province").insert({
    "field1": "value1",
    "field2": "value2"
});
Neodan
  • 5,154
  • 2
  • 27
  • 38
0

In mongoDB When the first collection is created then the Database is automatically created because of it is Document Oriented and Schema Less

For Example: `

use testDatabase 

It will not create Database But your db object is pointing to the testDatabse if you run the below command then it will create a Database along with a collection named as : myColl

db.runCommand ( {
   create: "myColl",
   collation: { locale: "fr" }
});`

now run a command show dbs command on the mongo shell. you are able to see the testDatabase named database and after that run command show collections or show tables you are able to see the myColl named collection

sanjeev kumar
  • 61
  • 2
  • 3
  • But I have one doubt, I would like to save the datas in student marks. that student marks is should be this way. Province->Distric->Zone->School name->Grade10->Subject->Student marks – TPath Dec 18 '17 at 06:31
  • At any level of mongodb you are able to access the array as well as JSON object and you can push also the json object to any of the level for example Province->Distric->Zone->School name->Grade10->Subject->Student-marks @sutudent marks you have push json like this {"student_marks":{ marks: 243, date: new Date()}} – sanjeev kumar Dec 22 '17 at 04:27