问题
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