I would like to add to my Database using MongooDB values with the following code but when my code is running the data I want to add to my database does not add
const profileModel = require('../DataBase/Schema.js')
function UserTotal(userId,TotalForTheSession){
const time = new Date();
var TotalTimeUserPath = './Settings/UserDataTotal.json'
var TotalTimeUserRead = fs.readFileSync(TotalTimeUserPath);
var TotalTimeUserFile = JSON.parse(TotalTimeUserRead); //ready for use
var theyear = time.getFullYear();
var themonth = time.getMonth() +1;
var thetoday = time.getDate();
let DateAllTogether = `${theyear}/${themonth}/${thetoday}`
async function totalTimeDataBase(userId,TotalForTheSession,DateAllTogether){
let userData
try{
userData = await profileModel.modelUser.findOne({userID: userId})
if (!userData){
let profile = await profileModel.modelUser.insertMany({
userID : userId,
totalTime: TotalForTheSession,
timeDate: {
[DateAllTogether]: TotalForTheSession
}
})
}
else if (!userData.timeDate[DateAllTogether]){
await profileModel.modelUser.updateOne({
userID:userId, "timeDate": {
$add: [[DateAllTogether], TotalForTheSession]
},
},
)
}
}catch(err){
console.log(err)
}
}
totalTimeDataBase(userId,TotalForTheSession,DateAllTogether)
The code for how my Schema in my dataBase is made
const mongoose = require('mongoose')
const profileSchema = new mongoose.Schema({
userID: {type: String, require: true},
timeTotal: {type: String, default: 0 },
timeDate:{}
})
const modelUser = mongoose.model('ProfileModels', profileSchema)
module.exports = {modelUser}
When the code is executed i’m getting this errorMongoServerError: The dollar ($) prefixed field '$push' in 'timeDate.$push' is not valid for storage.
*
The file where my data is stored in my database
{
"_id" : ObjectId("61bd159db3daaef59378e5f1"),
"userID" : "526166979321790466",
"timeTotal" : "0",
"timeDate" : {
"2021/12/16" : 1
},
"__v" : 0
}
I want to add “2021/12/17” : 1 in “timeDate” value like so
{
"_id" : ObjectId("61bd159db3daaef59378e5f1"),
"userID" : "526166979321790466",
"timeTotal" : "0",
"timeDate" : {
"2021/12/16" : 1,
"2021/12/17" : 1
},
"__v" : 0
}