mongodb常用脚本

1.查询
/** or **/
db.getCollection('collectionName').find({$or: [{"fieldName1": 10424}, {"fieldName2": 676}]});

/** in、not in **/
db.getCollection('collectionName').find({"fieldName": {$in: [10424, 24457]}});
db.getCollection('collectionName').find({"fieldName": {$nin: [24456, 24457]}});

/** !=、<、>、<=、>= **/
db.getCollection('collectionName').find({"fieldName": {$ne: null}});
db.getCollection('collectionName').find({"fieldName": {$lt: 24456}});
db.getCollection('collectionName').find({"fieldName": {$gt: 24456}});
db.getCollection('collectionName').find({"fieldName": {$lte: 24456}});
db.getCollection('collectionName').find({"fieldName": {$gle: 24456}});

/** like **/
db.getCollection('collectionName').find({"fieldName": /te/});
db.getCollection('collectionName').find({"fieldName": /^te/});
db.getCollection('collectionName').find({"fieldName": /te$/});

/** count、limit、skip、sort、distinct**/
db.getCollection('collectionName').count();
db.getCollection('collectionName').find().limit(10).skip(5);
db.getCollection('collectionName').find().sort({"fieldName": -1});
db.getCollection('collectionName').distinct("fieldName1", {"fieldName2":223});

/** list操作 **/
db.getCollection('collectionName').find({"fieldName1": {$elemMatch: {"fieldName2": 6266}}});
db.getCollection('collectionName').find({"fieldName1.0.fieldName2": 6266});
db.getCollection('collectionName').find({"fieldName": {$size: 8}});

/** 投影 **/
db.getCollection('collectionName').find({"fieldName": 223}, {"fieldName1": 1});
db.getCollection('collectionName').find({"fieldName": 223}, {"fieldName2": 0});

/** group **/
db.getCollection('collectionName').group({
    key: {"fieldName1": 1},
    cond: {"fieldName2": {$gte: 233}},
    reduce: function(curr, result) {
        result.total += 1;
    },
    initial:{total: 0}
});

2.更新
/** update **/
db.getCollection('collectionName').update({'fieldName': 88888}, {$set: {'fieldName': 8888}}, false, true);
db.getCollection('collectionName').update({'fieldName': 88888}, {$set: {'fieldName': 8888}}, {multi: true});
db.getCollection('collectionName').update({'fieldName': 88888}, {$unset: {'fieldName': 8888}}, false, true);

3.删除
/** delete、drop **/
db.getCollection('collectionName').remove({"fieldName": 88888});
db.getCollection('collectionName').drop();

4.索引
db.getCollection('collectionName').createIndex({"fieldName": 1}, {background: true});
db.getCollection('collectionName').dropIndex("fieldName");
db.getCollection('collectionName').getIndexes();
db.getCollection('collectionName').totalIndexSize();
db.getCollection('collectionName').dropIndexes();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值