SQL >> Database teknologi >  >> NoSQL >> MongoDB

MongoDB count-kommandoen forklaret

count-kommandoen tæller dokumenter i en collection eller et view.

Eksempel

db.runCommand({ count: "pets" })

Du kan tælle med et filter:

db.runCommand({ count: "pets", query: { type: "Dog" } })

Limit og skip

db.runCommand({ count: "pets", query: { type: "Dog" }, limit: 3 })
db.runCommand({ count: "pets", query: { type: "Dog" }, skip: 2 })

Kommandoen understøtter også felter som hint, comment, readConcern og collation.

MongoDB anbefaler nyere drivere at bruge countDocuments() og estimatedDocumentCount() i stedet.

  1. Tæl dokumenter med countDocuments() i MongoDB

  2. Brug dropIndexes-kommandoen til at fjerne MongoDB-indekser

  3. 7 måder at tælle dokumenter i MongoDB

  4. Tæl samlede resultater med MongoDBs aggregeringsframework