I have a collection with an entry that looks like the below.
{
"_id" : ObjectId("57f6021caa3e1d3ee099af0d"),
"user" : "JBL123",
"name" : "Joe Bloggs",
"monthly_activity" : [
{
"month" : "September2016",
"status" : "active",
"daily_stats" : [
{
"date" : "27/SEP/2016",
"stats" : [
{
"category":
"stat1":
"stat2":
},
{
"category":
"stat1":
"stat2":
}
]
},
{
"date" : "28/SEP/2016",
"stats" : [
{
"category": ...
"stat1": ...
"stat2": ...
},
{
"category": ...
"stat1": ...
"stat2": ...
}
]
},
],
"extra_fields": ....
}
]
}
I want to insert stats for a new category on a specified date.
I've tried the below
mdb.getCollection("users").updateOne(new Document("user", "JBL123").append("monthly_activity.daily_stats.date", "27/SEP/2016"),
new Document("$addToSet", (new Document("monthly_activity.daily_stats.$.stats",
new Document("category",cat)
.append("stat1", s1)
.append("stat2", s2)))));
But it doesn't like using dot notation to access the subarray of an array. I get the following error...
Exception in thread "main" com.mongodb.MongoWriteException: can't append to array using string field name [daily_stats]
Is this possible from a single update?