0

Working with a frontend variable like below in javascript with many objects... (Note - the scores would be different for each user therefore I would need to be able to get the info from the frontend)

var campgrounds = [{ name: "State Park #1" score: 0, }, { name: "State Park #2" score: 0, }, { name: "State Park #3" score: 0 }]

How would I be able to store that data in MongoDB? Could I somehow loop through the data? Or could I store the entire variable "campgrounds" in Mongo? like $("#quizData").value(campgrounds)

Would I set up a Schema like below? Even if I did I'm not sure how to actually get the data in the variable into Mongo. Not sure how to accomplish my goal. Thanks for any help! :)

var campgroundsSchema = new Schema ({
    "campgrounds" : [{
      name : {type: String},
      score : {type : Number}
    }],
});

1 Answer 1

2

If you connect to mongo using mongoshell ...

mongo --host localhost:27017

you can issue the following javascript commands...

use campgroundsdb
var campgrounds = [{ name: "State Park #1", score: 0 }, { name: "State Park #2", score: 0, }, { name: "State Park #3", score: 0 }]
db.campgroundscollection.insertMany(campgrounds)

Then find them by issuing...

db.campgroundscollection.find().pretty()

If you want to create a program written in JavaScript it will need the ability to connect to the mongodb process. Node.js tutorials fit this requirement...

https://www.w3schools.com/nodejs/nodejs_mongodb.asp

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

3 Comments

Thanks for the help! Would this work with the campgrounds variable having data that changes based on user input on the front end? Thats my main blocker :)
As long as the data provided by the users can be stored in a JavaScript variable it can be saved into Mongo. There is a 16MB limit on how much data can be saved in one document. Thats a large amount of data so it usually isn't much of a limit.
Incredible! Thank you for the help!

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.