mongoDB过滤重复、循环替换

一、滤重

db.question.aggregate([
    { $match: { "audit.status": false } },
    { $group: { _id: "$md5", max_id: { $max: "$_id" }, count: { $sum: 1 }, dups: { $addToSet: '$_id' } } },
    { $match: { "count": { "$gt": 1 } } },
    { $sort: { count: -1 } }
],{allowDiskUse:true}).forEach(function (doc) {
    doc.dups.shift();
    db.question.remove({ _id: { $in: doc.dups } });
})

二、循环替换

let reg= /【   】/g
db.question.find({question:{$regex:reg}}).forEach(i=>{
    let c = i.question.replace(reg, '')
    console.log(i._id,i.question)
    console.log(i.question.match(reg))
    db.question.update({_id:i._id},{$set:{question:c}});
}) 

 

posted @ 2018-02-24 09:26  yudis  阅读(816)  评论(0编辑  收藏  举报