MongoDB语句命令

   

更新列名
db.xx.update({}, {$rename : {"StoreId" : "MetaId"}}, false, true)

查询长度
db.getCollection("xx").find({$where:'this.StoreId.length>2'},{Name:0})

查询总条数
db.getCollection("xx").find({}).count()

区间查询
db.getCollection("xx").find({'StoreId':1139,'CardNo':{'$gte':'90225001','$lte':'90295000'}})

多个区间查询
db.getCollection("xx").find({ $or : [{ $and : [{"CardNo" : { $gte : "10000001" }}, {"CardNo" : { $lte : "10000003" }}] }, { $and : [{"CardNo" : { $gte : "10000006" }}, {"CardNo" : { $lte : "10000008" }}] }] }).limit(1000).skip(0) 

排序 1 升序  -1降序
db.getCollection("xx").find().sort({"CreationTime":1})

db.xx.find({'PicId' : { $type : 16 }}).forEach(function(x) {x.PicId = String(x.PicId);db.Youzy_Stores_Experts.save(x); })

添加一个字段.  table 代表表名 , 添加字段 content,字符串类型。
db.table.update({}, {$set: {content:""}}, {multi: true})

删除一个字段
db.table.update({},{$unset:{content:""}},false, true)

清空数据
db.table.remove({})

查询指定列
db.news.find( {}, { id: 1, title: 1 } )

修改列表 db.getCollection(
'xx').update({},{$rename:{"OId":'MetaId'}},false,true) 添加索引 db.test.createIndex({"username":1}) db.Youzy_Users_MobileAuthCodes.createIndex({"Code":1,"Mobile":1,"ExpiresTime":1},{"name":"MobileAuthCodes_Validate"}) group db.getCollection("xx").aggregate([{$match:{"IsDeleted":false}},{$group : {_id : "$UserId", count : {$sum : 1}}},{$sort:{"count":-1}}]) --条件修改 update单个 updatemany 全部 db.getCollection('xx').update( // query { "MenuKey" : 28 }, // update { $set:{"Url":"/tzy/choosebatch?type=3"} }, false, true ); 按照时间年月分组 db.xx.aggregate([ {$match: { CreateDate: { $gte: new Date('2018-01-01'), $lte: new Date('2019-07-31') } }} , {$group:{_id:{CreateDate:{year: { $year: "$CreateDate"},month: { $month: "$CreateDate" }}}, count: { $sum: 1 }}} ]) 分组 db.xx.aggregate([ {$match: { CreateDate: { $gte: new Date('2017-01-01'), $lte: new Date('2019-07-31') }}} , { "$group": { _id: { month: { $dateToString: { format: "%Y-%m", date: "$CreateDate" } } }, count: { $sum: 1 } } }, { "$project": { "年月": "$_id.month", "总数": "$count", } } ])

 

posted @ 2019-10-18 13:48  卡玛兹  阅读(487)  评论(0编辑  收藏  举报
你好