Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
调用删除mongo文档的php函数2次_Php_Mongodb - Fatal编程技术网

调用删除mongo文档的php函数2次

调用删除mongo文档的php函数2次,php,mongodb,Php,Mongodb,当我调用2次或更多次从mongo中删除文档的函数时,我会遇到问题 以下是函数: function deleteUser($userId){ $rangeQuery = array('metadata.userId' => intval($userId)); $cursor = $this->collection->remove($rangeQuery); } 如果我只调用一次,函数就可以正常工作。问题是当我多次调用该函数时。例如: deleteUser(2);

当我调用2次或更多次从mongo中删除文档的函数时,我会遇到问题

以下是函数:

function deleteUser($userId){
    $rangeQuery = array('metadata.userId' => intval($userId));
    $cursor = $this->collection->remove($rangeQuery);
}
如果我只调用一次,函数就可以正常工作。问题是当我多次调用该函数时。例如:

deleteUser(2);
deleteUser(3);
deleteUser(4);
此代码仅删除第一个用户(UserId=2)。未删除用户3或4。我需要重新加载页面以删除3,再次重新加载以删除4。在mongo中删除文档有任何限制吗

这是使用php mongolog类的日志:

Message: CON INFO: mongo_get_read_write_connection: finding a STANDALONE connection
Message: CON FINE: found connection 81.4
Message: CON FINE: is_ping: pinging 81.4
Message: CON FINE: send_packet: read from header: 36
Message: CON FINE: send_packet: data_size: 17
Message: CON WARN: is_ping: last pinged at 1383224885; time: 11701ms
Message: REPLSET FINE: finding candidate servers
Message: REPLSET FINE: - all servers
Message: REPLSET FINE: filter_connections: adding connections:
Message: REPLSET FINE: - connection: type: STANDALONE, socket: 1600, ping: 11701, hash: 81.4
Message: REPLSET FINE: filter_connections: done
Message: REPLSET FINE: limiting by seeded/discovered servers
Message: REPLSET FINE: - connection: type: STANDALONE, socket: 1600, ping: 11701, hash: 81.4
Message: REPLSET FINE: limiting by seeded/discovered servers: done
Message: REPLSET FINE: sorting servers by priority and ping time
Message: REPLSET FINE: - connection: type: STANDALONE, socket: 1600, ping: 11701, hash: 81.4
Message: REPLSET FINE: sorting servers: done
Message: REPLSET FINE: selecting near servers
Message: REPLSET FINE: selecting near servers: nearest is 11701ms
Message: REPLSET FINE: - connection: type: STANDALONE, socket: 1600, ping: 11701, hash: 81.4
Message: REPLSET FINE: selecting near server: done
Message: REPLSET FINE: pick server: random element 0
Message: REPLSET INFO: - connection: type: STANDALONE, socket: 1600, ping: 11701, hash: 81.4
Message: IO FINE: is_gle_op: no
Message: CON FINE: The requested database (readings) is not what we have in the link info (admin)
Message: CON FINE: The link info has 'admin' as database, no need to clone it then
Message: CON INFO: mongo_get_read_write_connection: finding a STANDALONE connection
Message: CON FINE: found connection 81.4 (looking for 81.4)
Message: CON FINE: is_ping: pinging 81.4
Message: CON FINE: is_ping: skipping: last ran at 1383224885, now: 1383224885, time left: 5
Message: REPLSET FINE: finding candidate servers
Message: REPLSET FINE: - all servers
Message: REPLSET FINE: filter_connections: adding connections:
Message: REPLSET FINE: - connection: type: STANDALONE, socket: 1600, ping: 11701, hash: 81.4
Message: REPLSET FINE: filter_connections: done
Message: REPLSET FINE: limiting by seeded/discovered servers
Message: REPLSET FINE: - connection: type: STANDALONE, socket: 1600, ping: 11701, hash: 81.4
Message: REPLSET FINE: limiting by seeded/discovered servers: done
Message: REPLSET FINE: sorting servers by priority and ping time
Message: REPLSET FINE: - connection: type: STANDALONE, socket: 1600, ping: 11701, hash: 81.4
Message: REPLSET FINE: sorting servers: done
Message: REPLSET FINE: selecting near servers
Message: REPLSET FINE: selecting near servers: nearest is 11701ms
Message: REPLSET FINE: - connection: type: STANDALONE, socket: 1600, ping: 11701, hash: 81.4
Message: REPLSET FINE: selecting near server: done
Message: REPLSET FINE: pick server: random element 0
Message: REPLSET INFO: - connection: type: STANDALONE, socket: 1600, ping: 11701, hash: 81.4
Message: IO FINE: is_gle_op: no
谢谢大家!

function deleteUser($userId){
    $rangeQuery = array('metadata.userId' => userId);
    $cursor = $this->collection->remove($rangeQuery);
}
您缺少userId变量前面的$符号

应该是:

$rangeQuery = array('metadata.userId' => $userId);

我建议您打开PHP错误日志记录来捕捉此类错误:)

不,没有限制我不确定问题出在哪里,但我不认为这是mongothank,谢谢您的回答您确定您所有的用户ID实际上都存储为数字吗?是的,我非常确定。我添加了intval以确保将整数传递给mongo。Mongo对此非常敏感。老实说,我能想到的唯一一件事是数据类型不匹配谢谢你bjori!!我的代码粘贴错误。我有‘$’。还有一个intval可以确保它是整数而不是字符串。现在问题仍然存在。如果我只调用一次,函数就可以完美运行。如果我呼叫2次或以上,mongo只删除第一个用户。我现在使用PHP日志运行,没有显示任何错误。很奇怪,也许你应该发布你正在使用的实际代码,不可能基于“也许它看起来像这样,并且像这样调用”来调试代码。我已经用实际的代码修改了这个问题(你对我添加的“$”和intval的观察),你能把$cursor变为var_dump()并发布你从中得到了什么吗?如果这没有提供任何有用的信息,请尝试启用驱动程序日志记录:php.net/mongolog.setcallback