I am learning mongoDB , version 3 and I wanted to create a collection with two fileds, a date field and a descrition field. When I try to insert a document with the below syntax, from the mongo shell, I get an error and do not know how to correct it.

> db.timeTable.insert( dateField: new Date("Oct, 04, 2012"), description:  'Next total solar eclipse visible from North America')
2016-02-08T19:34:15.474-0600 E QUERY    [thread1] SyntaxError: missing ) after argument list @(shell):1:30

How should I write this insert?

Recommended Answers

All 3 Replies

I'm not an expert in Mongo at all but have you tried it with double quotes around the text string instead of singles?
Single quotes can be an escape character in SQL and may be in mongo too, in which case the ) isn't being read as such.

Yes I have tried that and it does not help. The error message points to line 1 character 30, but I don't see anything suspicious there.

These are the various answers that I found to resolve this:

INSERT INTO A COLLECTION VIA MONGO SHELL
dateField is a string

db.timeTable.insert({dateField:  "2007,08,07"},{description : "American baseball player Barry Bonds hit his 756th career home run, breaking the record set by Hank Aaron."});

Date field is an ISO object

db.timeTable.insert({dateField: Date("2013-08-07")},{description : "American baseball player Barry Bonds hit his 756th career home run, breaking the record set by Hank Aaron."});

This will work with mongo import if this series of 3 documents were in a file

{dateField: ISODate("2003-05-13T00:00:00Z"), description: "The First  government unveiled a new version of the $20 bill the first to be colorized in an effort to thwart counterfeiters."}
{dateField: ISODate("2003-05-13T00:00:00Z"), description: "The Second government unveiled a new version of the $20 bill the first to be colorized in an effort to thwart counterfeiters."}
{dateField: ISODate("2003-05-13T00:00:00Z"), description: "The government unveiled a new version of the $20 bill the first to be colorized in an effort to thwart counterfeiters."}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.