Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Conditional query of complex array elements
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  2 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
DaveC  
View profile  
 More options Oct 6 2012, 3:50 pm
From: DaveC <dacur...@gmail.com>
Date: Sat, 6 Oct 2012 12:50:21 -0700 (PDT)
Local: Sat, Oct 6 2012 3:50 pm
Subject: Conditional query of complex array elements

I've got a set of documents where I'm storing access control information in
an array on each document. This access control information contains a
"Group" and a "Right". I don't seem to be able to query based on both
elements at once, as in, where a certain group has a certain right.  Here
is example data:

{
  "Name": "Doc1",
  "Acl" : [ {"Group":"Users", "Right":"Read" } ]

},

{
  "Name": "Doc2",
  "Acl" : [ {"Group":"Users", "Right":"Read" }, { "Group" : "Admins",
"Right": "Read" }, { "Group" : "Admins", "Right": "Delete" }]
},

{
  "Name": "Doc3",
  "Acl" : [ {"Group":"Admins", "Right":"Read" } ]
},

{
  "Name": "Doc4",
  "Acl" : [ {"Group":"Users", "Right":"Delete" } ]

}

I'd like to find the documents where members of the group "Users" have the
right to "Delete". However, if I query like this:

db.docs.find({"Acl.Group":"Users", "Acl.Right":"Delete"})

then I get Doc2 and Doc4 because it matches on Acl.Group == "Users" OR
Acl.Right == "Delete".  Is there some way to find docs with an Acl array
element where Group = "Users" AND Right == "Delete"?  Is it possible
without JavaScript?  I'm running 2.2, if that helps.

Thanks.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
DaveC  
View profile  
 More options Oct 6 2012, 3:55 pm
From: DaveC <dacur...@gmail.com>
Date: Sat, 6 Oct 2012 12:55:02 -0700 (PDT)
Local: Sat, Oct 6 2012 3:55 pm
Subject: Re: Conditional query of complex array elements

Answered my own question...$elemMatch seems to do the trick:

db.docs.find({"Acl":{$elemMatch:{"Group":"Users", "Right":"Delete"}}})


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »