Master MongoDB: From Basics to Advanced Queries and Aggregations
This comprehensive guide walks you through MongoDB fundamentals, connection strings, creating databases and collections, inserting, updating, deleting, querying documents with AND/OR logic, advanced range and regex queries, and powerful aggregation pipelines including sorting and grouping.
Preface
MongoDBis a representative NoSQL database, a high‑performance key‑value store often used for crawler storage.
It is built in C++, provides scalable high‑performance data storage for web applications, and combines features of relational databases.
Connection
Format:
mongodb://username:password@host:port/database mongodb://: fixed prefix, required. username:password: authentication; password may be omitted. host: required server address. port: defaults to 27017 if omitted. database: defaults to test if omitted.
Creating Database and Collection
Create a database
> use underground_station
switched to db underground_stationIf the database exists, the command switches to it; otherwise it creates it.
List databases
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GBIn MongoDB a database appears in the list only after it contains data.
Create a collection
> db.createCollection("Quotations")
{ "ok" : 1 }The collection is created successfully.
Show collections
> show collections
QuotationsInsert Documents
MongoDB stores data as documents; a collection is analogous to a table.
Insert a single document:
> use underground_station
switched to db underground_station
> db.Quotations.insert({
name: 'Jia Gui',
description: 'I owe enough meals for two years',
createDate: '2021-04-15'
})
WriteResult({ "nInserted" : 1 })Insert multiple documents:
> db.Quotations.insertMany([
{name:'Gold Mark', description:'This weight is also boiled', createDate:'2021-04-15'},
{name:'Jia Gui', description:'I fought him with my head and face', createDate:'2021-04-16'},
{name:'Hei Teng', description:'Who am I? I am Hei Teng', createDate:'2021-04-16'},
{name:'Sun Youfu', description:'No water, no fish; no you, no donkey', createDate:'2021-04-17'}
])Update Documents
Update a single document:
> db.Quotations.update({name:'Hei Teng'}, {$set:{name:'Hei Teng', description:'All traitors are stupid', createDate:'2021-04-16'}})Update multiple documents (multi flag):
> db.Quotations.update({name:'Jia Gui'}, {$set:{description:'I don\'t know, I just know she looks great', createDate:'2021-04-16'}}, {multi:true})Upsert (insert if not exists):
> db.Quotations.update({name:'Sae River'}, {$set:{name:'Sae River', description:'Go east, cause trouble'}}, {upsert:true})Delete Documents
Delete all documents matching a condition:
> db.Quotations.remove({name:'Jia Gui'})
WriteResult({ "nRemoved" : 2 })Delete all documents in a collection:
> db.Quotations.remove({})Query Documents
General find syntax: db.collection.find(query, projection) Example: exclude _id field: > db.Quotations.find({}, {'_id':0}) AND query (implicit):
> db.Quotations.find({name:'Gold Mark', createDate:'2021-04-26'})OR query (explicit):
> db.Quotations.find({$or:[{name:'Jia Gui'},{description:'All traitors are stupid'}]})Combined AND/OR:
> db.Quotations.find({createDate:'2021-04-18', $or:[{name:'Jia Gui'},{description:'All traitors are stupid'}]})Advanced Queries
Range operators: $lt, $gt, $lte, $gte, $ne, $in, $nin.
Example: documents with createDate greater than 2021-04-17:
> db.Quotations.find({createDate: {$gt: '2021-04-17'}})Regular expression query:
> db.Quotations.find({name: {$regex: 'Gold'}})Aggregation
Sorting
> db.Quotations.find({}, {_id:0}).sort({createDate:-1})Grouping
Group by name and sum a numeric field forceValue:
> db.Quotations.aggregate([
{$match:{createDate: {$gt:'2021-04-07'}}},
{$group:{_id:"$name", forceValue: {$sum:"$forceValue"}}}
])Similar examples for $avg, multi‑field grouping, and other aggregation operators are shown.
For more advanced usage, refer to the official MongoDB documentation.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Python Crawling & Data Mining
Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
