MongoDB学习

查询:

查询将会返回一个游标,游标只会在你需要的时候才将需要的文档批量返回;还有许多针对游标执行的元操作,包括忽略一定数量的结果,或者限定返回结果的数量,以及对结果的排序

可以用向查询文档中添加键值对的形式限定查询条件。如:db.users.find({"usernaem":"joe","age":27})  查询用户名和年龄为规定的数值的文档

有的时候并不需要将文档中的所有键值对都返回。通过find()的第二个参数指定想要的键。如:只对用户集合的username和email感兴趣: db.users.find({},{"usernaem":1,"email":1});也可以剔除键: db.users.find({},{"email":0})

db.users.find({},{"usernaem":1,"_id":0})剔除“_id”键只显示username键。

"$lt" "$lte" "$gt" "$gte"  <  <=  > >=   例如:db.users.find({"age":{"$gte":18,"$lte":30}})   $ne  不等于   $in  用来查询一个键的多个值    $nin 将返回与数组中所有条件都不匹配的文档。     db.users.find({"age":{"$in":[12,143,24]}})    db.users.find({"age":{"$nin":[12,143,24]}})     $or  对多个键 取或值   db.users.find({"$or":[{"age":33},{"winner":true}]})      也可以都整合起来使用

$not  是元条件句,即可以用在任何其他条件之上。            db.users.find({"id_num":{"$mod":[5,1]}})  将查询的值除以第一个给定值,若余数等于第二个给定值则匹配成功

  db.users.find({"id_num":{"$not":{"$mod":[5,1]}}})  

 db.users.find({"$and":[{"x":{"$lt":1}},{"x":4}]}) 查询 X 的值字段的值小于等于1 并且 等于 4 {"x":[0,4]}

 

 

自定义实现mongodbFactory工厂类  实现与Spring的整合:

<bean id="mongoDBFactory" class="cn.edu.neu.tqgp.util.MongoDBFactory"

        init-method="init">

        <property name="url" value="219.216.125.71:27017"></property>

        <property name="username" value="root"></property>

        <property name="db" value="admin"></property>

        <property name="password" value="root"></property>

        <property name="min" value="50"></property>

        <property name="max" value="5"></property>

    </bean> 

MongoDBFactory的实现:

import com.mongodb.DB;

import com.mongodb.Mongo;

import com.mongodb.MongoOptions;



/**

 * 通过扩展AbstractFactoryBean创建 Mongo的FactoryBean类

 */

public class MongoDBFactory {

    

    private MongoOptions options;

    private String url;

    private String username; // 用户名

    private String password; // 密码

    private String db;

    private Mongo mongo;

    private int max;

    private int min;



    private void init() throws Exception {

        options = new MongoOptions();

        options.setAutoConnectRetry(true);

        options.setConnectionsPerHost(min); // 连接池初始大小

        options.setThreadsAllowedToBlockForConnectionMultiplier(max); // 连接数最大倍率



        Mongo mongo = new Mongo(url, options);

        DB admin = mongo.getDB(db);

        

        boolean bool = admin.authenticate(username, password.toCharArray());

        

        if (bool) {

            this.mongo = mongo;

        } else {

            throw new Exception("用户名密码错误");

            

        }

    }



    public DB getMongoDB() {

        return mongo.getDB(db);

    }



    public String getUrl() {

        return url;

    }



    public void setUrl(String url) {

        this.url = url;

    }



    public String getUsername() {

        return username;

    }



    public void setUsername(String username) {

        this.username = username;

    }



    public String getPassword() {

        return password;

    }



    public void setPassword(String password) {

        this.password = password;

    }



    public String getDb() {

        return db;

    }



    public void setDb(String db) {

        this.db = db;

    }



    public int getMax() {

        return max;

    }



    public void setMax(int max) {

        this.max = max;

    }



    public int getMin() {

        return min;

    }



    public void setMin(int min) {

        this.min = min;

    }



}

在dao层的应用:

public List<DBObject> queryMySurvey(Student student) {

        DB db = this.mongoFactory.getMongoDB();

        DBCollection collection = db.getCollection("SURVEYS");

        List<DBObject> list = new ArrayList<DBObject>();

        BasicDBObject basicDBObject = new BasicDBObject();

        basicDBObject.put("major", new BasicDBObject("$in", new String[] { "所有专业", student.getMajor() }));

        basicDBObject.put("college", new BasicDBObject("$in", new String[] { "全校", student.getCollege() }));

        basicDBObject.put("grade", new BasicDBObject("$in", new String[] { "所有年级", student.getGrade() }));

        DBCursor dbCursor = collection.find(basicDBObject);

        

        while (dbCursor.hasNext()) {

            DBObject dbObject = dbCursor.next();

            list.add(dbObject);

        }

        return list;

    }

 

你可能感兴趣的:(mongodb)