Reputation: 16656
I need to store the role ID and role name in each document in my Users
collection. I need to be able to say "Get Users that have xyz role" or "Get users that have a role who's name contains 'XYZ'", stuff like that.
Is that even possible in Mongo?
Right now, I'm storing data like this:
{
"_id": {
"$id": "4fe30e3db92f5d2a5c000000"
},
"alias": "rogue_coder",
"display_name": "Rogue Coder",
"email": "[email protected]",
"roles": {
"4fe30e5fb92f5d6f53000000": "Super Administrator"
}
}
With that, I can see if a user has a given role, but I can't get all users who have a role that contains the word "Super" for example. Any ideas on how to do this?
Upvotes: 0
Views: 275
Reputation: 1418
Not sure but you may try a bit like this
db.collection.find( { roles: { /super.*/i } );
Upvotes: 1
Reputation: 1037
Based on what your attempting to do, you may have to store role_id & role_name in separate arrays, like such:
{
"_id": {
"$id": "4fe30e3db92f5d2a5c000000"
},
"alias": "rogue_coder",
"display_name": "Rogue Coder",
"email": "[email protected]",
"role_names": [
"Super Administrator"
],
"role_ids" : [
"4fe30e5fb92f5d6f53000000"
]
}
Upvotes: 1
Reputation: 1964
MongoDB has support for the regular expression for matching key's values.
Take look at this MongoDB Regular Expression
Upvotes: 1