How to push or pull to embed array if exists in only a query mongodb

末鹿安然 提交于 2019-12-11 05:58:00

问题


I have a likes array embed in photos collection. Likes array contains user_id, Every time the user presses the like button, I need to check if the user_id exists in the like array. If it doesn't exist, I push user_id to likes array, else I pull it out of likes array. So I need to perform two queries. I want to encapsulate them in a single query.

Photos:

"likes" : [ 
        {
            "user_id" : ObjectId("")
        }, 
        {
            "user_id" : ObjectId("")
        }
],

来源:https://stackoverflow.com/questions/57840431/how-to-push-or-pull-to-embed-array-if-exists-in-only-a-query-mongodb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!