|
bject();
query22.put("d", 6);
List orQueryList2 = new ArrayList();
orQueryList2.add(query21);
orQueryList2.add(query22);
BasicDBObject orQuery2 = new BasicDBObject("$or", orQueryList2);
List orQueryCombinationList = new ArrayList();
orQueryCombinationList.add(orQuery1);
orQueryCombinationList.add(orQuery2);
BasicDBObject finalQuery = new BasicDBObject("$and",
orQueryCombinationList);
DBCursor cursor = MongoInit.getColl("wujintao").find(finalQuery);
}
/**
* IN查询
* if i need to query name in (a,b); just use { name : { $in : ['a', 'b'] } }
* select * from things where name='a' or name='b'
* @param coll
*/
public void queryIn() {
BasicDBList values = new BasicDBList();
values.add("a");
values.add("b");
BasicDBObject in = new BasicDBObject("$in", values);
DBCursor cursor = MongoInit.getColl("wujintao").find(
new BasicDBObject("name", in));
try {
while (cursor.hasNext()) {
System.out.println(cursor.next());
}
} finally {
cursor.close();
}
}
/**
* 或查询
* select * from table where name = '12' or title = 'p'
* @param coll
*/
public void queryOr() {
QueryBuilder query = new QueryBuilder();
query.or(new BasicDBObject("name", 12), new BasicDBObject("title", "p"));
DBCursor cursor = MongoInit.getColl("wujintao").find(query.get()).addSpecial("$returnKey", "");
try {
while (cursor.hasNext()) {
System.out.println(cursor.next());
}
} finally {
cursor.close();
}
}
public void customQueryField() throws UnknownHostException{
Mongo mongo = new Mongo("localhost", 27017);
DB db = mongo.getDB("zhongsou_ad");
BasicDBObjectBuilder bulder = new BasicDBObjectBuilder();
bulder.add("times",1);
bulder.add("aid",1);
DBCursor cusor = db.getCollection("ad_union_ad_c_1").find(new BasicDBObject(),bulder.get());
for (DBObject dbObject : cusor) {
System.out.println(dbObject);
}
}
public void mapReduce() throws UnknownHostException{
Mongo mongo = new Mongo("localhost", 27017);
DB db = mongo.getDB("zhongsou_ad");
/***
* book1 = {name : "Understanding JAVA", pages : 100}
* book2 = {name : "Understanding JSON", pages : 200}
* db.books.save(book1)
* db.books.save(book2)
* book = {name : "Understanding XML", pages : 300}
* db.books.save(book)
* book = {name : "Understanding Web Services", pages : 400}
* db.books.save(book)
* book = {name : "Understanding Axis2", pages : 150}
* db.books.save(book)
*
var map = function() {
var category;
if ( this.pages >= 250 )
category = 'Big Books';
else
category = "Small Books";
emit(category, {name: this.name});
};
var reduce = function(key, values) {
var sum = 0;
values.forEach(function(doc) {
sum += 1;
});
return {books: sum};
};
var count = db.books.mapReduce(map, reduce, {out: "book_results"});
*/
try {
DBCollection books = db.getCollection("books");
BasicDBObject book = new BasicDBObject();
book.put("name", "Understanding JAVA");
book.put("pages", 100);
books.insert(book);
book = new BasicDBObject();
book.put("name", "Understanding JSON");
book.put("pages", 200);
books.insert(book);
book = new BasicDBObject();
book.put("name", "Understanding XML");
book.put("pages", 300);
books.insert(book);
book = new BasicDBObject();
book.put("name", "Understanding Web Services");
book. |