简体   繁体   English

如何使用带有Codeigniter的mongoDB选择特定字段?

[英]How can i select particular field using mongoDB with codeigniter?

I am new in mongodb. 我是mongodb的新手。 I want to select the the country and address of particular id 我想选择特定idcountryaddress

in mysql we will wright it as 在MySQL中,我们将其作为

"Select address,country from table_name where user_id=value"

how I can implement this in mongodb with codeigniter 我如何使用codeigniter在mongodb中实现这一点

here is my user record 这是我的用户记录

array (
  '_id' => '55bb1f3bb117284412000032',
  'name' => 'Sriram',
  'email' => 'sri@gmail.com',
  'password' => '123456',
  'phone' => '354657',
  'tittle' => 'Ms',
  'sname' => 'Kumar',
  'dob' => '07/22/2015',
  'gender' => 'Male',
  'address' => 
  array (
    'address' => 'Pantheon road',
    'street' => 'Egmore',
    'city' => 'Chennai',
  ),
  'country' => 'India',

)

Here is the model file for fetching data 这是用于获取数据的模型文件

$collection = $this->mongo_db->db->selectCollection('Users');
$profile = $collection->findOne(array('email' => 'athira@gmail.com'));  

I have tried like this 我已经试过了

$profile = $collection->findOne(array('email' => 'athira@gmail.com')).country; $ profile = $ collection-> findOne(array('email'=>'athira@gmail.com'))。country;

but its not working 但它不起作用

thank you 谢谢

Try following and let me know whether it is working or not 尝试关注并让我知道它是否有效

$collection->aggregate(
                    array(
                        '$match' => array(
                            '$and' => array(
                                array('email' => 'athira@gmail.com')        
                            )
                        )
                    ),
                    array(
                        '$group' => array(
                            '_id' => '$country'
                        )
                    )
                );

Try this way for codeigniter https://github.com/verkhoumov/codeigniter-mongodb-library 对于codeigniter尝试这种方式https://github.com/verkhoumov/codeigniter-mongodb-library

$this->load->library('mongo_db');
$data = $this->mongo_db->select(['address','country'])->where(['user_id'=>$user_id])->get("collection_name");

The required fields can be given in an array to the select([]) function ,it will return the objects with selected fields. 可以在数组中将必需字段提供给select([])函数,它将返回带有选定字段的对象。

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

相关问题 如何使用Codeigniter选择不同的mongodb数据库 - How to select distinct mongodb database using Codeigniter 如何使用jquery选择特定字段 - how to select a particular field using a jquery 如何在 Codeigniter 中显示具有特定字段绑定条件的字段? - How can I display a field with a certain field bound condition in Codeigniter? 使用select2,如何使用CodeIgniter通过PHP获取多个数据? - Using select2, how can I get multiple data by PHP using CodeIgniter? 如何使用带有 codeigniter 的 Select2 插件将多个数据插入到我的数据库中? - How can I insert multiple data to my database using the Select2 plugin with codeigniter? 如何在Codeigniter中按键字段更新所有设置字段? - How can I update all settings fields by key field in codeigniter? 如何在Codeigniter表单验证中获取字段数组的索引? - How can I get the index of a field array in codeigniter form validation? 如何在mysql数据库的特定列中选择最后一个值? - How can i select the last value in a particular column in mysql database? 如何选择此特定php数组的最后一个元素? - How can I select the last element of this particular php array? 如何在Flex的Datagrid itemrenderer中选择特定的复选框? - how can i select particular checkbox within datagrid itemrenderer in flex?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM