如何通过 php 中的 ObjectId 查找 mongodb 集合条目
How to find a mongodb collection entry by ObjectId in php
我有一个 mongodb 数据库,其中包含两个连接的集合。
第一个有一个如下所示的数据集:
{
"_id": ObjectId("5326d2a61db62d7d2f8c13c0"),
"reporttype": "visits",
"country": "AT",
"channel": "wifi",
"_level": NumberInt(3)
}
ObjectId 连接到第二个集合中的几个数据集,如下所示:
{
"_id": ObjectId("54c905662d0a99627efe17a9"),
"avg": NumberInt(0),
"chunk_begin": ISODate("2015-01-28T12:00:00.0Z"),
"count": NumberInt(15),
"max": NumberInt(0),
"min": NumberInt(0),
"sum": NumberInt(0),
"tag": ObjectId("5326d2a61db62d7d2f8c13c0")
}
如您所见,第一个数据集中的“_id”与第二个数据集中的 "tag" 相同。
我想在php中写一个例程,它从第一个集合中获取id,并在第二个集合中找到特定时间范围内的数据集以进行删除。
我从第一个集合中获得了 id,但我怀疑我在第二个集合的查询中错误地使用了它,因为没有找到或删除任何东西。
代码如下所示:
// select a collection (analog to a relational database's table)
$tagCollection = $db->tags;
$orderCollection = $db->orders;
// formulate AND query
$aTagCriteria = array(
'reporttype' => new MongoRegex('/[a-z]+/'),
);
// retrieve only _id keys
$fields = array('_id');
$cursor = $tagCollection->find($aTagCriteria, $fields);
$startOfTimeperiod = new MongoDate(strtotime('2015-01-05 00:00:00'));
$endOfTimeperiod = new MongoDate(strtotime('2015-01-07 13:20:00'));
// iterate through the result set
foreach ($cursor as $obj) {
echo '_id: '.$obj['_id'].' | ';
// Until here all is ok, I get the _id as output.
$aOrdercriteria = array(
'tag' => new MongoId($obj['_id']),
'date' => array(
'$lte' => $endOfTimeperiod,
'$gte' => $startOfTimeperiod
),
);
$iCount = $orderCollection->count($aOrdercriteria);
if ($iCount > 0) {
echo PHP_EOL.$iCount.' document(s) found.'.PHP_EOL;
$result = $orderCollection->remove($aOrdercriteria);
echo __FUNCTION__.'|'.__LINE__.' | '.json_encode($result).PHP_EOL;
echo 'Removed document with ID: '.$aOrdercriteria['tag'].PHP_EOL;
}
}
搜索条件的正确方法是什么,所以它会寻找标签对象
使用之前找到的 ID?
PS:
我试过了
'tag' => $obj['_id'],
而不是
'tag' => new MongoId($obj['_id']),
也没有用。
所以有两件事必须改变。
第一个就像 EmptyArsenal 暗示的那样:
tag' => new MongoId($obj['_id']),
错误,因为 $obj['_id'] 已经是一个对象。
所以
'tag' => $obj['_id'],
是正确的。
如果我将条件从 "date" 更改为 "chunk_begin" yahooo.... 它会起作用。愚蠢的我。
我有一个 mongodb 数据库,其中包含两个连接的集合。
第一个有一个如下所示的数据集:
{
"_id": ObjectId("5326d2a61db62d7d2f8c13c0"),
"reporttype": "visits",
"country": "AT",
"channel": "wifi",
"_level": NumberInt(3)
}
ObjectId 连接到第二个集合中的几个数据集,如下所示:
{
"_id": ObjectId("54c905662d0a99627efe17a9"),
"avg": NumberInt(0),
"chunk_begin": ISODate("2015-01-28T12:00:00.0Z"),
"count": NumberInt(15),
"max": NumberInt(0),
"min": NumberInt(0),
"sum": NumberInt(0),
"tag": ObjectId("5326d2a61db62d7d2f8c13c0")
}
如您所见,第一个数据集中的“_id”与第二个数据集中的 "tag" 相同。
我想在php中写一个例程,它从第一个集合中获取id,并在第二个集合中找到特定时间范围内的数据集以进行删除。
我从第一个集合中获得了 id,但我怀疑我在第二个集合的查询中错误地使用了它,因为没有找到或删除任何东西。
代码如下所示:
// select a collection (analog to a relational database's table)
$tagCollection = $db->tags;
$orderCollection = $db->orders;
// formulate AND query
$aTagCriteria = array(
'reporttype' => new MongoRegex('/[a-z]+/'),
);
// retrieve only _id keys
$fields = array('_id');
$cursor = $tagCollection->find($aTagCriteria, $fields);
$startOfTimeperiod = new MongoDate(strtotime('2015-01-05 00:00:00'));
$endOfTimeperiod = new MongoDate(strtotime('2015-01-07 13:20:00'));
// iterate through the result set
foreach ($cursor as $obj) {
echo '_id: '.$obj['_id'].' | ';
// Until here all is ok, I get the _id as output.
$aOrdercriteria = array(
'tag' => new MongoId($obj['_id']),
'date' => array(
'$lte' => $endOfTimeperiod,
'$gte' => $startOfTimeperiod
),
);
$iCount = $orderCollection->count($aOrdercriteria);
if ($iCount > 0) {
echo PHP_EOL.$iCount.' document(s) found.'.PHP_EOL;
$result = $orderCollection->remove($aOrdercriteria);
echo __FUNCTION__.'|'.__LINE__.' | '.json_encode($result).PHP_EOL;
echo 'Removed document with ID: '.$aOrdercriteria['tag'].PHP_EOL;
}
}
搜索条件的正确方法是什么,所以它会寻找标签对象 使用之前找到的 ID?
PS: 我试过了
'tag' => $obj['_id'],
而不是
'tag' => new MongoId($obj['_id']),
也没有用。
所以有两件事必须改变。
第一个就像 EmptyArsenal 暗示的那样:
tag' => new MongoId($obj['_id']),
错误,因为 $obj['_id'] 已经是一个对象。
所以
'tag' => $obj['_id'],
是正确的。 如果我将条件从 "date" 更改为 "chunk_begin" yahooo.... 它会起作用。愚蠢的我。