| 注册
请输入搜索内容

热门搜索

Java Linux MySQL PHP JavaScript Hibernate jQuery Nginx

mongoDB的java实现简单的评分、评论和标签

3
Java MongoDB C/C++ Go 13963 次浏览

用mongodb 的java开发包

 

实现歌曲的评分、评论、标签的基本功能。

 

第一次使用,如果有不对的地方希望能有热心人不吝赐教。

 

package mongodb;

import java.util.ArrayList;
import java.util.List;


import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.Mongo;

public class A1 {
	public static Mongo m=null;
	public static DB db=null;
	public static DBCollection  coll1 = null;
	
	
	//这个静态块用来连接mongodb数据库,默认端口
	static {
		try {
			m = new Mongo();
		} catch (Exception e) {
			e.printStackTrace();
		}
		db = m.getDB("mydb");
		coll1 = db.getCollection("music");
	}
	public static void main(String[] args) throws Exception {
		//建立一个基本的对象,存基本信息,像歌曲的歌手,歌曲名
		BasicDBObject doc = new BasicDBObject();
		doc.put("id", 1001);
		doc.put("singer", "刘德华");
		doc.put("song", "爱你一万年");
		
		//评分为一个单独的对象,可没有初始数据。
		BasicDBObject score = new BasicDBObject();
		score.put("0", 3);
		score.put("1", 1);
		score.put("2", 5);
		score.put("3", 4);
		score.put("4", 16);
		score.put("5", 16);
		doc.put("someScore", score);
		
		//评论的基本信息,可没有初始数据。
		List<String> reviews =new ArrayList<String>();
		reviews.add("很喜欢");
		doc.put("someReview", reviews);
		
		
		coll1.drop();
		coll1.insert(doc);
		DBCursor cur = coll1.find();
		while (cur.hasNext()) {
			System.out.println("修改前的数据:"+cur.next());
		}
		System.out.println("-------");
		
		addARevert("不好听");
		addARevert("打酱油");
		addARevert("真的挺不错,一直很喜欢刘德华");
		
		
		addAScore(5);
		addAScore(4);
		addAScore(2);
		
		addATags("经典");
		addATags("四大天王");
		addATags("流行");
		addATags("流行");
		addATags("经典");
		addATags("经典");
		addATags("刘德华");
		
		//显示出修改数据
		DBCursor cur1 = coll1.find();
		while (cur1.hasNext()) {
			System.out.println("修改后的数据:"+cur1.next());
		}
	}
	
	//加歌曲标签
	public static void addATags(String tag){
		BasicDBObject tagO =new BasicDBObject();
		tagO.append("tags."+tag, 1);
		BasicDBObject doc =new BasicDBObject();
		doc.put("$inc", tagO);
		coll1.update(new BasicDBObject().append("id", 1001), doc);
	}
	
	//评分
	public static void addAScore(int i){
		BasicDBObject doc =new BasicDBObject();
		doc.put("$inc", new BasicDBObject().append("someScore."+i, 1));
		coll1.update(new BasicDBObject().append("id", 1001), doc);
	}
	
	//加评论
	public static void addARevert(String revert ){
		BasicDBObject doc =new BasicDBObject();
		doc.put("someReview", revert);
		//这个是在数组末添加
		BasicDBObject temp=new BasicDBObject().append("$push", doc);
		coll1.update(new BasicDBObject().append("id", 1001), temp);
	}
	
}
转自:http://wdhdmx.iteye.com/blog/1223155

4个答案

0

First You got a great blog .I will be interested in more similar topics. i see you got really very useful topics, i will be always checking your blog thanks. 토토커뮤니티

0

I think this is definitely an amazing project here. So much good will be coming from this project. The ideas and the work behind this will pay off so much. เว็บดูหนังออนไลน์

0

When you use a genuine service, you will be able to provide instructions, share materials and choose the formatting style. best CBD oil for sleep

0

I think I'll get used to this kind of coding soon. Please try crush the castle 2 unblocked.