MongoDB 集合(collection)常用方法 (Method)

collection 的官方 method文档

db.collection.aggregate() :
对集合进行聚合操作

db.collection.countDocuments(query, limit, skip, ...) :
返回满足条件的文档数

db.collection.createIndex(keys, background,unique,name,...) :
为集合创建新的索引

db.collection.dataSize() :返回集合的字节数

db.collection.deleteOne(query) :
删除满足条件的第一个文档

db.collection.deleteMany(query) :
删除满足条件的所有文档

db.collection.drop() :
删除该集合及相关的索引等文件

db.collection.dropIndex(index) :
删除指定的索引

db.collection.dropIndexes(index) :
删除指定的多个索引

db.collection.deleteOne(filter,...) :
删除满足条件的第一个文档

db.collection.deleteMany(filter,...) :
删除满足条件的所有文档

db.collection.explain() :
和其他方法一起使用,用于输出具体的执行过程

db.collection.find(query, projection) :

query :用于指定过滤条件; projection :用于投影字段;
  • 返回满足条件的所有文档
  • db.collection.findOne(query, projection) :
    返回满足条件的第一个文档

    db.collection.findAndModify(query,sort,remove,document,upsert,...) :
    upsert 设置为true时,当没有满足条件的文档时,文档将插入集合

    db.collection.getIndexes() :
    返回该集合的所有索引

    db.collection.insert(document) :
    db.collection.insertOne(document) :
    db.collection.insertMany(documents) :
    向集合中插入一个或多个文档

    db.collection.isCapped() :
    判断该集合是否是固定集合

    db.collection.reIndex() :
    删除该集合所有的索引,并重新创建(执行期间无法执行其他指令)

    db.collection.remove(query, justOne) :
    删除该集合中满足条件的文档

    db.collection.renameCollection(newName) :
    更改集合名称

    db.collection.replaceOne(filter, replacement,{upsert,...}) :
    替换符合条件的文档

    db.collection.save(document) :
    更新现有集合

    db.collection.stats({scale,indexDetails,...}) :
    返回该集合的统计信息

    db.collection.storageSize() :
    返回该集合文档的总储存空间

    db.collection.totalSize() :
    返回该集合文档和索引所占的总字节数

    db.collection.update(query, update, {upsert,multi,...}) :
    multi :设置为true,更新所有满足条件的文档,默认只更新第一个文档

    db.collection.updateOne(filter, update, options) :