db.getCollection('br_wlt').ensureIndex({"addr":1})
2)、or操作
https://blog.csdn.net/yaomingyang/article/details/75103480
db.getCollection('br_wlt').find({$or:[{'addr1':'3Az4X8'},{'addr2':'3Az4X8'}]})
3)、修改字段类型
db.getCollection('br_wlt').find({$or:[{'addr1':'3Az4X8'},{'addr2':'3Az4X8'}]})
3)、修改字段类型
https://cloud.tencent.com/developer/article/1406368
将字符串修改为int
db.getCollection('br_wlt').find({}).forEach(
function(doc){
db.getCollection('br_wlt').update({'_id': doc._id},{$set:{'bal': parseInt(doc.bal)}})
}
)
4)、group by 操作
https://www.cnblogs.com/shaosks/p/5760819.html
db.getCollection('chain_state').aggregate(
[
{
$group:
{
_id:'$addr',
balance:{$sum:"$bal"}
}
}
]
)
Error: command failed: {
"ok" : 0,
"errmsg" : "Exceeded memory limit for $group, but didn't allow external sort. Pass allowDiskUse:true to opt in.",
"code" : 16945,
"codeName" : "Location16945"
} : aggregate failed
db.getCollection('br_wlt').update({},{$unset:{bal:1}},false,true)
将字符串修改为int
db.getCollection('br_wlt').find({}).forEach(
function(doc){
db.getCollection('br_wlt').update({'_id': doc._id},{$set:{'bal': parseInt(doc.bal)}})
}
)
4)、group by 操作
https://www.cnblogs.com/shaosks/p/5760819.html
db.getCollection('chain_state').aggregate(
[
{
$group:
{
_id:'$addr',
balance:{$sum:"$bal"}
}
}
]
)
Error: command failed: {
"ok" : 0,
"errmsg" : "Exceeded memory limit for $group, but didn't allow external sort. Pass allowDiskUse:true to opt in.",
"code" : 16945,
"codeName" : "Location16945"
} : aggregate failed
db.getCollection('RAW_COLLECTION').aggregate([
// Group on unique value storing _id values to array and count
{ "$group": {
"_id": { RegisterNumber: "$RegisterNumber", Region: "$Region" },
"ids": { "$push": "$_id" },
"count": { "$sum": 1 }
}},
// Only return things that matched more than once. i.e a duplicate
{ "$match": { "count": { "$gt": 1 } } }
], { allowDiskUse: true } )
5)、删除一个字段db.getCollection('br_wlt').update({},{$unset:{bal:1}},false,true)
使用update命令
update命令
update命令格式:
db.collection.update(criteria,objNew,upsert,multi)
参数说明:
criteria:查询条件
objNew:update对象和一些更新操作符
upsert:如果不存在update的记录,是否插入objNew这个新的文档,true为插入,默认为false,不插入。
multi:默认是false,只更新找到的第一条记录。如果为true,把按条件查询出来的记录全部更新。
//例如要把User表中address字段删除 db.User.update({},{$unset:{'address':''}},false, true)