我尝试了两种不同的方法来使用 Doctrine 的 MongoDB-ODM 进行相同的查询。
你能弄清楚为什么这两个在我看来相似的查询都返回不同的结果吗?Snippet 1 不返回 Snippet 2 返回正确数据库条目的任何内容。这两个查询在日志文件中看起来很相似——除了 #1 没有跳过和限制行。
片段 1
$dateDayAgo = new \DateTime('1 day ago');
$recentLogins = $this->get('user_activity_tracker')->findBy(array(
'targetUser' => $userAccount->getId(),
'code' => array('$in' => array('login.attempt','login.ok')),
'ts' => array('$gte', $dateDayAgo)
))->sort(['ts' => 1]);
Snippet 1 中 Symfony 的日志条目:
[2012-08-13 09:14:33] doctrine.INFO: MongoDB query: { "find": true, "query": { "targetUser": ObjectId("4fa377e06803fa7303000002"), "code": { "$in": [ "login.attempt", "login.ok" ] }, "ts": [ "$gte", new Date("Sun, 12 Aug 2012 09:14:33 +0000") ] }, "fields": [ ], "db": "eventio_com", "collection": "ActivityEvent" } [] []
[2012-08-13 09:14:33] doctrine.INFO: MongoDB query: { "sort": true, "sortFields": { "ts": 1 }, "query": { "targetUser": ObjectId("4fa377e06803fa7303000002"), "code": { "$in": [ "login.attempt", "login.ok" ] }, "ts": [ "$gte", new Date("Sun, 12 Aug 2012 09:14:33 +0000") ] }, "fields": [ ] } [] []
片段 2
$recentLoginsQuery = $this->get('user_activity_tracker')->createQueryBuilder()
->field('targetUser')->equals($userAccount->getId())
->field('code')->in(array('login.attempt','login.ok'))
->field('ts')->gte($dateDayAgo)
->sort('ts','asc')
->getQuery();
$recentLogins = $recentLoginsQuery->execute();
片段 2 的日志条目:
[2012-08-13 09:17:30] doctrine.INFO: MongoDB query: { "find": true, "query": { "targetUser": ObjectId("4fa377e06803fa7303000002"), "code": { "$in": [ "login.attempt", "login.ok" ] }, "ts": { "$gte": new Date("Sun, 12 Aug 2012 09:17:30 +0000") } }, "fields": [ ], "db": "eventio_com", "collection": "ActivityEvent" } [] []
[2012-08-13 09:17:30] doctrine.INFO: MongoDB query: { "limit": true, "limitNum": null, "query": { "targetUser": ObjectId("4fa377e06803fa7303000002"), "code": { "$in": [ "login.attempt", "login.ok" ] }, "ts": { "$gte": new Date("Sun, 12 Aug 2012 09:17:30 +0000") } }, "fields": [ ] } [] []
[2012-08-13 09:17:30] doctrine.INFO: MongoDB query: { "skip": true, "skipNum": null, "query": { "targetUser": ObjectId("4fa377e06803fa7303000002"), "code": { "$in": [ "login.attempt", "login.ok" ] }, "ts": { "$gte": new Date("Sun, 12 Aug 2012 09:17:30 +0000") } }, "fields": [ ] } [] []
[2012-08-13 09:17:30] doctrine.INFO: MongoDB query: { "sort": true, "sortFields": { "ts": 1 }, "query": { "targetUser": ObjectId("4fa377e06803fa7303000002"), "code": { "$in": [ "login.attempt", "login.ok" ] }, "ts": { "$gte": new Date("Sun, 12 Aug 2012 09:17:30 +0000") } }, "fields": [ ] } [] []
我的“user_activity_tracker”服务就像底层 Doctrine 存储库/文档管理器的代理一样工作。两个片段都在查询后返回一个 LoggableCursor。