DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Data Engineering
  3. Databases
  4. Finding Mongo Document IDs Using the PHP Library

Finding Mongo Document IDs Using the PHP Library

This quick how-to shows how to scour MongoDB to find documents. This method uses the MongoDB PHP library to make it happen.

Lorna Mitchell user avatar by
Lorna Mitchell
·
Jun. 12, 16 · Tutorial
Like (2)
Save
Tweet
Share
5.72K Views

Join the DZone community and get the full member experience.

Join For Free

My new job as a Developer Advocate with IBM means I get to play with databases for a living (this is the most awesome thing ever invented, seriously). On my travels, I spent some time with MongoDB, which is a document database — but I ran into an issue with fetching a record by ID. So, here's the code I eventually arrived at so I can refer to it later, and if anyone else needs it, hopefully they will find it too.

MongoDB and IDs

When I inserted the data to the collection, I did not set the _id field; if this is empty, MongoDB will just generate an ID and use that, which is fine by me. My issues arose when I wanted to then fetch that data using that generated identifier.

If I inspect my data by using db.posts.find() (my collection is called posts), then the data looks like this:

{ "_id" : ObjectId("575038831661d710f04111c1"), ...

So if I want to fetch by ID, I need to include that ObjectId function call around the ID.

Using the PHP Library

When I came to do this with PHP, I couldn't find an example of using the new MongoDB PHP Library that used the ID in this way (but it's a good library, use it). Older versions of this library used a class called MongoID, and I knew that wasn't what I wanted — but had I checked the docs for that, and I'd have found that they have been updated to point to the new equivalent!  So this is also very useful to know if you can only find older code examples.

To pass an ID to MongoDB using the PHP Library, you will need to construct a MongoDB\BSON\ObjectID. My example was blog posts and to fetch a record by its ID, I used:

$post = $posts->findOne(["_id" => new MongoDB\BSON\ObjectID($id)]);

Later, I updated the record — the blog post included nested comments in the record, so to add an array to the comments collection of a record whose _id I knew, I used this code:

$result = $posts->updateOne(
 ["_id" => new MongoDB\BSON\ObjectID($id)],
 ['$push' => [
 "comments" => $new_comment_data
 ]
 ]
 );

Hopefully, this gives you a pointer on using the generated IDs in MongoDB from the PHP library and saves you at least as much time as I lost trying to figure this out!

Related Refcard: MongoDB

PHP Library code style Database

Published at DZone with permission of Lorna Mitchell, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How to Submit a Post to DZone
  • API Design Patterns Review
  • Microservices Discovery With Eureka
  • 7 Awesome Libraries for Java Unit and Integration Testing

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: