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) {});