Database
[{"_id":123,"login":"john","details":{"a":"abc","verified":true},"locations":[{"location":{"country":"UK","region":"foo","city":"bar"},"createdAt":ISODate("2020-01-22T21:21:41.052Z")}],"profiles":[{"preference":{"a":"foo","b":"bar"},"details":{"a":"foo","b":"bar"},"hash":"hash123","createdAt":ISODate("2020-01-22T21:21:41.052Z")}],"posts":[{"postId":234,"message":"foo bar","createdAt":ISODate("2020-01-22T21:21:41.052Z")},{"postId":345,"message":"bar foo","createdAt":ISODate("2020-01-22T21:21:41.052Z")}]}]
Query
db.collection.update({"_id":123},[{"$set":{"locations":{"$let":{"vars":{/** your location input here*/"locationInput":{"country":"TEST","region":"test","city":"test"}},"in":{"$cond":{"if":{$ne:["$$locationInput",/** last location*/{$last:"$locations.location"}]},"then":{"$concatArrays":["$locations",["$$locationInput"]]},"else":"$locations"}}}}}},{"$set":{"profiles":{"$let":{"vars":{"profileInput":{/** your profile hash input here*/"preference":{"a":"test","b":"test"},"details":{"a":"test","b":"test"},"hash":"hash456","createdAt":"$$NOW"}},"in":{"$cond":{"if":{$ne:["$$profileInput.hash",/** last profile*/{$last:"$profiles.hash"}]},"then":{"$concatArrays":["$profiles",[/** your profile to be inserted here*/"$$profileInput"]]},"else":"$profiles"}}}}}},{"$set":{"posts":{"$let":{"vars":{"postInput":/** your post to be inserted here*/{"postId":456,"message":"test","createdAt":"$$NOW"}},"in":{"$cond":{"if":{$in:["$$postInput.postId","$posts.postId"]},"then":"$posts","else":{"$concatArrays":["$posts",["$$postInput"]]}}}}}}}])