0

I have a mongoDB set up like so:

fieldName: {
  "1": {
      client: "name-here",
      subfield: "more-junk-here"
   },
   "2": {
      client: "another-client-name-here",
      subfield: "more-data"
   },
   "3": {
      client: "client-num-3",
      subfield: "i-like-turkey-sandwiches"
   }
}

How do I query for "i-like-turkey-sandwiches", without knowing it's in position 3, and without doing a search of the whole document? I started writing this, but am completely stumped how to do the search...

$cursor = $collection->findOne(array('fieldName' => 'i-like-turkey-sandwiches));

1 Answer 1

1

(edit: My answer is valid only if fieldName content is an array of subdocument and not a document with "1"/2/3 as key and subdocument as value)

If you query with

$cursor = $collection->findOne(
              array('fieldName.subfield' => 'i-like-turkey-sandwiches'));

It will return you all documents where you have a subfield inside a fieldName field with this value.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.