使用PHP查询两个MongoDB集合,首先返回正确的结果,但第二个是空的

时间:2017-05-12 16:59:11

标签: php mongodb

我从PHP运行两个MongoDB查询,一个在第一个集合上(名为" products"),第二个在另一个集合上(名为stats)。

两个馆藏的一些数据:

{
"_id" : "20711122",
"labels_hierarchy" : [ 
    "en:pgi"
],
"languages_hierarchy" : [ 
    "en:french"
],
"labels_prev_hierarchy" : [ 
    "en:pgi"
],
"languages" : {
    "en:french" : 5
},
"countries_tags" : [ 
    "en:france"
],
"purchase_places_debug_tags" : [],
"photographers_tags" : [ 
    "tacite"
]
}

其他收藏:

{
"_id" : "7613035010550",
"purchases" : [ 
    {
        "date" : ISODate("2017-04-15T14:15:00.000Z"),
        "coords" : {
            "lon" : 43.729604,
            "lat" : 1.416017
        },
        "metar" : {},
        "quantity" : 1,
        "price" : 2.31
    }, 
    {
        "date" : ISODate("2017-05-02T16:23:00.000Z"),
        "coords" : {
            "lon" : 43.722862,
            "lat" : 1.415837
        },
        "metar" : {},
        "quantity" : 6,
        "price" : 12
    }, 
    {
        "date" : ISODate("2017-05-02T18:32:00.000Z"),
        "coords" : {
            "lon" : 46.307353,
            "lat" : 3.28937
        },
        "metar" : {},
        "quantity" : 2,
        "price" : 5
    }
],
"rates" : [ 
    {
        "value" : 5
    }, 
    {
        "value" : 4
    }, 
    {
        "value" : 5
    }, 
    {
        "value" : 2
    }
]
}

正如你所看到的,收藏品是不同的,但" _id"键...

当我对第一个集合(产品)运行查询时,一切正常,但第二个具有相同的" _key"返回空结果,这里是两个查询的跟踪(用MongoDB :: Driver的executeQuery()创建):

<pre><code>Interrogation de la collection : stats
Avec le filtre :
{"id":"7613035010550"}
<pre><code>Interrogation de la collection : products
Avec le filtre :
{"id":"7613035010550"}
Results :
{"ean":"7613035010550","title":"Eau Min\u00e9rale Naturelle","image":"https:\/\/static.openfoodfacts.org\/images\/products\/761\/303\/501\/0550\/front_fr.11.400.jpg","brands":"Vittel","categories":["Boissons","Eaux","Eaux min\u00e9rales","Eaux min\u00e9rales naturelles","Boissons non sucr\u00e9es"],"quantity":"1.5 l."}

正如您所见,只有第一个查询返回结果,而不是第二个...

对这种好奇的行为有什么看法吗?

Thx 4帮助,

JL

1 个答案:

答案 0 :(得分:0)

我只是愚蠢或累... 第二个查询是使用“id”键执行但不是“_id”键...所以,总是返回空结果......

再喝4杯咖啡,接下来......

JL