How do i make it so when i submit to my database it adds it to a object that has the same name? I want it so that when i submit an array on the same date they go under the same object. When I try it automatically adds an object between the parent element and the element I'm adding.
MongoDB adds this object between the elements I want.
I tried looking everywhere but could find any help.
My code:
app.post('/add-submit', (req,res) => {
var query = { username: req.body.username };
db.collection("users").find(query).toArray((err, result) => {
if (err) throw err;
console.log(result);
});
let postDate = new Date();
let dd = String(postDate.getDate()).padStart(2, '0');
let mm = String(postDate.getMonth() + 1).padStart(2, '0');
let yyyy = postDate.getFullYear();
postDate = mm + '/' + dd + '/' + yyyy;
subject = req.body.subject
pushValue = {"submits": {[postDate]: { [subject] : [ time = req.body.time, description = req.body.description, date = postDate ]}}}
console.log(pushValue)
let myquery = { username: req.body.username};
let newvalues = { $push: pushValue }
db.collection("users").updateOne(myquery, newvalues, (err, response) => {
if (err) throw err;
console.log("1 document updated");
res.redirect('/users/'+req.body.id)
});
})
What I'm trying to do:
Thanks.
