无法使用 PHP 通过游标访问 MongoDB 中的值
当我尝试从 MongoDB 中提取数据时,我在使用游标时似乎遇到了一些问题。这是我的简单数据结构的屏幕截图:
数据结构屏幕截图 http://droplr.com/IpcH+
I'我尝试使用以下内容:
$collection_name = "users";
$collection = $this->mongo->db->$collection_name;
$which_document = array("email" => $email_address);
$which_fields = array("words");
$cursor = $collection->find($which_document, $which_fields);
但我的 $cursor 似乎并没有比实际文档更进一步地进入文档。最后,我希望对“words”字段内的created_at字段进行sort()和limit(),但我在这里似乎陷入了僵局。要么 MongoDB 的错误传递不是很有帮助,要么我不知道如何正确使用它。 (我假设是后者。)
一个想法:我不确定方括号内生成的“单词”表是否正确,但这是我用来在此处插入数据的代码:
function create_word($word, $email_address){
$collection = 'users';
// make sure word isn't null
if($word !== NULL){
// add new data to the 'words' set
$new_data = array('$addToSet' => array('words' => array(
'word' => $word,
'status' => 'open',
'created_at' => time()
)));
$this->mongo->db->$collection->update(array('email' => $email_address), $new_data);
}
非常感谢您的帮助你能给予的帮助!
I seem to be having some trouble using the cursor as I try to pull data out of a MongoDB. Here is a screenshot of my simple data structure:
data structure screenshot http://droplr.com/IpcH+
I'm trying to use the following:
$collection_name = "users";
$collection = $this->mongo->db->$collection_name;
$which_document = array("email" => $email_address);
$which_fields = array("words");
$cursor = $collection->find($which_document, $which_fields);
But my $cursor does not seem to be going any further into the document than the actual document. In the end, I'm looking to sort() and limit() on the created_at field within the "words" field, but I seem to be at an impasse here. And either MongoDB's error passing isn't very helpful, or I've no idea how to use it properly. (I'd assume the latter.)
One idea: I'm not sure the "words" table being generated inside of square brackets is correct, but here is the code I'm using to insert data here:
function create_word($word, $email_address){
$collection = 'users';
// make sure word isn't null
if($word !== NULL){
// add new data to the 'words' set
$new_data = array('$addToSet' => array('words' => array(
'word' => $word,
'status' => 'open',
'created_at' => time()
)));
$this->mongo->db->$collection->update(array('email' => $email_address), $new_data);
}
Thanks so much for any help you can give!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您无法对子文档进行排序。您需要在客户端执行此操作。在您的情况下,检索单词数组:
然后使用 php 对值进行排序。
You can't sort subdocuments. You need to do it on the client side. In your case retrieve the words array:
and then use php to sort the values.
尝试
Try