0

I have a collection called Forms: Inside that collection i have a sub collect called Form_Fields. So a document may look like this:

}
   "_id": ObjectId("4f2984b1af06e80418000000"),
   "Form_Name": "Users",
   "Form_Fields": [
                  {"Field_Name": "First_Name"},
                  {"Field_Name": "Last_Name"}
                  ]
}

So how do i find the array for the form fields?

I have tried :

$collection->find(array("Form_Name"=>"Users");

but that does return the array

First_Name, Last_Name

What i am trying to accomplish is to perform a find that will locate the document with the Form_Name = Users and then return an array that contains all of the Form_Fields.

I know this isn't right but something like:

find(array("Form_Name=>"Users"(array(Form_Fields)))

and it return the array of only the 2 Field_Names?

Can someone help me to construct the "find" to find the 'Users" form elements in an array. so i want to find all the 'Form_Fields' inside the document with the 'Form_Name' = 'Users' document.

Thank you.

Update

I want to search an embedded doc with a particular parent document attribute. So lets say i have a collection "Forms" and embedded inside that collection i have a doc called "Form_Fields". So that each form has embedded/related fields. I wanted to find all embedded form fields for the doc with the attribute "Form_Name":"Users".

Should i perform an embedded search like:

$cursor = $collection->find(array("Form_Name"=>"Users"));

//this brings back the doc where form_name = users. //Should i then expand that search by using something like:

$cursor -> find("the search to find all embedded Form_Fields")

??

1 Answer 1

1

You can specify what field from a document you want to fetch. Here's your example in javascript.

db.collection.find({"Form_Name": "Users"}, {"Form_Fields": 1});

This will return a document (or several) with two fields, _id and Form_Fields. If you don't want the _id, you have to specifically exclude it.

db.collection.find({"Form_Name": "Users"}, {"Form_Fields": 1, "_id": -1});
Sign up to request clarification or add additional context in comments.

6 Comments

but what if i want to fetch them all? Also i am using php not shell, can you give example in php? thank you.
Fetch all what? Also, you should be able to translate from shell language to PHP yourself, since all examples in the doc are in javascript.
Yes i can translate. Ok so let me restate the q for clarity. I want to search an embedded doc with a particular parent attribute. So lets say i have a collection "Forms" and embedded inside that collection i have a doc called "Form_Fields". So that each form has embedded/related fields. I wanted to find all embedded form fields for the doc with the attribute "Form_Name":"Users". See my update above.
@user982853, that's basically what this code does. Only it doesn't give you a naked array, but rather a partial document with only that field, Form_Fields.
But i am trying to return all the fields!
|

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.