skip to Main Content

This is literally my first foray into Mongo. I have exported my Active Directory into a Mongo collection. It’s structure, as best I can describe it as a complete novice, is a row, with an object containing properties which contain an array of one string. (See attached image for clarification of my limited vocab).

enter image description here

My question is, I want to filter to a specific "givenname". What do I put in filter to do that? I tried {givenname: "Test"}, but it matches nothing. I suspect because I have to drill all the way down through the multiple layers. Probably something like:

{Properties: { givenname: 0: {"Test"}}}

I really do not know the json syntax (’cause that’s what it looks like) to go from row, to property {} object, to column [] array, to array element 0, to value "Test".

EDIT: Here is an example of the JSON I imported to create this data. It’s just a straight dump of an LDAP query, with PII edited out.

[
  {
    "Path": "LDAP://CN=My Name,OU=Admins,OU=Service Accounts,OU=asdf Company,DC=asdf,DC=local",
    "Properties": {
      "objectclass": [
        "top",
        "person",
        "organizationalPerson",
        "user"
      ],
      "countrycode": [
        0
      ],
      "primarygroupid": [
        513
      ],
      "givenname": [
        "MyName"
      ],
      "codepage": [
        0
      ],
      "memberof": [
        "CN=sp-Net-FDA-RW,OU=Sharepoint,OU=Permission Groups"
      ],
      "samaccounttype": [
        805306368
      ],
      "description": [
        "IS - MyName Acct."
      ],
      "msds-supportedencryptiontypes": [
        0
      ],
    }
  },
  
  {
    // more accounts...
  }
  
]

As you can see it’s an array of objects, and each object’s properties is an array of one or more elements. But almost always an array of a single element. GivenName, for example.

2

Answers


  1. Try:

    { Properties: { givenname:  ["Test"] }}
    
    Login or Signup to reply.
  2. As mentioned in the comments, if your goal is to match a document where any of entries in the givenname array have the requested string, then the syntax is simply:

    { "Properties.givenname": "MyName" }
    

    You can see how this works in this playground example.

    This combines two concepts from MongoDB’s querying syntax:

    1. Dot notation to query on a nested field
    2. Querying an Array.

    Links to further reading on both topics are provided above. The two associated takeaways are, respectively:

    1. Dot notation should generally be preferred as it provides flexibility for searching for a component of a nested field (as opposed to the entire subdocument).
    2. MongoDB generally tries to search inside of arrays to see if any of the entries contain the requested value.

    The latter point is what helps address your latest comment:

    And, hey… GivenName is an array of one element. Why did you not have to index into the array to get "MyName?" Your query appears to be considering GivenName to be a string, when it’s actually an array with a string element in it? But your query worked just fine without making any reference to the array element

    You can see in the playground example that all three of these documents match the syntax above:

      {
        _id: 1,
        "Properties": {
          "givenname": [ "MyName" ]
        }
      },
      {
        _id: 2,
        "Properties": {
          "givenname": [ "First", "MyName" ]
        }
      },
      {
        _id: 3,
        "Properties": { "givenname": "MyName" }
      }
    

    If you specifically wanted something different you could express a more verbose query to prevent the unwanted documents from being returned. But generally this portion of the language attempts to yield the results that are most likely what you are looking for in a less verbose manner.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search