2

I'm very new to MongoDB so forgive me if this question isn't worded correctly. I know how to insert into the database, and I also know that I can have a nested object and know how to install that. I have:

Questions.insert({ Order:1, Question: "What type of property is it?", 
    Answers: { Order: 1, Answer: "House" }});

I hope from the above statement you can see that I'm aiming to try and insert multiple answers for this question (this may be where I'm going wrong, is this the right approach?). So looking at the above statement, I thought that I could insert multiple answers as such:

Questions.insert({ Order:1, Question: "What type of property is it?", 
    Answers: [{ Order: 1,    Answer: "House" }, 
             { Order: 2, Answer: "Flat" }, 
             { Order: 3, Answer: "Bungalow" }, 
             { Order: 4, Answer: "Maisonette }]
});

SyntaxError: Unexpected token ILLEGAL

4
  • What errors are you seeing? Hard to diagnose without specifics of the problem. Commented Aug 2, 2013 at 14:14
  • @AlfieHanssen It's an is this the right approach question not a why isn't this working question Commented Aug 2, 2013 at 14:17
  • Incidentally, the SyntaxError you're getting is an error from JavaScript, not from MongoDB -- it means the command you put together doesn't parse into a valid JavaScript command. MongoDB's error messages are formatted differently. Per @potatosalad, there's nothing wrong with your approach necessarily, you just missed a quote mark! Commented Aug 2, 2013 at 14:26
  • @JimDagg How silly of me. Thanks a bunch Commented Aug 2, 2013 at 14:27

2 Answers 2

5

You are missing a " at the end of Maisonette which is where the error is coming from.

{ Order: 4, Answer: "Maisonette }]

Otherwise your query is on the right track for inserting embedded documents.

Sign up to request clarification or add additional context in comments.

1 Comment

Your answer more appropriately addresses the problem the OP is having than mine -- +1.
1

Your answers sub-document is kind of acting like an array. There are two possibilities you could use to store multiple answers in each question:

1) Just use an array:

Questions.insert({order : 1, 
    question : "What type of property is it?", 
    answers : [ "House", "Flat", "Bungalow", "Maisonette" ]
    });

2) The way MongoDB will sometimes internally store arrays is to simply use an ordinal as the key to each sub-document, like so:

Questions.insert({order : 1, 
    question : "What type of property is it?", 
    answers : {"1" : "House",
               "2" : "Flat",
               "3" : "Bungalow",
               "4" : "Maisonette"}
    });

4 Comments

First one gave an error about unexpected token. The second one worked however so thank you!
That's because I forgot a quote around "Maisonette". Edited.
@JimDagg, the script of @danrhul is also correct except this missing quote "
True! I posted this answer before @darnhul updated with the error message he was getting.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.