doudiejian5827
2016-09-23 11:25
浏览 90

查询时间戳字段比使用PHP的MongoDB中的另一个时间戳字段更旧

How can I obtain an object from a MongoDB collection where a specific field1 (timestamp or date) is older/newer than another specific field2 (timestamp or date)?

Given the following example object:

// MongoDB 3.2
{
  name: 'test',
  updated_on: Timestamp(1474416000, 0),
  export: {
    active: true,
    last_exported_on: Timestamp(1474329600, 0)
  }
}

This object should match a query like: where export.active is true and updated_on > export.last_exported_on

I've tried it with the aggregation framework, since I've read that $where can be very slow, but without any success.

// PHP 5.4 (and MongoDB PHP lib. http://mongodb.github.io/mongo-php-library)
$collection->aggregate([
  ['$project' => [
    'dst' => ['$cmp' => ['updated_on', 'export.last_exported_on']],
    'name' => true
  ]],
  ['$match' => ['dst' => ['$gt' => 0], 'export.active' => ['$eq' => true]]],
  ['$limit' => 1]
]);

I can change timestamps into date or anything else, but I don't see the problem in the type.

Edit: Not all objects have the last_exported_on or the export fields at all. Besides that both can be null or empty or 000000.

图片转代码服务由CSDN问答提供 功能建议

如何从MongoDB集合中获取特定字段1(时间戳或日期)较旧/较新的对象 另一个特定的字段2(时间戳或日期)?

给出以下示例对象:

  // MongoDB 3.2 
 {
 name  :'test',
 updated_on:Timestamp(1474416000,0),
 export:{
 active:true,
 last_exported_on:Timestamp(1474329600,0)
} 
} 
  <  / pre> 
 
 

此对象应与以下查询匹配:其中export.active为true且updated_on&gt; export.last_exported_on

我已经尝试使用聚合框架,因为我已经读过那里$可能很慢,但没有任何成功。

  // PHP 5.4(和MongoDB PHP lib。http://mongodb.github.io/mongo-php-library)
$collection->aggregate([
 ['  $ project'=&gt; [
'dst'=&gt; ['$ cmp'=&gt; ['updated_on','export.last_exported_on']],
'name'=&gt; true 
]],  
 ['$ match'=&gt; ['dst'=&gt; ['$ gt'=&gt; 0],'export.active'=&gt; ['$ eq'=&gt; true]]],\  n ['$ limit'=&gt; 1] 
]); 
   
 
 

我可以将时间戳更改为日期或其他任何内容,但我看不到 类型中的问题。

编辑:并非所有对象都具有 last_exported_on export 字段。 除此之外,两者都可以为null或空或000000。

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doukesou4452 2016-09-23 11:42
    已采纳

    That's because after you do the $project you end up only with the dst and _id fields, so you cannot $match on export.active. You need to match on export.active before the projection. After that you need another match on the dst field.

    [
        {
            $match: {
                "export.active": true
            }
        },
        {
            $project: {
                dst: {
                    $cmp: [
                        "$updated_on",
                        "$export.last_exported_on"
                    ]
                }
            }
        },
        {
            $match: {
                dst: 1
            }
        }
    ]
    

    Edit

    Alternatively, you can make sure to preserve export.active and to spare another $match:

    [
        {
            $project: {
                "export.active": 1,
                cmp: {
                    $cmp: [
                        "$updated_on",
                        "$export.last_exported_on"
                    ]
                }
            }
        },
        {
            $match: {
                cmp: 1,
                "export.active": true
            }
        }
    ]
    
    已采纳该答案
    打赏 评论

相关推荐 更多相似问题