findOne
noun
say it: “fynd-WUN” /ˌfaɪndˈwʌn/
A MongoDB method that returns the first document matching a filter, or null when there is none.
What does findOne do?
findOne gives back the first document that matches a filter, as the document itself rather than a list. When nothing matches, it gives back null, so it's the way to fetch one record, such as one recipe by its name.
db.recipes.findOne({ name: "Paella" });
// { _id: 2, name: 'Paella', cuisine: 'Spanish', minutes: 60 }
db.recipes.findOne({ name: "Moon Cheese" });
// null
In a sentence
- “The recipe page calls findOne with the recipe's id and shows whatever comes back.”
- “When findOne returned null, the API answered 404 Not Found.”
Level: Apprentice · More MongoDB terms at this level
Updated