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", } } ])
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步