如何返回 MongoDB 数组的单个元素?

发布于 2024-12-20 14:43:16 字数 265 浏览 4 评论 0原文

我想返回 Mongodb 数组的单个元素。我从表单中获取用户名和密码,并使用 findOne() 函数来验证它在数据库中是否存在。

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

在它返回的数组中还有一个邮政编码。我想将数组的该元素存储在一个变量中,以便将其与另一个变量连接起来。

I would like to return a single element of a Mongodb array. I am taking a username and password from a form and using the findOne() function to verify it's existence in the database.

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

In the array that it returns there is also a zip code. I would like to store that element of the array in a variable to concatenate it with another variable.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小镇女孩 2024-12-27 14:43:16
<?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

?>
<?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

?>
音栖息无 2024-12-27 14:43:16

您不需要返回所有这些信息。据我了解,您所需要的只是一个密码,因此您的查询应该是这样的

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

$user['pincode'] 将是您需要的密码。
第二个数组确保您不会收到除 pincode 之外的任何附加且不重要的信息

You don't need an to return all this information. As I understood all you need is a pincode, so your query should be this way

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

;

$user['pincode'] will be a pincode you need.
the second array ensure that you will not receive any additional and unimportant information except of pincode

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文