MongoDB数据读写的几种方法(二)

2014-11-24 03:24:03 · 作者: · 浏览: 7
使用时需要调用get方法生成具体query语句 ArrayList articles = new ArrayList(); //此处element类型均为Object HashSet clusters = new HashSet(); DBObject article = null; while(curs.hasNext()) { article = curs.next(); articles.add(article.get("_id")); clusters.add(article.get("clusterId")); } QueryBuilder removeBuilder = new QueryBuilder(); //注意下句使用了$in操作符,类似于{_id: articleID1} or {_id: articleID2} or {_id: articleID3} ... DBObject removeObject = new BasicDBObject("_id", new BasicDBObject("$in", articles)); removeBuilder.and(removeObject); /*打印结果*/ coll.remove(removeBuilder.get()); DBObject articleCountQuery = null; for(Object o: clusters) { articleCountQuery = new BasicDBObject("clusterId", o); curs = coll.find(articleCountQuery); if(curs.count() != 0) { clusters.remove(o); } } removeObject = new BasicDBObject("clusterId", new BasicDBObject("$in", clusters)); removeBuilder.and(removeObject); coll.remove(removeBuilder.get()); /** curs = coll.find(removeBuilder.get()); articles = new ArrayList
(); clusters = new HashSet(); article = null; while(curs.hasNext()) { article = curs.next(); articles.add(article.get("_id")); clusters.add(article.get("clusterId")); } /**/ System.out.println(articles); System.out.println(clusters); } }

定时操作,参考这篇博文,利用Java代码编程实现(利用开源库Quartz)。

Linux的环境可以使用crontab工具,更为简单方便。此处所需要配合使用的JS代码简略。