title: Making a query on a mongo DB with ParseSwift
body: <p>I have two collections in a Mongo DB.</p>
  <p>Here is how a document looks in the first collection (<code>MainCollection</code>):</p>
  <pre><code>_id
  :&quot;mzWqPEDYRU&quot;
  TITLE
  :&quot;ZAZ: I want.&quot;
  ownerID
  :&quot;lGutCBY52g&quot;
  accessKey
  :&quot;0kAd4TOmoK0&quot;
  _created_at
  :2020-03-13T11:42:11.169+00:00
  _updated_at
  :2020-03-13T17:08:15.090+00:00
  downloadCount
  :2
  </code></pre>
  <p>And here is how it looks in the second collection (<code>SecondCollection</code>):</p>
  <pre><code>_id
  :&quot;07BOGA8bHG&quot;
  _p_unit
  :&quot;MainCollection$mzWqPEDYRU&quot;
  SENTENCE
  :&quot;I love nature peace and freedom.&quot;
  Order
  :5
  ownerID
  :&quot;lGutCBY52g&quot;
  AUDIO
  :&quot;07067b5589d1edd1d907e96c1daf6da1_VOICE.bin&quot;
  _created_at
  :2020-03-13T11:42:17.483+00:00
  _updated_at
  :2020-03-13T11:42:19.336+00:00
  </code></pre>
  <p>There is a parent children relationship between the first and the second collection. In the last document we can see the _p_unit field where the &quot;mzWqPEDYRU&quot; part points to the id of the parent in the first collection.</p>
  <p>Though I finally get what I want, getting elements of the second collection with a given parent, it is not done how it should.</p>
  <p>I have one problem making a selective query on <code>SecondCollection</code>. Here is the currently working code:</p>
  <pre><code>func theFunction(element: MainCollection) {
      do {SecondCollection.query().find() {
              result in
              switch result {
              case .success(let items):
                  print(&quot;items.count = \(items.count)&quot;)
                  var finalCount = 0
                  for item in items {
                      // Ignore useless elements:
                      if item.unit?.objectId != element.objectId! {continue}
  
                      finalCount += 1
                      /// .... Work with selected element.
                  }
                  print(&quot;finalCount = \(finalCount)&quot;)
              case .failure(let error):
                  print(&quot;Error in \(#function): \(error)&quot;)
              }
          }
      }
  }
  </code></pre>
  <p>The way the above code is written works in the sense that I get the elements in <em>SecondCollection</em> I am interested in. But this trick inside the <em>for loop</em> to eliminate the non-needed element is not the way to go.</p>
  <pre><code>if item.unit?.objectId != element.objectId! {continue}
  </code></pre>
  <p>The filtering should happen in the query, with the line:</p>
  <pre><code>SecondCollection.query().find()
  </code></pre>
  <p>The problem is that everything I have tried failed. I did things like:</p>
  <pre><code>SecondCollection.query(&quot;unit&quot; == element.objectId!).find()
  </code></pre>
  <p>with a zillion variations, but all with no luck.</p>
  <p>Does anybody know the proper syntax?</p>
  <p>In case this may be useful, here is how SecondCollection is declared:</p>
  <pre><code>struct SecondCollection: ParseObject,Identifiable,Equatable,Hashable {
      // These fields are required for any Object.
      var objectId: String?
      var createdAt: Date?
      var updatedAt: Date?
      var ACL: ParseACL?
      
      // Local properties.
      var id:UUID {return UUID()}
      var SENTENCE: String?,
          Order: Int?,
          ownerID: String?,
          AUDIO: ParseFile?,
          unit: Pointer&lt;MainCollection&gt;?
      .......
  }
  </code></pre>