MongoDB Query Guide: Methods for Pulling Data from Collections
🚀 What Will You Learn in This Guide?
In this guide, you will learn how to query data in MongoDB collections.
Basic querying, comparison operators ($gt, $in), multiple condition filtering, array and embedded document query techniques are explained with the find() method.
You will also see how to select specific areas (projection) to improve performance.
🧠 Technical Summary
Subject: MongoDB find() method and data extraction methods with different filtering conditions
Purpose: Accurately pull only the desired data in large collections
Steps:
- Connecting to MongoDB shell
- Creating a sample data collection
- Single field queries ($eq, $gt, $in, $ne)
- Multiple condition queries ($and, $or)
- Array query ($all)
- Field access (dot notation) in embedded documents
- Area limitation (projection)
⚙️ 1. Preparing the Sample Dataset
Connect to the MongoDB shell with the admin user:
mongo -u genixnode_yonetici -p --authenticationDatabase admin
➡️ Establishes an authenticated connection to MongoDB.
Then create the collection named Highlights and add the sample documents:
db.zirveler.insertMany([
{ "isim": "Everest", "yukseklik": 8848, "lokasyon": ["Nepal", "Çin"], "tirmanislar": { "ilk": { "yil": 1953 }, "toplam": 5656 } },
{ "isim": "K2", "yukseklik": 8611, "lokasyon": ["Pakistan", "Çin"], "tirmanislar": { "ilk": { "yil": 1954 }, "toplam": 306 } },
{ "isim": "Kançencunga", "yukseklik": 8586, "lokasyon": ["Nepal", "Hindistan"], "tirmanislar": { "ilk": { "yil": 1955 }, "toplam": 283 } },
{ "isim": "Lhotse", "yukseklik": 8516, "lokasyon": ["Nepal", "Çin"], "tirmanislar": { "ilk": { "yil": 1956 }, "toplam": 461 } },
{ "isim": "Makalu", "yukseklik": 8485, "lokasyon": ["Çin", "Nepal"], "tirmanislar": { "ilk": { "yil": 1955 }, "toplam": 361 } }
])
➡️ Creates a sample data collection containing five mountain peaks.
🔍 2. Querying Individual Fields
🟢 Equality
db.zirveler.find({ "isim": "Everest" })
➡️ Returns the document whose namespace is “Everest”.
🟠 Not Equal ($ne)
db.zirveler.find({ "isim": { $ne: "Everest" } })
➡️ Lists all peaks except Everest.
🔵 Included ($in)
db.zirveler.find({ "isim": { $in: ["Everest", "K2"] } })
➡️ Returns documents named “Everest” or “K2”.
🔺 Greater than ($gt)
db.zirveler.find({ "yukseklik": { $gt: 8500 } })
➡️ It brings peaks with an altitude of more than 8500 meters.
⚡ 3. Filtering with Multiple Conditions
AND (implicit)
db.zirveler.find({ "isim": "Everest", "yukseklik": 8848 })
➡️ Returns documents that satisfy both conditions.
OR (on)
db.zirveler.find({ $or: [{ "isim": "Everest" }, { "isim": "K2" }] })
➡️ Returns documents named Everest or K2.
🧩 4. Querying Array Fields
Single valued query:
db.zirveler.find({ "lokasyon": "Nepal" })
➡️ Returns documents containing “Nepal” in the location string.
Multivalued query ($all):
db.zirveler.find({ "lokasyon": { $all: ["Çin", "Nepal"] } })
➡️ Both China and Nepal bring passing documents.
🧱 5. Querying Nested Documents
Single-tier access:
db.zirveler.find({ "tirmanislar.toplam": { $gt: 1000 } })
➡️ The total number of climbs brings documents greater than 1000.
Multi-tiered access:
db.zirveler.find({ "tirmanislar.ilk.yil": { $lt: 1960 } })
➡️ It brings the peaks that were first climbed before 1960.
🧾 6. Rotating a Subset of Fields (Projection)
Including specific fields:
db.zirveler.find({}, { "isim": 1 })
➡️ Returns only the name and _id fields.
Exclude certain areas:
db.zirveler.find({}, { "tirmanislar": 0, "lokasyon": 0 })
➡️ Returns all fields except the specified fields.
Rule: You cannot use both 1 (include) and 0 (exclude) at the same time in the projection document. Only the _id field can be excluded.
💬 Frequently Asked Questions (FAQ)
- Does querying the entire collection impact performance?
Yes, using db.collection.find() can slow down the system, especially on large data. Filtering is a must.
- What is the difference between find() and findOne()?
find() returns a cursor; findOne() returns only one document.
- Can I check specific order in array?
Yes. location.0: You can select documents whose first element is “Nepal” by typing “Nepal”.
- What is a cursor?
It is the result set returned by the find() method. It can be managed with methods such as limit() and sort().
- Why does the _id field always appear?
MongoDB automatically adds the _id key to each document; It can be hidden with _id: 0 if desired.
🧭 Result
In this guide, you learned how to filter documents, query embedded fields, and limit fields using MongoDB's find() method and operators ($gt, $in, $and, $or, $all).
You can immediately try your MongoDB infrastructure on GenixNode Virtual Servers to optimize your application's data access and increase performance.

