mongo和mongoose基本语法

mongo:

1. distinct

db.getCollection('tokentransfer').distinct('from',{"$and": [{"blockNumber": {$gte: 370000}}, {"blockNumber": {$lte: 373085}}]}).length

2. update:(更新满足条件的所有,multi设置true)
db.getCollection('holder').update({},{$set:{'quantity':null}},{multi:true})

3. 模糊匹配

db.getCollection('tokentransfer').find({"transactionHash": /0x2c/})

4. group(group by address字段,count(*)倒叙,查找address重复最多的记录)

db.holder.aggregate([{$group : {_id : "$address", num : {$sum : 1}}}, {$sort:{num: -1}}])

 

mongoose:

1. 增:

单个增:
var fromAddressHolder = {"address": fromAddress,  "percentage": percentage, "flag": fromAddress + currContractAddress};
new Holder(fromAddressHolder).save(function(err){
	if (err) {
		log.error("fromAddress新增失败");
	} else {
		log.info("fromAddress新增成功!");
	}
});


批量增:
TokenTransfer.collection.insert(tokOps, function( err, tx ){
    if (er) {
        log.warn('Skip: Duplicate key ' + err);
    } else {
    	log.info('transfer insert success');

    }
});


2. 删:

LackBlock.remove({"blockHash": desiredBlockHashOrNumber}, function(err){
    if (err) {
    	log.error("lackBlock删除失败-----" + err);
    } else {
    	log.info("lackBlock删除成功!");
    }
});

3. 改:

Holder.update({"address": toAddress, "contractAddress": currContractAddress}, {$set: {"percentage": percentage}}, function(err){
	if (err) {
		log.error("toAddress更新余额失败-----" + err);
	} else {
		//console.log("toAddress更新余额成功!");
	}
});

4. 新增或修改

var tokenHolder = {'address':address, 'quantity':quantity, 'contractAddress':contractAddress,
                    'percentage':'', 'flag':address + contractAddress};
                    
Holder.update({'flag': address + contractAddress}, {$set: tokenHolder}, {upsert: true}, function(err){
    if (err) {
        log.error("holder新增或更新失败");
    } else {
        log.info("holder新增或更新成功!");
    }

});

5. 模糊查询:

mongodb的原生语法是/xxx/, mongoose不能使用的原因是组装后是“/xxx/”

参考https://segmentfault.com/a/1190000008161345

6. 多条件查询,并sort(顺序依次是条件,查询列,分页的skip/limit/sort)

Transaction.find({"$and": [{input: /0xa9059cbb/}, {"blockNumber": {$gte: fromNum}}]},
'from to input hash timestamp blockNumber',{sort:{blockNumber:1}},function (err, result) {});

InOrOutRecord.find({"owner": from, "type": type}, null, {skip: page * rows, limit: rows, sort: {"updateTime": -1}}, function (err1, result1) {});

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值