[ MongoDB ] ฝึกเขียน MongoDB | Part.2
1 min readJan 25, 2021
ฮายย 🎈🎈 เราจะมาต่อ Part.2 กันนะครับ สำหรับ Part.1 ลิ้งค์ตามด้านล่างเลยยย 👇👇
[ MongoDB ] ฝึกเขียน MongoDB | Part.1 | by Siriwat J. | Jan, 2021 | Medium
Mongo Shell (ต่อ)
[ การค้นหาข้อมูล ]
- db.<ชื่อ collection>.find()
แสดงข้อมูลทั้งหมดใน collection เช่น
> db.users.find(){ "_id" : ObjectId("600f457f5d5dddaaa74e26af"), "username" : "aom", "password" : "1234" }
{ "_id" : ObjectId("600f469d5d5dddaaa74e26b0"), "username" : "test", "password" : "5555" }
{ "_id" : ObjectId("600f47785d5dddaaa74e26b2"), "username" : "test1", "password" : "1234" }
{ "_id" : ObjectId("600f47785d5dddaaa74e26b3"), "username" : "test2", "password" : "1234" }
- db.<ชื่อ collection>.find().help()
แสดง command เกี่ยวกับการค้นหา เช่น
> db.users.find().help()find(<predicate>, <projection>) modifiers
.sort({...})
.limit(<n>)
.skip(<n>)
.batchSize(<n>) - sets the number of docs to return per getMore
.collation({...})
.hint({...})
.readConcern(<level>)
.readPref(<mode>, <tagset>)
.count(<applySkipLimit>) - total # of objects matching query. by default ignores skip,limit
.size() - total # of objects cursor would return, honors skip,limit
.explain(<verbosity>) - accepted verbosities are {'queryPlanner', 'executionStats', 'allPlansExecution'}
.min({...})
.max({...})
.maxTimeMS(<n>)
.comment(<comment>)
.tailable(<isAwaitData>)
.noCursorTimeout()
.allowPartialResults()
.returnKey()
.showRecordId() - adds a $recordId field to each returned object
.allowDiskUse() - allow using disk in completing the queryCursor methods
.toArray() - iterates through docs and returns an array of the results
.forEach(<func>)
.map(<func>)
.hasNext()
.next()
.close()
.objsLeftInBatch() - returns count of docs left in current batch (when exhausted, a new getMore will be issued)
.itcount() - iterates through documents and counts them
.pretty() - pretty print each document, possibly over multiple lines
- db.<ชื่อ collection>.find().pretty()
แสดงข้อมูลให้สวยงาม
- db.<ชื่อ collection>.find().toArray()
แสดงข้อมูลเป็น Arrays
- db.<ชื่อ collection>.find().count()
แสดงจำนวนข้อมูล
- db.<ชื่อ collection>.find( [query] )
ค้นหาข้อมูลตาม query
เช่น ต้องการค้นหา username = “aom”
> db.users.find({username : "aom"}){ "_id" : ObjectId("600f457f5d5dddaaa74e26af"), "username" : "aom", "password" : "1234" }
ไว้เจอกันใน Part 3 นะค้าบ 🎈🎈