需求:将商品表的相同型号的商品中价格为[[0,0]]的异常数据标记为404
//collection_name MongoDB集合名
//ModelName 字段名
//Tiered 字段名,值[[0,0]]
db.collection_name.find({"error":404}).count();
var arr = db.collection_name.aggregate([{ $group: { _id : '$ModelName', count: { $sum : 1 } } }, { $match: { count: { $gt : 1} } }]).valueOf();
arr.forEach(function(doc) {
var datas = db.collection_name.find({"ModelName":doc._id}).valueOf();
datas.forEach(function(doc) {
if(doc.Tiered[0][0]== 0 && doc.Tiered[0][1]==0 ){
// print(doc.GoodsSn)
db.collection_name.updateOne({GoodsSn : doc.GoodsSn},{$set:{error:NumberInt(404)}}, {upsert:false})
}
});
});
参考:https://www.haohongfan.com/posts/2018-09-30-mongo-duplicate/