Home > Net >  What am I missing? MongoDB keeps giving me the same error no matter what I try
What am I missing? MongoDB keeps giving me the same error no matter what I try

Time:09-17

I am trying to insert a document into mongodb under the inspections collection but no matter what I try, I get an error!

The code I am trying:

db.inspections.insertOne({
"_id": 20032-2020-ACME,
 certificate_number: 9998888,
 business_name: "ACME Explosives",
 date: newDate("09-05-2022"),
 result: "Business Padlocked",
 sector: "Explosive Retail Dealer-999",
 {
 "address":"number->1721" "street-> Boom Road" "city->BRONX" "zip->10463"}
 }

This is the error I keep getting:

E  QUERY    [js] uncaught exception: SyntaxError: expected property name, got '{' :

I have tried to replace the brackets, tried formatting in a different way, tried using without quotes or switching quotes around but each attempt gives me this same error.

How can I fix this?

CodePudding user response:

The error actually tells you what is wrong. The item you are trying to insert is not valid json.

Most likely you have forgotten to put a key before the { that sits before "address". Also, in that same line you don't have any comma's to seperate the key value pairs.

I believe you will have more success with

db.inspections.insertOne({
"_id": "20032-2020-ACME",
 certificate_number: 9998888,
 business_name: "ACME Explosives",
 date: newDate("09-05-2022"),
 result: "Business Padlocked",
 sector: "Explosive Retail Dealer-999",
 address: {
    "number":"1721",
    "street":"Boom Road",
    "city":"BRONX", 
    "zip":"10463"
 }
}

The address: part is what it is truely complaining about now. Most likely it would have complained about the lack of comma's after fixing address:. Or your use of -> instead of :.

Also, it may also be that keys must be quoted.

EDIT: Also just noticed that your original ID is probably being interpreted as integer minus integer minus string/unknown var. I can't say what the effect might be of that, but I'm gonna assume you want that to be the id, as a string. To do so, I've put quotes around the 20032-2020-ACME

  • Related