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 8165997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T19:42:31+00:00 2026-06-06T19:42:31+00:00

I have a PHP app which can hook into multiple backends (currently MySQL or

  • 0

I have a PHP app which can hook into multiple backends (currently MySQL or XML) and which I’m trying to make work with MongoDB as well. One seemingly minor issue that I’m distinctly struggling with is that Mongo mandates ‘_id’ is the name of the primary key. The backends are generally fairly well abstracted by the application, but the application does need to work with the id on a fairly regular basis, so I access it with code like $result['id'], which up until now has abstracted well.

But now I’m faced with trying to (efficiently!) handle a DB that won’t let me use ‘id’ as its primary key, and I’m not sure what the best option is. Here’s what I’ve thought of so far:

  1. Just leave Mongo’s ‘_id’ value alone, and set an application variable $id to "id" or "_id" depending on the backend. Applications should use $id to access the ID field, rather than the hard coded "id". Note that all other fields are accessed by their string names, such as $result['user'] and so this would be breaking with norms in the application.

    Pros: allows MongoCursor objects to be returned directly when possible, ensuring minimal memory usage and fast access to the data.

    Cons: not backwards-compatible, would require (a fairly tedious) refactoring of any code using this application.

  2. Wrap the returned MongoCursor objects in a new iterator class that returns each item with "_id" mapped to "id" appropriately. The application would map inbound calls to "id" back to "_id" when communicating with Mongo.

    Pros: Retains most of the memory efficiency of 1. while preventing most backwards-compatibility issues.

    Cons: Not totally sure how to implement such an object, not convinced it could be done cleanly, or that it would actually work as well as I’m imagining. To do it right, I’d want to implement similar iterator wrappers for the other backends as well.

  3. Load results into memory with iterator_to_array() as described in MongoCollection.find(), do appropriate transforms, and return the array.

    Pros: conceptually simpler than 2., would work well with the rest of my app.

    Cons: obviously a poor choice in terms of memory. Not the end of the world given the app, but still not ideal.

Do any of these options stand out as particular reasonable and robust solutions to this issue? Can anyone suggest other alternatives for handling primary keys in a backend-agnostic way? Additional backends are a possibility in the future, so issues or benefits of a given method related to other data storage systems are also welcome.

I’m currently leaning towards 2., but I welcome your thoughts.

  • 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-06T19:42:35+00:00Added an answer on June 6, 2026 at 7:42 pm

    I ended up going with option 2, wrapping the iterator and hiding the existing ‘_id’ field. Not clean, and not elegant, but the best I could do with what I had. Working on this really made me pine for Python.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an existing large(ish) PHP web app (using Apache and MySQL) which now
I have a web app which I want to work internationally. I can ask
I have a PHP app that creates a CSV file which is forced to
Iam making an app which has one index.php file. I also have these other
Here's the scenario I anticipate: I have an app written in PHP which has
I have an app which stores some data in .php files in /library/ directory,
Currently working on a pretty basic hosted PHP app which I'm in the process
I have a PHP app which takes a user-inputted $imageurl and does the following:
I have a web app which is currently not rendering in IE. For the
What I need to have happen: PHP needs to launch an server app which

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.