Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> MongoDB
MongoDB
  1. Query Document ที่ Array ไม่มี Field Value ที่ระบุใน MongoDB

    ใช้ $nin สำหรับ Array ที่ไม่มีค่าที่ระบุ Operator $nin (Not In) ใช้หา Document ที่ Array Field ไม่มี Element ที่มีค่าตรงกับที่กำหนด สร้างข้อมูลตัวอย่าง > db.documentWithAParticularFieldValueDemo.insertOne({ StudentId: 101, StudentDetails: [ { TheoryMarks: 78, PracticalMarks: 91 }, {

  2. คำสั่งตรงข้าม $addToSet คือ $pull ใน MongoDB

    ลบค่าออกจาก Array ด้วย $pull (ตรงข้าม $addToSet) $addToSet เพิ่มค่าเข้า Array ถ้ายังไม่มี | $pull ลบค่าที่ match ออกจาก Array สร้างข้อมูลตัวอย่าง > db.oppositeAddToSetDemo.insertOne({StudentName:John,StudentHobby:[Cricket,Cooking,Drawing]}); { acknowledged : true, insertedId : ObjectId(5c8eddcc2

  3. หาผลรวมค่าของ Field ทุก Document ใน MongoDB Collection

    หาผลรวม Field แบบรวมทุก Document ด้วย Aggregation สร้างข้อมูลตัวอย่าง > db.sumOfValueDemo.insertOne({Name:Larry,Amount:14.50}); { acknowledged : true, insertedId : ObjectId(5c8ee7272f684a30fbdfd592) } > db.sumOfValueDemo.insertOne({Name:Mike,Amount:15.68}); { acknowledged : true, insertedId :

  4. ลบข้อมูลทั้งหมดใน Collection MongoDB Shell

    ลบ Document ทั้งหมดใน Collection ด้วย remove({}) db.yourCollectionName.remove({}); สร้างข้อมูลตัวอย่าง > db.deleteAllRecordsDemo.insertOne({StudentName:John}); { acknowledged : true, insertedId : ObjectId(5c8f6ca32f684a30fbdfd596) } > db.deleteAllRecordsDemo.insertOne({StudentName:Carol,Stude

  5. วิธีแสดงรายการ User ทั้งหมดใน Mongo Shell

    แสดง User ทั้งหมด 2 วิธี วิธีที่ 1: ใช้ getUsers() db.getUsers(); วิธีที่ 2: ใช้ show users show users; ตัวอย่างผลลัพธ์ getUsers() [ { _id : test.John, user : John, db : test, roles : [ { role : readWrite, db : test }, { role : dbAdmin, db : test } ], mechanisms : [ SCRAM-SHA-1, SCRAM-SHA-256 ]

  6. วิธีแสดง Index ของ Collection ใน MongoDB

    แสดง Index ทั้งหมดของ Collection ด้วย getIndexes() db.yourCollectionName.getIndexes(); สร้างข้อมูลและ Index ตัวอย่าง > db.indexDemo.insertOne({StudentName:Larry,StudentAge:21}); { acknowledged : true, insertedId : ObjectId(5c8f7c4f2f684a30fbdfd599) } > db.indexDemo.insertOne({StudentName:Mike

  7. ตรวจสอบจำนวน Connection ปัจจุบันใน MongoDB

    เช็ค Connection ปัจจุบันด้วย serverStatus() วิธีที่ 1: เก็บใน Variable var anyVariableName = db.serverStatus(); yourVariableName.connections; วิธีที่ 2: เรียกใช้โดยตรง db.serverStatus().connections; ตัวอย่างผลลัพธ์ { current : 1, available : 999999, totalCreated : 1 } current: จำนวน Connection

  8. MongoDB Aggregation $sum เพื่อหาผลรวมและนับจำนวน

    ใช้ $sum ใน Aggregation เพื่อนับจำนวนและหาผลรวม สร้างข้อมูลตัวอย่าง > db.aggregateSumDemo.insertOne({CustomerName:Larry,Amount:140}); { acknowledged : true, insertedId : ObjectId(5c8baa0680f10143d8431e18) } > db.aggregateSumDemo.insertOne({CustomerName:Mike,Amount:160}); { acknowledged : true

  9. วิธีเปลี่ยนชื่อ Collection ใน MongoDB

    เปลี่ยนชื่อ Collection ด้วย renameCollection() ใช้ Method renameCollection() เพื่อเปลี่ยนชื่อ Collection db.yourOldCollectionName.renameCollection(yourNewCollectionName); ตัวอย่างการเปลี่ยนชื่อ > use sample; switched to db sample > show collections; copyThisCollectionToSampleDatabaseDemo dele

  10. วิธีหา Document ที่มี _id สูงสุดใน MongoDB

    หา Document ที่มี _id มากที่สุด (ล่าสุด) ใช้ sort({_id: -1}) เรียงจากมากไปน้อย แล้ว limit(1) เอาแค่ตัวแรก db.yourCollectionName.find().sort({_id:-1}).limit(1).pretty() สร้างข้อมูลตัวอย่าง > db.getElementWithMaxIdDemo.insertOne({Name:John,Age:21}); { acknowledged : true, insertedId : ObjectId(5c8

  11. วิธี Clone Collection ใน MongoDB

    Clone Collection ด้วย forEach() และ insert() ใช้ Method forEach() วนลูปผ่าน Document ทุกตัว แล้ว Insert เข้า Collection ใหม่ สร้าง Collection ต้นทาง > db.studentInformation.insertOne({StudentName:Chris}); { acknowledged : true, insertedId : ObjectId(5c8bc15780f10143d8431e21) } > db.studentInf

  12. การเขียนข้อความลง Console ใน MongoDB JavaScript Execution

    พิมพ์ข้อความและ Object ลง Console ใน MongoDB Shell 1. print() - พิมพ์ข้อความ String print(yourString); 2. printjson() - พิมพ์ Object แบบ JSON printjson(yourObjectName); ตัวอย่างการใช้งาน > print(Welcome to MongoDB Console); Welcome to MongoDB Console สร้าง Object และพิมพ์ด้วย printjson() >

  13. การค้นหาแบบ Case Insensitive ใน MongoDB

    Case Insensitive Search ด้วย $regex ใช้ Operator $regex พร้อม Regular Expression เพื่อค้นหาแบบไม่สนใจตัวพิมพ์เล็ก/ใหญ่ รูปแบบที่ 1: ใช้ ^pattern$ แบบ Exact Match db.yourCollectionName.find({ yourFieldName : { $regex: ^yourValue$ } }); รูปแบบที่ 2: ใช้ Flag i สำหรับ Case Insensitive db.yourCollecti

  14. แก้ไข Error 'multi update only works with $ operators' ใน MongoDB

    แก้ไขข้อผิดพลาด Multi Update ต้องใช้ $ Operators ข้อผิดพลาด multi update only works with $ operators เกิดขึ้นเมื่อ Update หลาย Document โดยไม่ใช้ Update Operator (เช่น $set, $inc, $push) รูปแบบที่ถูกต้อง: ใช้ $set db.yourCollectionName.update({ }, { $set: { yourFieldName: yourValue } }, false, true

  15. MongoDB count() ช้า แก้ไขอย่างไร

    เพิ่มประสิทธิภาพ count() ด้วย Index ใช้ ensureIndex() (หรือ createIndex() ในเวอร์ชันใหม่) สร้าง Index บน Field ที่ Query บ่อย เพื่อเพิ่มความเร็วของ count() สร้างข้อมูลตัวอย่าง > db.countPerformanceDemo.insertOne({StudentName:John,StudentCountryName:US}); { acknowledged : true, insertedId : Objec

  16. Collection.find() ใน MongoDB คืนค่า Field ทั้งหมดเสมอหรือไม่

    ควบคุม Field ที่คืนค่าใน find() ด้วย Projection คำสั่ง find() สามารถระบุ Field ที่ต้องการคืนค่าได้โดยใช้ Projection (พารามิเตอร์ที่ 2) กรณีที่ 1: คืนค่าเฉพาะ Field ที่ระบุ (ตั้งค่าเป็น 1) db.yourCollectionName.find({},{ yourFieldName: 1 }).pretty(); กรณีที่ 2: คืนค่าทุก Field ยกเว้น Field ที่ระบุ

  17. Query Document ที่ Field เป็น Null หรือไม่มี Field ใน MongoDB

    กรณี 2 กรณีของ Field ที่เป็น Null หรือไม่มี กรณีที่ 1: Field มีอยู่แต่เป็นค่า Null db.yourCollectionName.count({yourFieldName: null}); กรณีที่ 2: Field ไม่มีอยู่จริง (Not Exist) db.yourCollectionName.count({yourFieldName: {$exists: false}}); สร้างข้อมูลตัวอย่าง > db.fieldIsNullOrNotSetDemo.ins

  18. วิธีลบ Element ใน Array ตาม Index ใน MongoDB

    ลบ Array Element ตาม Index แบบ 2 ขั้นตอน MongoDB ไม่มีคำสั่งลบ Array Element ตาม Index โดยตรง ต้องทำ 2 ขั้นตอน: ขั้นตอนที่ 1: ตั้งค่า Element ที่ Index นั้นเป็น null db.yourCollectionName.update({}, {$unset : {yourArrayFieldName.yourIndexValue : 1 }}); ขั้นตอนที่ 2: ลบค่า null ออกจาก Array ด้วย $p

  19. MongoDB Query เปรียบเทียบค่าของ 2 Field

    Query เปรียบเทียบค่าระหว่าง 2 Field ด้วย $where ใช้ Operator $where พร้อม JavaScript Function เพื่อเปรียบเทียบค่าของ 2 Field ใน Document เดียวกัน db.yourCollectionName.find( { $where: function() { return this.yourFirstFieldName < this.yourSecondFieldName } } ).pretty(); สร้างข้อมูลตัวอย่าง > db.

Total 1351 -คอมพิวเตอร์  FirstPage PreviousPage NextPage LastPage CurrentPage:63/68  20-คอมพิวเตอร์/Page Goto:1 57 58 59 60 61 62 63 64 65 66 67 68