Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8437415
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T07:25:15+00:00 2026-06-10T07:25:15+00:00

I am converting from mysqli to mongodb and having some problems with basic tasks.

  • 0

I am converting from mysqli to mongodb and having some problems with basic tasks.
This is part of a login authentication process, if the email and password matches to a record set in the dB i would like to get the _id and create a session variable.

Heres what i have so far, all the searches i’ve done seems to have info on getting the _id from different steps and i couldn’t modify it for my situation.

Also, if my logic is too literal of a translation from mysqli, please i’m open for suggestions on best practices for noSql

$collection = $db->members;
$query = array("email" => $email, "password" => $newpass);
  //$cursor = $collection->findOne($query);  // was told this is better but need deeper understanding
$cursor = $collection->find($query); 
$result = $cursor->count();

if($result==1){
    $_SESSION['email'] = $email;

    //having problems here
    $id = $cursor->findOne(array("_id" => $query['_id']));
    $_SESSION['id'] = $id;

    return true; 
}else 
    return false;

QUESTION how do i get the _id after authenticating a unique username and password?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T07:25:17+00:00Added an answer on June 10, 2026 at 7:25 am

    You should not run findOne on the cursor. The cursor points to the result from the previous query, you can use iterator_to_array to get an array with all the found records. Or you can use findOne() in the first place – it returns the actual row

    $collection = $db->members;
    $query = array("email" => $email, "password" => $newpass);
    $result = $collection->findOne($query);
    
    if(!empty($result)){
        $_SESSION['email'] = $email;
    
        //having problems here
        $id = "" . $result['_id']; // cast to string, as its a MongoId object
        $_SESSION['id'] = $id;
    
        return true; 
    }else 
        return false;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need some help in converting my script from using mysql to mysqli I
I'm having an issue converting from Fahrenheit to Celsius in Java. I just started
I have some objective-c code I'm converting from iPhone to iPad. CFGregorianDate is used
I am manually converting code from Java (1.6) to C# and finding some difficulty
I have some problem converting a MYSQL query to CI syntax. This my MySQL
I am having this trouble in converting the exiting time string into a new
Having trouble converting AM time values from MySQL fetch_array when using the PHP date()
I am in the process of converting our site from the PHP Mysql API
We're in the process of converting to SQL Server from MySQL . I'm trying
MySQL Gurus, I'm converting some reports from a MSSQL database for usage on a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.