I am trying to make a pretty simple mongoDB document to track searches by date in a node.js project. Here's what I'm shooting for:
{
"date": 1626930000000,
"searches": [
{
"search": "search 1",
"count": 2
},
{
"search": "search 2",
"count": 5
}
]
}
I want to update/add new searches to each date and increment the count for existing searches. Then on new dates do the same thing. Here is my current attempt using mongoose:
const query = { date: date, searches: { search: search } };
guideSearchesModel.findOneAndUpdate(query, {$addToSet: { searches: search ,$inc: { count: 1 }}}, { upsert: true, new: true }, callback);
But this is what gets added to the DB:
{
"_id": {
"$oid": "60f9eb370f12712140dd29db"
},
"date": 1626930000000,
"searches": {
"_id": {
"$oid": "60f9eb38c4ff591f50137726"
},
"search": "search 1"
}
Its missing the "count", and if I run it again it inserts a new document, doesn't update the one I want to based on the date.
Any ideas would be greatly appreciated.
Thanks!