温馨提示:您的每一次转载,体现了我写此文的意义!!!烦请您在转载时注明出处http://www.blogjava.net/sxyx2008/谢谢合作!!!

雪山飞鹄

温馨提示:您的每一次转载,体现了我写此文的意义!!!烦请您在转载时注明出处http://www.blogjava.net/sxyx2008/谢谢合作!!!

BlogJava 首页 新随笔 联系 聚合 管理
  215 Posts :: 1 Stories :: 674 Comments :: 0 Trackbacks
添加Mongodb驅動到你的classpath環境中
package com.mongodb;


public class Mongodb {
    
    
private Mongo mongo;
    
private DBCollection dbCollection;
    
private DB db;
    
    
public Mongodb() throws Exception{
        
//使用ip地址创建Mongo对象
        mongo=new Mongo("127.0.0.1");
        
//获取orcl数据库
        db=mongo.getDB("orcl");
        
//判断是否存在集合person
        boolean b=db.collectionExists("person");
        System.out.println(
"是否存在集合[person]:"+b);
        dbCollection 
= db.getCollection("person");
        
long count=dbCollection.count();
        System.out.println(
"总记录数是:"+count);
        DBCursor cursor 
=dbCollection.find().skip(20).limit(20);
        
while(cursor.hasNext()){
            System.out.println(cursor.next());
        }
        
        
    }
    
    
    
    
public void list(){
        dbCollection
=db.getCollection("person");
        BasicDBObject dbObject
=new BasicDBObject();
        dbObject.put(
"age"new BasicDBObject("$gt",20).append("$lt"60));
        DBCursor cursor 
= dbCollection.find(dbObject);
        System.out.println(cursor.count());
        
while(cursor.hasNext()){
            System.out.println(cursor.next());
        }
        
    }
    
    
    
    
private boolean insert(){
        
        dbCollection
=db.getCollection("person");
        BasicDBObject dbObject
=new BasicDBObject();
        dbObject.put(
"name""zhangsan");
        dbObject.put(
"age"20);
        WriteResult writeResult 
= dbCollection.save(dbObject);
        System.out.println(writeResult.getN());
        
return false;
    }
    
    
private boolean delete(){
        dbCollection
=db.getCollection("person");
        BasicDBObject dbObject
=new BasicDBObject();
        dbObject.put(
"name""zhangsan");
        WriteResult writeResult 
= dbCollection.remove(dbObject);
        System.out.println(writeResult.getN());
        
return false;
    }
    
    
    
private boolean update(){
        dbCollection
=db.getCollection("person");
        BasicDBObject dbObject
=new BasicDBObject();
        dbObject.put(
"name""s0020");
        BasicDBObject dbObject2
=new BasicDBObject();
        dbObject2.put(
"name""s0020");
        dbObject2.put(
"age"65);
        WriteResult writeResult 
= dbCollection.update(dbCollection.findOne(dbObject), dbObject2);
        System.out.println(writeResult.getN());
        
return false;
    }
    
    
    
private Object getOne(){
        dbCollection
=db.getCollection("person");
        BasicDBObject dbObject
=new BasicDBObject();
        dbObject.put(
"name""s0020");
        
//dbObject.put("age", 65);
        DBObject object=dbCollection.findOne(dbObject);
        System.out.println(object.toMap().get(
"name")+"\t"+object.toMap().get("age"));
        
return object;
    }
    
    
    
public static void main(String[] args) throws Exception{
        Mongodb mongodb
=new Mongodb();
        
//mongodb.insert();
        
//mongodb.getOne();
        
//mongodb.update();
        
//mongodb.delete();
        
//mongodb.list();
    }
}
posted on 2012-03-12 16:05 雪山飞鹄 阅读(5658) 评论(2)  编辑  收藏 所属分类: MongoDB

Feedback

# re: Java操作Mongodb 2013-05-27 11:07 安安
不错  回复  更多评论
  

# re: Java操作Mongodb 2014-07-03 14:34 zuidaima
不错  回复  更多评论
  


只有注册用户登录后才能发表评论。


网站导航: