3.7 Trouble with $elemMatch example

I am on Mac using terminal. Following along with the video. In mongo shell attached to my sandbox,

  1. I type:
    var martian = db.movieDetails.find({title: “The Martian”})
  2. I type: martian
    I get back the martian
    result: document as expected
  3. I type: martian a 2nd time
    result: nothing Note: I did this becauses step 4 didn’t work, so I thought the delete command was messing things up, but I just can’t repeatedly have the variable persist in my shell…
  4. I go back through steps 1 and 2. I then type: delete martian._id
    result: true
  5. I type martian
    result: nothing

I then tried to just see if I could get the box office array added. So I did

  1. var martian = db.movieDetails.find({title: “The Martian”})
  2. martian.boxOffice = [
    {“country”: “USA”, “revenue”: 228.4}.
    {“country”: “Australia”, “revenue”: 19.6},
    {“country”: “UK”, “revenue”: 33.9},
    {“country”: “Germany”, “revenue”: 16.2},
    {“country”: “France”, “revenue”: 19.8}
    ]
    result:
    [js] SyntaxError: missing name after . operator @(shell):1:71

MongoDB Enterprise Cluster0-shard-0:PRIMARY>

Now I don’t know if the syntax error is legitimate (I copied exactly what was done in the video) or it is because the variable martian is not really set in memory.

Any ideas?

1 Like

I found the problem. I incorrectly used find() instead of findOne(). This lead to instability. After switching to findOne() all worked as expected and found that there was indeed a syntax error in the boxOffice command. Hard to see but have a “.” instead of a “,” after the USA entry.

3 Likes