由于后台删除了一部分评论,导致文章评论书有少量的不准,需要更正,这里用到 aggregate 。

> db.comments.find({},{topicid:1})
{ "_id" : ObjectId("5105e5aacd0cc40a0a025d5f"), "topicid" : ObjectId("5101ed41cd0cc40a0a021366") }
{ "_id" : ObjectId("512467d0cd0cc40a0a052ccd"), "topicid" : ObjectId("51231fdccd0cc40a0a050d25") }
{ "_id" : ObjectId("51249527cd0cc40a0a0534c6"), "topicid" : ObjectId("50f674eccd0cc40a0a0114ee") }
{ "_id" : ObjectId("512af904cd0cc40a0a05f533"), "topicid" : ObjectId("512ad65ecd0cc40a0a05f080") }
{ "_id" : ObjectId("514036f0fd6181f368129719"), "topicid" : ObjectId("512ad65ecd0cc40a0a05f080") }
{ "_id" : ObjectId("5141939ffd6181f3681402a7"), "topicid" : ObjectId("514192d7fd6181f3681401cf") }
{ "_id" : ObjectId("5142bed1fd6181f368155ef6"), "topicid" : ObjectId("512596b4cd0cc40a0a0558f8") }
{ "_id" : ObjectId("51491552dba1dfc40801b7b3"), "topicid" : ObjectId("50f674eccd0cc40a0a0114ee") }
{ "_id" : ObjectId("51510a0644d87ffd18000590"), "topicid" : ObjectId("512596b4cd0cc40a0a0558f8") }
{ "_id" : ObjectId("51525146dc9f934119011c5b"), "topicid" : ObjectId("515114f3dc9f9341190008d7") }
{ "_id" : ObjectId("5154f17e8c49a72a36008d85"), "topicid" : ObjectId("50f36bf3cd0cc40a0a00c447") }
{ "_id" : ObjectId("515ade7cf7e53fe453021222"), "topicid" : ObjectId("5158f51061ff09b335008061") }
{ "_id" : ObjectId("51664959f7e53fe4530cb0d4"), "topicid" : ObjectId("50caa33fee680fee79000c82") }
{ "_id" : ObjectId("51887382e38d26a40a004e78"), "topicid" : ObjectId("512047a5cd0cc40a0a04c381") }
{ "_id" : ObjectId("51aed24e875faf754b03a02e"), "topicid" : ObjectId("519b48b8e38d26a40a0a21c4") }
{ "_id" : ObjectId("51cbca0f4308d66406094f1c"), "topicid" : ObjectId("51cbb4f64308d664060939c2") }
{ "_id" : ObjectId("51cfb5320acf7bac0200e37f"), "topicid" : ObjectId("51b2d5e8875faf754b05fd48") }
{ "_id" : ObjectId("51d0e9950acf7bac0202265a"), "topicid" : ObjectId("512047a5cd0cc40a0a04c381") }
{ "_id" : ObjectId("51d4e5680acf7bac0206d8a5"), "topicid" : ObjectId("51d4dea60acf7bac0206c6ec") }
{ "_id" : ObjectId("51edef688b79850857032e45"), "topicid" : ObjectId("5173d275cbce24580a033bd8") }
Type "it" for more

> db.comments.aggregate( [
...    { $project: { topicid: 1 } },
...    { $unwind: "$topicid" },
...    { $group: { _id: "$topicid", count: { $sum : 1 } } },
...     { $out : "tmp" }
... ] )
> db.tmp.find()
{ "_id" : ObjectId("55db387bff8e2192371e11a2"), "count" : 3 }
{ "_id" : ObjectId("56dc4e9ae13823525b000013"), "count" : 1 }
{ "_id" : ObjectId("55f23070502fe95e526c54b5"), "count" : 1 }
{ "_id" : ObjectId("55c9a69ec3f8a8336f3bf71a"), "count" : 1 }
{ "_id" : ObjectId("559f7bd687aa16386feb498a"), "count" : 4 }
{ "_id" : ObjectId("5456503ad9abb4454c31d4fa"), "count" : 2 }
{ "_id" : ObjectId("55011395f65f78ef16fac03d"), "count" : 10 }
{ "_id" : ObjectId("5507b96dbbaece6250c8c70e"), "count" : 4 }
{ "_id" : ObjectId("54cb35a64f8f66db6c7d1126"), "count" : 1 }
{ "_id" : ObjectId("54cb25397d59472762d51281"), "count" : 1 }
{ "_id" : ObjectId("5497ddc88df7ac243fa4de8b"), "count" : 1 }
{ "_id" : ObjectId("5456f36755fdf95c54b42b60"), "count" : 3 }
{ "_id" : ObjectId("5451fa707f40d92c75af837e"), "count" : 7 }
{ "_id" : ObjectId("544494d8bed30a781084a66d"), "count" : 5 }
{ "_id" : ObjectId("5444d3c65e081e4b32903d0a"), "count" : 2 }
{ "_id" : ObjectId("53f6dcd40d6e869123593430"), "count" : 2 }
{ "_id" : ObjectId("53c35978a13bb9902464900a"), "count" : 1 }
{ "_id" : ObjectId("53a7d711a13bb9902477a566"), "count" : 2 }
{ "_id" : ObjectId("53a1fb14a13bb99024118915"), "count" : 1 }
{ "_id" : ObjectId("521466b1da65860a04115b23"), "count" : 1 }
Type "it" for more
> db.tmp.find({},{"count" : 1}).forEach( function(myDoc) { 
...   var c=db.topics.find({"_id" :myDoc._id})
...   if(!c.hasNext())
...    print("user: " + myDoc._id);
...    n=c.next()
...   if(n.commentcount!=myDoc.count) 
...    print("commentcount: " + n.commentcount +", tmp.count:" + myDoc.count);
...    db.topics.update({"_id" :myDoc._id},{$set:{commentcount:myDoc.count}})
...   } );
commentcount: 8, tmp.count:3
commentcount: 53, tmp.count:45
commentcount: 14, tmp.count:9
> db.tmp.find({},{"count" : 1}).forEach( function(myDoc) { 
...   var c=db.topics.find({"_id" :myDoc._id})
...   if(!c.hasNext())
...    print("user: " + myDoc._id);
...    n=c.next()
...   if(n.commentcount!=myDoc.count) 
...    print("commentcount: " + n.commentcount +", tmp.count:" + myDoc.count);
...   } );

发现 3 条数据不准,更正。


nosqldb 于 3 个月前 修改
0 回复
需要 登录 后方可回复, 如果你还没有账号你可以 注册 一个帐号。