【问题标题】:Querying two MongoDB collections with PHP, first returns correct result, but second is empty用PHP查询两个MongoDB集合,第一个返回正确结果,第二个为空
【发布时间】:2017-05-12 16:59:11
【问题描述】:

我从 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 的 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."}

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

知道这种奇怪的行为吗?

谢谢 4 帮助,

JL

【问题讨论】:

    标签: php mongodb


    【解决方案1】:

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

    再喝 4 杯咖啡,然后……

    JL

    【讨论】:

      猜你喜欢
      • 2012-10-10
      • 1970-01-01
      • 2018-12-18
      • 2015-03-16
      • 1970-01-01
      • 2022-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多