–$in
db.Student.find({"_id":{"$in":[1001,1002,1]}})
–$or
db.Student.find({"$or":[{"_id":99},{"pageViews":2}]})
–and and or
db.Student.find({"pageViews":2,"$or":[{"_id":99}]})
游标控制
for(i=0;i<10;i++){
db.Student.insert({"_id":i});
}
var cursor=db.Student.find();
cursor.forEach(function(x){
print(x);
});
–分页查询
db.Student.find().limit(3)
db.Student.find().limit(3).skip(3)
–排序
db.Student.find().sort({"_id":-1}) --1为升序,-1为降序
db.Student.find().sort({"_id":-1,"pageView":1}) --前面如果相同就按后面pageView的升序来排序
DBHelper:
package com.yc.link;
import java.net.UnknownHostException;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.Mongo;
import com.mongodb.MongoException;
public class DBHelper {
private static Mongo mongo=null;
private DB db=null;
static{
try {
mongo=new Mongo(Mypro.getInstance().getProperty("ip"),Integer.parseInt(Mypro.getInstance().getProperty("port")));
} catch (NumberFormatException e) {
e.printStackTrace();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (MongoException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public boolean getDB(String dbName,String name,String pwd){
mongo.getDB(dbName);
if(name!=null&&!"".equals(name)&&pwd!=null&&!"".equals(pwd)){
if(db.authenticate(name, pwd.toCharArray())){
return true;
}else{
return false;
}
}else{//查看配置文件中是否给定用户名和密码
String uname=Mypro.getInstance().getProperty("uname");
String pwds=Mypro.getInstance().getProperty("password");
if(name!=null&&!"".equals(name)&&pwd!=null&&!"".equals(pwd)){
if(db.authenticate(name, pwd.toCharArray())){
return true;
}else{
return false;
}
}
}
return true;
}
/**
* 关闭链接
* */
public void closeAll(Mongo mongo){
if(mongo!=null){
mongo.close();
}
}
/**
* 获取指定的集合
* collectionName要链接的集合
* dbName集合所在的数据库
* 链接数据的用户名
* 链接数据库的密码
*
* */
public DBCollection getDBCollection(String collectionName,String dbName){
DBCollection dbCollection=null;
if(getDB(dbName,null,null)){
db.requestStart();
if(collectionName==null){//如果为空则冲配置文件中加载
collectionName=Mypro.getInstance().getProperty(collectionName);
}
dbCollection= db.getCollection(collectionName);
}else{
throw new RuntimeException("数据库连接失败.....");
}
return dbCollection;
}
public DBCollection getDBCollection(String collectionName,String dbName,String name,String password){
DBCollection dbCollection=null;
if(getDB(dbName,name,password)){
db.requestStart();
if(collectionName==null){//如果为空则冲配置文件中加载
collectionName=Mypro.getInstance().getProperty(collectionName);
}
dbCollection= db.getCollection(collectionName);
}else{
throw new RuntimeException("数据库连接失败.....");
}
return dbCollection;
}
}
Mypro:单例
package com.yc.link;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class Mypro extends Properties {
private static Mypro instance=new Mypro();
private Mypro(){
InputStream is=Mypro.class.getResourceAsStream("/db.properties");
try {
this.load(is);
} catch (IOException e) {
e.printStackTrace();
}finally{
if(is!=null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static Mypro getInstance(){
return instance;
}
}
package com.yc.link;
public class Test {
public static void main(String[] args) {
DBHelper db=new DBHelper();
System.out.println(db);
}
}
db.properties
ip=127.0.0.1
port=27017
dbName=
uname=
password=
collectionName=
DBHelper里应该要多封装一些常用方法,大家可以到网上慢慢收集
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-12-14 07:38:05