简体   繁体   English

Mongo DB 查询问题

[英]Mongo DB query issue

I am beginner in Mongo DB.我是 Mongo DB 的初学者。 I wrote the query see below for your further reference.我写了下面的查询供您进一步参考。

My Query is我的查询是

$result =  $this->likes->find(array("post_id" => $post_id));

In that my table name is "likes" having the field name post_id , I am passing the $post_id (its my dynamic value example $post_id= "55b86fb60fdd9419128b4567" ).因为我的表名是具有字段名称post_id “喜欢”,我传递了$post_id (它的动态值示例$post_id= "55b86fb60fdd9419128b4567" )。

But I cannot get my expected result from the below query, if I pass the $post_id is static means I got it my desired result.但是我无法从下面的查询中得到我的预期结果,如果我通过$post_id是静态的意味着我得到了我想要的结果。

Thanks advance for your help提前感谢您的帮助

You are attempting to use a string to match a MongoId .您正在尝试使用字符串来匹配MongoId Keep in mind that these are two different datatypes.请记住,这是两种不同的数据类型。 This is there in documentation这是在文档中

You should query like following:您应该查询如下:

$result =  $this->likes->find(array("post_id" => new MongoId($post_id)));

As explained in doc , You can print query result by using -文档中所述,您可以使用 - 打印查询结果

foreach ($result as $doc) {
  var_dump($doc);
}

请尝试以下操作:

$result =  $this->likes->find(array("post_id" => new MongoId($post_id)));

Try This:尝试这个:

function connection($tableName){        
    // Connect to Mongo
    //$this->connection = new MongoClient('localhost:27017'); //for linux
    $this->connection = new Mongo('localhost:27017'); // for window

    // Select a database
    $this->db = $this->connection->databaseName;

    // Select a collection
    $this->posts = $this->db->$tableName;
}

$this->connection('likes'); // likes is collection name
$id= "55b86fb60fdd9419128b4567";
$members = $this->posts->find(array("post_id" => $id));

Try this:尝试这个:

$result = $this->likes->find(array("post_id" => new\\ MongoId($post_id))); $result = $this->likes->find(array("post_id" => new\\ MongoId($post_id)));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM