[英]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
我想选择特定
id
的country
和address
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.