简体   繁体   English

find()查询MongoDB的php

[英]find() query MongoDB for php

Hi i am using mongoDb for my new project. 嗨,我在我的新项目中使用mongoDb。 I am trying to find values from mongodb database. 我正在尝试从mongodb数据库中找到值。 My mongoDB database collection name is: test and json format is below 我的mongoDB数据库集合名称是:test和json格式如下

{
"College": [
    {
        "name": "tamy",
        "roll_no": "1"
    },
    {
        "name": "abhi",
        "roll_no": "2"
    },
    {
        "name": "jack",
        "roll_no": "3"
    }
],
"School": [
    {
        "name": "zack",
        "roll_no": "1"
    },
    {
        "name": "mac",
        "roll_no": "2"
    },
    {
        "name": "john",
        "roll_no": "3"
    }
]
}

**And i want to find the name:abhi from test collection ** and my mongodb find query in php **我想从测试集合中找到名称:abhi **,而我的mongodb在php中找到查询

 $criteria = array( 
                'College'=> array(
                    'name'=> 'abhi'
   ));
$cursor = $collection->find($criteria);

but its not returning any value. 但它没有返回任何值。 if i am displaying in php by using print_r. 如果我通过使用print_r在php中显示。

print_r(iterator_to_array($cursor));

displaying empty array() 显示空的array()

I need output like this: name : jack roll_no : 3 我需要这样的输出:name:jack roll_no:3

plz help me.. 请帮助我。

The key "College" is an array, your trying to treat it as a "subdocument" (but that wouldnt allow for multiple colleges to be stored) 密钥“学院”是一个数组,您尝试将其视为“子文档”(但是不会存储多个学院)

You want the elemMatch keyword here I believe: http://docs.mongodb.org/manual/reference/operator/query/elemMatch/ 您相信这里需要elemMatch关键字: http ://docs.mongodb.org/manual/reference/operator/query/elemMatch/

EDIT: After looking at this again, I think you have your structure a little out of whack. 编辑:再次查看此内容后,我认为您的结构有些不合理。 How many "students" will there be per college or school? 每所大学或学校将有多少名“学生”? Keep in mind each parent document can not exceed 10mb, and when you select a document, the entire document must be sent back across the connection (unless you are using aggregate or map/reduce. I would think you may want to have a collection of Persons and then a key associating a person to... something else. 请记住,每个父文档不能超过10mb,并且当您选择一个文档时,整个文档必须通过连接发送回(除非您使用的是汇总或映射/归约。我想您可能希望收集人员,然后是将人员关联到其他事物的密钥。

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

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