I have some json docs in cloudant DB like the following records.
{"@dataType":"GroupItem", "name":"aGroupName1", "GroupType":0}, {"@dataType":"GroupItem", "name":"aGroupName2", "GroupType":1, "Users":[{"deviceUUID":"id1", "userName":"user1"},{"deviceUUID":"id2", "userName":"user2"}] }
They are for groups and the users that belong to the group. I want to do a query to return the groups the login user belongs to.
So when "user1" login, I'd like to see both "aGroupName1" and "aGroupName2" being returned.
The condition is like: Select group when GroupType == 0 or (GroupType == 1 and userName == loginUser)
Because the userName is in an array in the json, I don't know how to do the query in Cloudant. Appreciate your help!
Jen
You'll need a map function like the following that emits a compound key:
function (doc) {
if (doc["@dataType"] == "GroupItem") {
doc.Users.map(function(user) {
emit([user.userName, doc.name]);
});
}
}
This will let you query by userName
and find all of the matching group name
values. Add GroupType
to that compound key if you also need to query by group type.