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

时间:2015-09-09 06:19:32

标签: php mongodb codeigniter

我是mongodb的新手。我想选择特定country

addressid

在mysql中我们将其视为

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

我如何使用codeigniter

在mongodb中实现这一点

这是我的用户记录

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',

以下是获取数据的模型文件

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

我试过这样的

  

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

但它不起作用

谢谢

2 个答案:

答案 0 :(得分:2)

尝试按照并告诉我它是否正常工作

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

答案 1 :(得分:0)

对于代码点火器https://github.com/verkhoumov/codeigniter-mongodb-library尝试这种方式

class DataService{
static let dataService = DataService()
private var _BASE_REF = Database.database().reference()
private var _ROOM_REF = Database.database().reference().child("rooms")
var BASE_REF: DatabaseReference {
   return _BASE_REF
}
var ROOM_REF:DatabaseReference{
    return _ROOM_REF
}
var storageRef: StorageReference{
    return Storage.storage().reference()
}
var fileURL: String!
// store the thumbnail in database
func CreateNewRoom(user: User, caption: String, data: NSData){
    let filePath = "\(user.uid)/\   
(Int(NSDate.timeIntervalSinceReferenceDate))"

    let metaData = StorageMetadata()
    metaData.contentType = "image/jpg"
    storageRef.child(filePath).putData(data as Data, metadata: metaData){ 
(metadata, error) in if error != nil {
        print ("Error Uploading: \(String(describing: 
error?.localizedDescription))")
        return
        }
        //create a url for data (photo thumbnail image)
          _ = metadata?.storageReference?.downloadURL(completion: error as! 
(URL?, Error?) -> Void)
        if Auth.auth().currentUser != nil {
            let idRoom = self.BASE_REF.child("rooms").childByAutoId()
            idRoom.setValue(["caption": caption,"thumbnailURLFromStorage": 
self.storageRef.child(metadata!.path!).description, "fileURL" : 
self.fileURL])
        }
    }
}

func fetchDataFromServer(callback: @escaping (Room) -> ()){
    DataService.dataService.ROOM_REF.observe(.childAdded){ (snapshot) in
        let room = Room(key: snapshot.key, snapshot: snapshot.value as! 
Dictionary<String, Any>)
     callback(room)
    }
}
func SignUp(username:String, email: String, password: String, firstName: 
String, lastName: String, data: NSData){
    Auth.auth().createUser(withEmail: email, password: password, completion: 
{ (user, error) in
        if  error != nil {
            print(error!)
        }
        else {
            print("Registration Successful")
        }
        let changeRequest = 
Auth.auth().currentUser?.createProfileChangeRequest()
        changeRequest?.displayName = username
        changeRequest?.commitChanges(completion: {(error) in
            if let error = error {
                print (error.localizedDescription)
                return
            }
        })
        let filePath = "profileimage/\(String(describing: 
Auth.auth().currentUser!.uid))"
        let metadata = FirebaseStorage.StorageMetadata()
        metadata.contentType = "image/jpeg"

        self.storageRef.child(filePath).putData(data as Data, metadata:  
metadata, completion: {(metadata, error) in

        if let error = error {
            print ("\(error.localizedDescription)")
            return
        }
            _ = metadata?.storageReference?.downloadURL(completion: error as! 
(URL?, Error?) -> Void)
            if let error = error {
                print (error.localizedDescription)
                return
            }
            else {
                print ("Sweet!")
            }
        let appDelegate: AppDelegate = UIApplication.shared.delegate as! 
AppDelegate
        appDelegate.login()

    })
    }
}

必需的字段可以在数组中提供给select([])函数,它将返回带有选定字段的对象。