mongodb

db.collection.aggregate()    提供对聚合管道的访问。
db.collection.bulkWrite()    提供批量写入操作功能。
db.collection.copyTo()    已过时。包装eval在单个MongoDB实例中的集合之间复制数据。
db.collection.count()    Wraps count返回集合或视图中文档数的计数。
db.collection.countDocuments()    $group使用$sum 表达式包装聚合阶段以返回集合或视图中文档数的计数。
db.collection.estimatedDocumentCount()    count用于返回集合或视图中文档的近似计数的包装。
db.collection.createIndex()    在集合上构建索引。
db.collection.createIndexes()    在集合上构建一个或多个索引。
db.collection.dataSize()    返回集合的大小。包裹size输出中的字段collStats。
db.collection.deleteOne()    删除集合中的单个文档。
db.collection.deleteMany()    删除集合中的多个文档。
db.collection.distinct()    返回具有指定字段的不同值的文档数组。
db.collection.drop()    从数据库中删除指定的集合。
db.collection.dropIndex()    删除集合上的指定索引。
db.collection.dropIndexes()    删除集合上的所有索引。
db.collection.ensureIndex()    已过时。使用db.collection.createIndex()。
db.collection.explain()    返回有关各种方法的查询执行的信息。
db.collection.find()    对集合或视图执行查询并返回游标对象。
db.collection.findAndModify()    以原子方式修改并返回单个文档。
db.collection.findOne()    执行查询并返回单个文档。
db.collection.findOneAndDelete()    查找单个文档并将其删除。
db.collection.findOneAndReplace()    查找单个文档并替换它。
db.collection.findOneAndUpdate()    查找单个文档并进行更新。
db.collection.getIndexes()    返回描述集合上现有索引的文档数组。
db.collection.getShardDistribution()    对于分片群集中的集合,db.collection.getShardDistribution()报告块分布的数据。
db.collection.getShardVersion()    分片群集的内部诊断方法。
db.collection.group()    已过时。提供简单的数据聚合功能。通过键对集合中的文档进行分组,并处理结果。使用aggregate()了更复杂的数据集合。
db.collection.insert()    在集合中创建新文档。
db.collection.insertOne()    在集合中插入新文档。
db.collection.insertMany()    在集合中插入几个新文档。
db.collection.isCapped()    报告集合是否为上限集合。
db.collection.latencyStats()    返回集合的延迟统计信息。
db.collection.mapReduce()    执行map-reduce样式数据聚合。
db.collection.reIndex()    重建集合上的所有现有索引。
db.collection.remove()    从集合中删除文档。
db.collection.renameCollection()    更改集合的名称。
db.collection.replaceOne()    替换集合中的单个文档。
db.collection.save()    提供围绕insert()和update()插入新文档的包装器。
db.collection.stats()    报告集合的状态。提供围绕的包装collStats。
db.collection.storageSize()    报告集合使用的总大小(以字节为单位)。提供输出storageSize字段周围的包装器collStats。
db.collection.totalIndexSize()    报告集合上索引使用的总大小。提供输出totalIndexSize字段周围的包装器collStats。
db.collection.totalSize()    报告集合的总大小,包括所有文档的大小和集合上的所有索引。
db.collection.update()    修改集合中的文档。
db.collection.updateOne()    修改集合中的单个文档。
db.collection.updateMany()    修改集合中的多个文档。
db.collection.watch()    在集合上建立变更流。
db.collection.validate()    对集合执行诊断操作。

二、常用操作
1.基本状态查看:
db.getCollection('集合名').stats()  

此方法为查看文档的一些统计信息。

2.常规查询:
db.getCollection('集合名').find({'字段名':'字段属性'})

3.查找某个字段不存在的文档:
db.getCollection('集合名').find({'字段名':{$exists:false}})

4.多字段查询:
db.getCollection('集合名').find({'字段1':{$exists:false},'字段2':{$exists:true}}).count()

5.嵌套字段的操作:
例如:字段name是嵌套在people下的字段,即name是people的子字段。

查找所有name为“lucy”的文档,则在people和name之间加点"."表示。

db.getCollection('集合名').find({'people.name':‘lucy’})

6.查找大于(大于,小于,等方法)某个值得文档
db.getCollection('集合名').find({'字段名':{'$gt':数值}})

$gt:大于;    $lt:小于;    $gte:大于或等于;    $lte:小于或等于; $ne: 不等于

注:使用不等于时,"$ne"后面可以跟非数值型的数据,例如str类型。

例如 查询字段name存在且不为空字符串:db.getCollection("集合名").find({"name":{"$exists":true, "$ne":""}})

7.删除指定字段:
db.getCollection('集合名').update({'字段名':{$exists:true}}, {$unset:{'字段名':''}}, {multi:true})

参数multi设置为true表示对集合中的所有文档执行该命令,若设置为false则只修改找到的第一条文档。

8.删除满足某条件的文档:
db.getCollection('集合名').remove({'字段名':'条件'})

此处的“条件”同find命令的查询条件。

例如:db.getCollection('API').remove({'created':{'$gt':154, '$lt':156}}), 为删除集合“API”中‘created’字段属性在154-156之间的数据。

9.update更新字段属性:
db.getCollection('集合名').update({'字段名':‘原属性’},{'$set':{'字段名':‘目标属性’}},{multi:true})

10.按照指定排序输出显示:
db.getCollection('集合名').find().sort({"字段名":-1}) 

其中 1 为升序排列,而 -1 是用于降序排列

11.只输出显示某个字段:
db.getCollection('集合名').find({}, {'要显示的字段':1})

12.查看集合索引:
db.getCollection('集合名').getIndexes()

13.使用正则匹配查询某个字段中含有“某部分”内容的文档(部分匹配):
db.getCollection('集合名').find({post_text:{$regex:"runoob"}})

按键盘F5或者Ctrl+Enter(回车)执行操作。

1.简单查询

db.getCollection('集合名').find({
    'sfcz':'1'
    })

2.count() 查询

db.getCollection('集合名').find({
    'deleted':0,
    'sfcz':'1'
    }).count()

  • 9
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值