0
votes

If I have many documents (or data objects) stored in MongoLab db similar to:

{"_id":{"$oid":"539467f6e4b02221d856bcd8"},
 "Object Type":"Data Object",
 "Object Name":"Human",
 "Subobject Types":
  {"S0":{"User ID":"x"},
   "S1":{"Password":"x"},
   "S2":{"First Name":"x"},
   "S3":{"Middle Name":"x"},
   "S4":{"Last Name":"x"},
   "S5":{"Cover Name":"x"},
   "S6":{"Taxpayer Identification":"x"},
   "S7":{"Email Address":"x"},
   "S8":{"Smartphone Number":"x"},
   "S9":{"Street":"x"},
   "S10":{"City":"x"},
   "S11":{"State":"x"},
   "S12":{"Nation":"x"},
   "S13":{"Postal Code":"x"}
 }
}

and if I make the following query call to MongoLab db using REST api...

https://api.mongolab.com/api/1/databases/my-db/collections/my-coll?q={}&apiKey=myAPIKey

where q=...

{ "Object Type":"Data Object",
  "Object Name":"Human",
  "Subobject Types":
  {"S0":{"User ID":{}},
   "S1":{"Password":{}},
   "S2":{"First Name":{}},
   "S3":{"Middle Name":{}},
   "S4":{"Last Name":{}},
   "S5":{"Cover Name":{}},
   "S6":{"Taxpayer Identification":{}},
   "S7":{"Email Address":{}},
   "S8":{"Smartphone Number":{}},
   "S9":{"Street":{}},
   "S10":{"City":{}},
   "S11":{"State":{}},
   "S12":{"Nation":{}},
   "S13":{"Postal Code":{}}
  }
}

...then why would db return nothing even though there are many documents (or data objects above) in db exactly matching above query?

It also returns nothing if I use the $exits query operator where q=

{"Object Type":"Data Object",
 "Object Name":"Human",
 "Subobject Types":
  {"S0":{"User ID":{"$exists":true}},
   "S1":{"Password":{"$exists":true}},
   "S2":{"First Name":{"$exists":true}},
   "S3":{"Middle Name":{"$exists":true}},
   "S4":{"Last Name":{"$exists":true}},
   "S5":{"Cover Name":{"$exists":true}},
   "S6":{"Taxpayer Identification":{"$exists":true}},
   "S7":{"Email Address":{"$exists":true}},
   "S8":{"Smartphone Number":{"$exists":true}},
   "S9":{"Street":{"$exists":true}},
   "S10":{"City":{"$exists":true}},
   "S11":{"State":{"$exists":true}},
   "S12":{"Nation":{"$exists":true}},
   "S13":{"Postal Code":{"$exists":true}}
  }
}
1
Can you edit your question to better format your JSON? It's very hard to read. - JohnnyHK
Tried to reformat it but input program will not accept 'tabs', 'returns', etc, to show reformat. - eric.atkinson
Sorry, figured out how to format after reading instructions. - eric.atkinson

1 Answers

1
votes

Happy to help try to explain the behavior you're seeing with those 2 queries. For the first, you're asking MongoDB to find documents that have an empty subdocument for every "Subobject Types.SX.Y" field, but there are probably no such documents where ALL of those are an empty embedded document.

The second is a misuse of the $exists operator. You'll probably get the results you're looking for if you formulated the query like so:

{
  "Object Type": "Data Object",
  "Object Name": "Human",
  "Subobject Types.S0.User ID": {"$exists": true},
  "Subobject Types.S1.Password": {"$exists": true},
  ...
  "Subobject Types.S13.Postal Code": {"$exists", true}
}

You need to use the dot notation when referring to fields in embedded documents in this case. If you provide a literal embedded document as part of the query, you're asking MongoDB to find an exact match on the value of the embedded document provided.

Hopefully that helps. You can always feel to free to email [email protected] with these types of questions as well, we're always happy to help.

Kind regards, Sean@MongoLab