Database
[{"userId":"641e6e1b65e76e035761952c","name":"jack","customProjectSettings":[{"_projectId":1,"settings":[{"_id":1,"show":[""]},{"_id":2,"show":[""]}]},{"_projectId":2,"settings":[{"_id":1,"show":[""]}]}]}]
Query
db.collection.aggregate({$match:{"userId":"641e6e1b65e76e035761952c"}},{"$set":{"customProjectSettings":{"$cond":{"if":{"$and":[{"$ifNull":["$customProjectSettings",false]},{"$isArray":"$customProjectSettings"}]},"then":{"$cond":{"if":{"$in":[2,"$customProjectSettings._projectId"]},"then":{"$map":{"input":"$customProjectSettings","as":"oneSet","in":{"$cond":{"if":{"$eq":["$$oneSet._projectId",2]},"then":{"$cond":{"if":{"$and":[{"$ifNull":["$$oneSet.settings",false]},{"$isArray":"$$oneSet.settings"}]},"then":{"$mergeObjects":["$$oneSet",{settings:{"$cond":{"if":{"$in":[2,"$$oneSet.settings._id"]},"then":{"$map":{"input":"$$oneSet.settings","as":"oneBaslik","in":{"$cond":{"if":{"$eq":[2,"$$oneBaslik._id"]},"then":{"$mergeObjects":["$$oneBaslik",{show:"exist so updated data"}]},"else":"$$oneBaslik"}}}},"else":{"$concatArrays":[[{_id:2,show:"if one Setting Id no exist"}],"$$oneSet.settings"]}}}}]},"else":{"$mergeObjects":["$$oneSet",{settings:[{_id:2,show:"settings array no exist or not array"}]}]}}},"else":"$$oneSet"}}}},"else":{"$concatArrays":["$customProjectSettings",[{_projectId:2,settings:[{_id:2,show:["if projectId no exist"]}]}]]}}},"else":[{"_projectId":2,"settings":[{"_id":2,"show":["if customProjectSettings array no exist"]}]}]}}}})