MongoDB 使用group by 并显示其他列max值

以下脚本实现功能为把word_info表通过word字段去重,并写到到word_info_new表里

db.word_info.aggregate([
{$group:
        { _id:"$word" ,
         meaning:{$max:"$meaning"}, 
         usphonetic:{$max:"$usphonetic"} 
        }
 }]).forEach(
            function(dc){
                    db.word_info_new.insert(
                                    {"word":dc._id,
                                        "meaning":dc.meaning,
                                        "usphonetic":dc.usphonetic
                                    }
                                )}
    );

猜你喜欢

转载自blog.51cto.com/mydbs/2407193
0条评论
添加一条新回复