How can I find a document in MongoDB by its id?

How can I find a document in MongoDB by its id?

MongoDB is a document-oriented database system that stores and manages data in the form of documents. Each document is assigned a unique identifier called the “id”. To find a specific document in MongoDB, you can use the “find” command along with the document’s id. This will retrieve the document with the matching id from the database. The id can be obtained from the document itself or from the “_id” field in the database. This method allows for efficient and precise retrieval of data from MongoDB.

MongoDB: Find Document By id


You can use the following basic syntax to find a document by id in MongoDB:

db.collection.find(ObjectId('619527e467d6742f66749b72'))

The following examples show how to use this syntax with a collection teams with the following documents:

{ _id: ObjectId("619527e467d6742f66749b70"),
  team: 'Rockets',
  position: 'Center',
  points: 19 }

{ _id: ObjectId("619527e467d6742f66749b71"),
  team: 'Rockets',
  position: 'Forward',
  points: 26 }

{ _id: ObjectId("619527e467d6742f66749b72"),
  team: 'Cavs',
  position: 'Guard',
  points: 33 }

Example: Find Document by id

We can use the following code to find the document with a specific id in the teams collection:

db.teams.find(ObjectId('619527e467d6742f66749b72'))

This query returns the following document:

{ _id: ObjectId("619527e467d6742f66749b72"),
  team: 'Cavs',
  position: 'Guard',
  points: 33 }

We can change the id to find another document with a different id in the teams collection:

db.teams.find(ObjectId('619527e467d6742f66749b71'))

This query returns the following document:

{ _id: ObjectId("619527e467d6742f66749b71"),
  team: 'Rockets',
  position: 'Forward',
  points: 26 }

Note that if you query for a certain document with an id that doesn’t exist, no results will be returned.

Additional Resources

The following tutorials explain how to perform other common operations in MongoDB:

Cite this article

stats writer (2024). How can I find a document in MongoDB by its id?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-find-a-document-in-mongodb-by-its-id/

stats writer. "How can I find a document in MongoDB by its id?." PSYCHOLOGICAL SCALES, 2 Jul. 2024, https://scales.arabpsychology.com/stats/how-can-i-find-a-document-in-mongodb-by-its-id/.

stats writer. "How can I find a document in MongoDB by its id?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-find-a-document-in-mongodb-by-its-id/.

stats writer (2024) 'How can I find a document in MongoDB by its id?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-find-a-document-in-mongodb-by-its-id/.

[1] stats writer, "How can I find a document in MongoDB by its id?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, July, 2024.

stats writer. How can I find a document in MongoDB by its id?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top