How can I append a new document to sub-document array on MongoDB with Java?
For exemple, append {tweetId : 456} to the array of documents "tweets":
{
day : 20170209,
hours : {
hour : 20,
tweets : {tweetId : 123 }
}
}
Desired result:
{
day : 20170209,
hours : [ {hour : 20,
tweets : [{tweetId : 123},{tweetId : 456}]},
{hour : 21,
tweets : [{tweetId : 567},{tweetId : 890}]}
]
}
UPDATE
Sorry, I didn't explain correctly, the tweets document is an Array of tweetsId. I fixed the above example to better understanding.
For example, with a collection with only 2 levels:
{
"_id":{
"$oid":"589c5a1047e9062f004b84a3"
},
"hashtag":"BRASIL",
"tweets":[
{
"tweetId":"829661297121845249"
},
{
"tweetId":"829661278352269313"
}
]
}
I can append a new TweetId to this array with:
collection.updateOne(new Document("hashtag", hashtag),
new Document("$push", new Document("tweets", new Document("tweetId", tweetId))));
But when the array is at level 3 (Hours > Tweets > TweetId), I can't find a way to do it correctly.
Hope now it's better explained.