mongoDB query command

模糊查询
/common/ 包含common的值
/[bt]_common/ 包含b or t or bt 开头_common的值
/[^bt]_common/ 不包含bt开头_common的值
/_common$/ common结尾的值
/^common/ common开头的值

db.Accounts.find({scenarioTag:/common/})

查询某个字段值在一个范围内,只显示某些字段

db.getCollection('Accounts').find({"scenarioTag":{$in:["message"]}},{"mainNumber":1})

查询只显示某个字段

db.getCollection('Accounts').find({},{"scenarioTag":1})

查询某个字段中同个值的个数

db.Accounts.aggregate([{$group: { _id: "$scenarioTag",count:{$sum:1}}}])
count.png

更新某个字段的值
inc : The inc operator increments a value of a field by a specified amount. If the field does not exist, $inc adds the field and sets the field to the specified amount.The inc operator accepts positive and negative values.
If the field does not exist, inc creates the field and sets the field to the specified value.
Use of the inc operator on a field with a null value will generate an error.
inc is an atomic operation within a single document.

set : The set operator replaces the value of a field with the specified value.
If the field does not exist, set will add a new field with the specified value, provided that the new field does not violate a type constraint. If you specify a dotted path for a non-existent field, set will create the embedded documents as needed to fulfill the dotted path to the field

db.getCollection('Accounts').update(
        {"genDate":"2018-10-10"},{$inc:{"genDate":"2017-1-1"}},false,true
        )
db.Accounts.update(
        {"envEntity.env":"**"},{$set:{"envEntity.env":"***"}},false,true
        )
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容