0

私はmongoDBにこの構造を持つドキュメントのコレクションを持っています:

{
    _id: ObjectId("50f970caaa070bb8150000ae"),
    通称:「アルゼンチン」、
    ポブラシオン: 1000,
    州: [
        {
            通称:「ブエノスアイレス」、
            知事: "ホルヘ・ルイス",
            cantMedicos: 2,
            _id: ObjectId("50f970caaa070bb8150000a9")
        }、
        {
            通称:「カタマルカ」、
            知事: "Rodrigo Neira",
            カントメディコス: 5,
            _id: ObjectId("50f970caaa070bb8150000aa")
        }
    ]
}

親の「名前」が「アルゼンチン」に等しいすべての「州」からフィールド「gobernador」を出力したいと思います。しかし、PHPでこれを達成する方法が見つかりません。

4

1 に答える 1

0

The first step would be selecting all documents with { nombre: "Argentina" } via MongoCollection::find(). That should return an iterable MongoCursor and allow you to access all matched documents.

Each document will be an array, so you can access the appropriate field just as Sammaye mentioned in the comment above.

You also have the option of having find() project specific fields in the matching documents, which is helpful if your documents are very large and you'd like to reduce (a) the amount of data passed from MongoDB to the driver and/or (b) the amount of memory consumed to work with returned documents. Query projections are controlled via the second parameter to MongoCollection::find() and are documented in the aforementioned link.

于 2013-01-18T19:14:39.140 に答える