![]() |
MongoDB is a popular choice for developers working with large volumes of data, due to its flexibility and powerful querying capabilities. One common task developers face is finding documents in a collection where an array field contains a specific value. In this beginner-friendly article, we’ll explore simple yet effective methods to achieve this using MongoDB. How to Find a Document with Array that Contains Specific Value in MongoDB?In MongoDB, the find() method selects the documents in a collection and returns the cursor to the selected documents. A cursor means pointer and how it is pointing to a document, when we use the find() method it returns a pointer on the selected documents and returns one by one. These find() methods can have the ability to access single or multiple documents in the collection. The syntax is as follows: db.Collection_name.find(selection, projection,options)
Explanation:
Examples: Find Document with Array that Contains a Specific Value in MongoDBExample 1Let’s Create a MongoDB collection named // Inserting sample documents into the collection
db.Specificvalue.insertMany( [
{ "StudentId":1,
"StudentName":"Larry",
"FavouriteSubject":["C","C++","Java"]
} ,
{ "StudentId":2,
"StudentName":"Larry",
"FavouriteSubject":["MongoDB","MySQL","SQL Server"]
}
] );
// Display all documents from the collection
db.Specificvalue.find();
// Finding specific value
db.Specificvalue.find({FavouriteSubject: "MongoDB"});
Output: ![]() Output Explanation: In the above output, we finded a document based on specific value is equal FavouriteSubject: “MongoDB” by using the find() function. Example 2Let’s Create a MongoDB collection named // Inserting sample documents into the collection
db.games.insertMany([
{
player: 1,
name: "Rahul",
game: "kabaddi",
team:"Telugu titans",
age: 26,
league: "PKL",
guests: ["hero", "heroine"]
},
{
player: 2,
name: "dhoni",
game: "Cricket",
team: "csk",
age: 43,
league: "IPL",
guests: ["cm", "pm"]
}
])
// displaying all the documents
db.games.find()
//displaying specific value
db.games.find({team:"csk"})
Output: ![]() Output Explanation: In the below output, we finded a document based on specific value which is equal to team:”csk” by using the find() function. Example 3Let’s Insert the provided student details into a MongoDB collection named // Inserting sample documents into the collection
db.student_details.insertMany([
{
student: 1,
Name: "Bhaskar",
age: 20,
sex: "Male",
id_no: 102,
college_name: "Sri sai"
},
{
student: 2,
Name: "Bhanu priya",
age: 19,
sex: "Female",
id_no: 103,
college_name: "vishnu"
},
{
student: 3,
Name: "Gopi",
age: 20,
sex: "Male",
id_no: 104,
college_name: "Raghu"
}
]);
// Displaying all the documents
db.student_details.find();
// Displaying specific value
db.student_details.find({ age: 20 });
Output: ![]() Output Explanation: In the above output, we finded a document based on specific value is equal to “age:20” by using the find() function. ConclusionOverall, Finding documents in a collection where an array field contains a specific value is a common requirement.By using the The |
Reffered: https://www.geeksforgeeks.org
Databases |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 14 |