mongoDB query command

2018-12-20  本文已影响0人  tyoko

模糊查询
/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
        )
上一篇 下一篇

猜你喜欢

热点阅读