package test.feed;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.skyline.common.mongo.MongoTemplate;
public class TestMongoDao {
private static MongoTemplate mongoTemplate;
@BeforeClass
public static void init() {
ApplicationContext ctx = new ClassPathXmlApplicationContext("config/context/applicationContext.xml");
mongoTemplate = (MongoTemplate) ctx.getBean("mongoTemplate");
}
@Test
public void testInsert2() {
DBCollection collection = mongoTemplate.getCollection("feed");
List<DBObject> objs=new ArrayList<DBObject>();
for (int i = 0; i < 10000; i++) {
BasicDBObject object = new BasicDBObject();
object.put("ownerId", i % 10000);
object.put("ownerNickname", "nickname" + i % 10000);
object.put("ownerPortrait", "portrait" + i % 10000);
object.put("content", "this is content" + i);
object.put("actionerId", i % 10000 + 7);
object.put("actionerNickname", "nickname" + i % 10000 + 7);
object.put("actionerrPortrait", "portrait" + i % 10000 + 7);
objs.add(object);
//collection.insert(object);
}
collection.insert(objs);
}
// @Test
public void testInsert() {
DBCollection collection = mongoTemplate.getCollection("feed");
for (int i = 0; i < 10000; i++) {
BasicDBObject object = new BasicDBObject();
object.put("ownerId", i % 10000);
object.put("ownerNickname", "nickname" + i % 10000);
object.put("ownerPortrait", "portrait" + i % 10000);
object.put("content", "this is content" + i);
object.put("actionerId", i % 10000 + 7);
object.put("actionerNickname", "nickname" + i % 10000 + 7);
object.put("actionerrPortrait", "portrait" + i % 10000 + 7);
collection.insert(object);
}
}
//@Test
public void testQueryIn() {
DBCollection collection = mongoTemplate.getCollection("feed");
// collection.createIndex(new BasicDBObject("actionerId",1));
DBObject in_data = new BasicDBObject("$in", new Object[] { 1, 3, 5, 37, 41, 33, 55, 37, 16, 333, 325, 767,
5431, 7653, 435, 787, 561, 233, 75, 657, 19, 653, 543, 745, 16, 334, 53, 71, 123, 335, 554, 746, 44,
23, 45, 432, 7765, 432, 75, 213, 6543, 432, 7576, 1321, 6546, 4234, 657, 42132, 675, 432, 654, 4232,
543 });
// 根据userId in('10010171','10010172')查询
DBObject in_query = new BasicDBObject();
in_query.put("actionerId", in_data);
DBCursor inQueryResult = collection.find(in_query);
// System.out.println("按in条件查询结果:");
for (Iterator<DBObject> iter = inQueryResult.iterator(); iter.hasNext();) {
// System.out.println(iter.next());
iter.next();
}
}
}