我有一个具有以下结构的文件:
user {id: 123, tag:"tag1"}
user {id: 123, tag:"tag2"}
user {id: 123, tag:"tag3"}
user {id: 456, tag:"tag1"}
给定用户 ID,我想查找该用户是否具有所有 3 个标签(AND 操作数)的记录。如果用户有 "tag1" AND "Tag2" AND "tag3" 的记录,则返回 true
SQL 等效项如下所示:
SELECT * FROM users WHERE
EXISTS (SELECT * FROM tags WHERE user_id = users.id AND name ='tag1') AND
EXISTS (SELECT * FROM tags WHERE user_id = users.id AND name ='tag2') AND
user_id=123
如何在 MongoDB 中表达类似的东西?