如何在PHP中使用mongo查询文档集合的标题? [英] How would I query just the title of a document collection using mongo in PHP?

查看:71
本文介绍了如何在PHP中使用mongo查询文档集合的标题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用正则表达式搜索框,它只返回我的文档集中的文档标题。



这可能看起来很简单,但是,因为我是初学者,我正在努力完成这项任务。



这是我的代码:



I am working on a regex search box which will return only document titles from my document collection.

It may seem a simple task, however, as I am a beginner, I am struggling on completing this task.

This is my code:

<?php

//error_reporting(0);

    require 'view/header.php';

    include 'config.php';

?>

<form action="" method="post">
    <h4>Insert a regex command below</h4>
    <!--Regex: <input type="text" name="regex"/>-->
    Title: <input type="text" name="title"/>
    <input type="submit" value="Search"/><br/><br/>
</form>

<?php

    $m = new MongoClient();

    $db = $m->selectDB("fldev");
    $collections = $db->getCollectionNames();
    $collection = $m->selectCollection('fldev', 'docs');
    //var_dump($collection);

    echo "<pre>";
    foreach ($collections as $collectionName) {
      echo "Found collection: ", $collectionName, "<br/>";
    }
    echo "</pre><br/>";
?>

<?php

    //$regex=(isset($_POST["regex"]) ? $_POST["regex"] : "");

    $title=(isset($_POST["title"]) ? $_POST["title"] : "");

    //var_dump($regex);



    //$results = $collection->findOne(array('title' => 'greeklish abstract'));

  echo "<pre>";
    var_dump($collection->findOne(array('$title' => 'greeklish abstract')));
    var_dump($collection->findOne(array('$id' => '526937f8fa96470e3944a7ef')));
  echo "</pre>";
?>

<?php

    require 'view/footer.php';

?>







我试图从集合中获取一个文件,但它输出'NULL',就像在那里一样没有找到文件。




I tried to just get one document from the collection however it outputs 'NULL' as in there is no document found.

推荐答案

m = new MongoClient();
< span class =code-summarycomment>
m = new MongoClient();


db < span class =code-summarycomment> =
db =


m - > selectDB(fldev);
m->selectDB("fldev");


这篇关于如何在PHP中使用mongo查询文档集合的标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆