简体   繁体   English

Mongodb-用PHP查找嵌入式数组

[英]Mongodb - Find Embedded Array in PHP

I have a collection called Forms: Inside that collection i have a sub collect called Form_Fields. 我有一个名为Forms的集合:在该集合中,我有一个名为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. 我要完成的任务是执行查找,该查找将使用Form_Name = Users定位文档,然后返回包含所有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? 它只返回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. 有人可以帮助我构造“查找”以在数组中查找“用户”表单元素,所以我想在文档中使用“ Form_Name” =“ Users”文档查找所有“ Form_Fields”。

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". 因此,可以说我有一个“ Forms”集合,并且嵌入到该集合中,我有一个名为“ 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". 我想找到属性为“ Form_Name”:“ Users”的文档的所有嵌入式表单字段。

Should i perform an embedded search like: 我应该执行嵌入式搜索吗?

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

//this brings back the doc where form_name = users. //这将带回文档,其中form_name = users。 //Should i then expand that search by using something like: //然后我应该使用类似以下的方式扩展搜索范围:

$cursor -> find("the search to find all embedded Form_Fields") $ cursor-> find(“搜索以查找所有嵌入的Form_Fields”)

?? ??

You can specify what field from a document you want to fetch. 您可以指定要从文档中获取的字段。 Here's your example in javascript. 这是您在javascript中的示例。

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

This will return a document (or several) with two fields, _id and Form_Fields . 这将返回一个(或几个)包含两个字段_idForm_Fields If you don't want the _id , you have to specifically exclude it. 如果您不希望_id ,则必须专门排除它。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM