• java操作mongodb


    package pymongo1;

    import com.mongodb.BasicDBObject;
    import com.mongodb.DB;
    import com.mongodb.DBCollection;
    import com.mongodb.DBCursor;
    import com.mongodb.DBObject;
    import com.mongodb.MongoClient;

    public class m1 {
    public String url;
    public int port;
    public String dbname;


    //public static String collection_name="DeviceData";
    MongoClient mongoClient;
    DB db;

    public static void main(String[] args) {
    // TODO Auto-generated method stub


    }
    public m1(String url,int port,String dbname)
    {
    this.url=url;
    this.port=port;
    this.dbname=dbname;
    }
    public DB conn()
    {
    mongoClient = new MongoClient(url , port );
    db = mongoClient.getDB( dbname );
    return db;

    }
    public DBCursor m2(String collection_name){
    DBCursor cursor=null;
    try{


    DBCollection coll = db.getCollection(collection_name);
    cursor=coll.find();
    // while(cursor.hasNext()){System.out.println(cursor.next());}
    //


    }catch(Exception e){e.printStackTrace();}
    return cursor;
    }


    public DBCursor m2(String collection_name,DBObject ob)
    {
    DBCursor cursor=null;
    try{


    DBCollection coll = db.getCollection(collection_name);
    cursor=coll.find(ob);

    // while(cursor.hasNext()){System.out.println(cursor.next());}
    //


    }catch(Exception e){e.printStackTrace();}
    return cursor;


    }


    public DBObject m3(String collection_name,BasicDBObject ob,String flag)
    {
    DBObject cursor=null;

    try{


    DBCollection coll = db.getCollection(collection_name);
    // cursor=coll.find(ob);
    cursor=coll.findOne(ob);
    // while(cursor.hasNext()){System.out.println(cursor.next());}
    //


    }catch(Exception e){e.printStackTrace();}
    return cursor;


    }
    }

  • 相关阅读:
    完美解决Python与anaconda之间的冲突问题
    ansible之roles
    ansible剧本之playbook操作
    git从远程仓库拉取内容或向远程仓库上传内容
    python操作excel
    nginx基于uwsgi部署Django
    第二篇:版本控制git之分支
    笔试题92-101
    Django总结
    笔试题70-91
  • 原文地址:https://www.cnblogs.com/luo-mao/p/5872570.html
Copyright © 2020-2023  润新知