2011-12-12 3 views
1

Mongodb 배열의 단일 요소를 반환하고 싶습니다. 양식에서 사용자 이름과 암호를 가져오고 findOne() 함수를 사용하여 데이터베이스에 존재하는지 확인합니다.MongoDB 배열의 단일 요소를 반환하는 방법은 무엇입니까?

$user = $collection->findOne(array(
    'username' => $username, 
    'password' => $password, 
)); 

반환하는 배열에는 우편 번호도 있습니다. 그 배열의 요소를 다른 변수와 연결하는 변수에 저장하고 싶습니다.

+3

의 제외 추가와 중요하지 않은 정보를 수신하지 않도록? –

답변

1
<?php 

$user = $collection->findOne(array(
    'username' => $username, 
    'password' => $password, 
    )); 

var_dump($user); // you will see your document as a PHP associative array here 
$myPin = $user['pincode_keyname']; // or whatever name your pincode element has 

?> 
0

이 정보를 모두 반환하지 않아도됩니다. 당신이 필요로하는 모든 것이 pincode라는 것을 이해 했으므로 질의는 다음과 같아야합니다.

$user = $collection->findOne(
    array(
    'username' => $username, 
    'password' => $password, 
), 
    array(
    'pincode' => 1, 
    '_id'  => 0 
) 
) 

;

$ 사용자 [ 'PIN 코드는'] 당신이 필요 PIN 코드가 될 것입니다. 두 번째 배열은 PIN 코드하지 않습니다`$ ZIP_CODE = $ 사용자 [ "zip_code_or_something"]`작업