王潇舶吧 关注:6贴子:383
  • 5回复贴,共1
Scanner sb = new Scanner(System.in);
System.out.print("insert or get?(i/g):");
String want = sb.nextLine();
if (want.equals("i")){
System.out.print("this is insert:");
System.out.print("please input tableName:");
String tableName = sb.nextLine();
System.out.print("please input rowKey:");
String rowKey = sb.nextLine();
System.out.print("please input colFamily :");
String colFamily = sb.nextLine();
System.out.print("please input col (if no col,it can null):");
String col = sb.nextLine();
System.out.print("please input val:");
String val = sb.nextLine();
insertRow( tableName, rowKey, colFamily, col,val);
}
else{
System.out.println("this is getData:");
System.out.print("please input tableName:");
String tableName1 = sb.nextLine();
System.out.print("please input rowKey:");
String rowKey1 = sb.nextLine();
System.out.print("please input colFamily :");
String colFamily1 = sb.nextLine();
System.out.print("please input col (if no col,it can null):");
String col1 = sb.nextLine();
getData( tableName1, rowKey1, colFamily1, col1);
}
sb.close();


IP属地:辽宁1楼2018-04-04 11:31回复
    傻哔


    来自Android客户端2楼2018-04-12 23:14
    回复


      来自Android客户端4楼2018-04-16 20:19
      回复
        import java.util.ArrayList;
        import java.util.List;
        import java.util.Scanner;
        import org.bson.Document;
        import com.mongodb.MongoClient;
        import com.mongodb.client.MongoCollection;
        import com.mongodb.client.MongoCursor;
        import com.mongodb.client.MongoDatabase;
        import com.mongodb.client.model.Filters;
        public class MongoDB {
        /**
        * @param args
        */
        public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (true) {
        System.out.println("1.插入 2.查询");
        System.out.println("------请选择------");
        switch (sc.nextInt()) {
        case 1:
        insert();
        break;
        case 2:
        find();
        break;
        default:
        break;
        }
        }
        // insert();// 插入数据。执行插入时,可将其他三句函数调用语句注释,下同
        // find(); // 查找数据
        // update();//更新数据
        // delete();//删除数据
        }
        /**
        * 返回指定数据库中的指定集合
        *
        * @param dbname
        * 数据库名
        * @param collectionname
        * 集合名
        * @return
        */
        // MongoDB无需预定义数据库和集合,在使用的时候会自动创建
        public static MongoCollection<Document> getCollection(String dbname,
        String collectionname) {
        // 实例化一个mongo客户端,服务器地址:localhost(本地),端口号:27017
        MongoClient mongoClient = new MongoClient("localhost", 27017);
        // 实例化一个mongo数据库
        MongoDatabase mongoDatabase = mongoClient.getDatabase(dbname);
        // 获取数据库中某个集合
        MongoCollection<Document> collection = mongoDatabase
        .getCollection(collectionname);
        return collection;
        }
        /**
        * 插入数据
        */
        public static void insert() {
        try {
        // 连接MongoDB,指定连接数据库名,指定连接表名。
        MongoCollection<Document> collection = getCollection("School", "student"); // 数据库名:school 集合名:student
        // 实例化一个文档,文档内容为{sname:'Mary',sage:25},如果还有其他字段,可以继续追加append
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你的姓名:");
        String name = sc.nextLine();
        System.out.println("请输入你的项目名称:");
        String title = sc.nextLine();
        System.out.println("请输入你的工作分配:");
        String assignment = sc.nextLine();
        System.out.println("请输入你的权重:");
        String weight = sc.nextLine();
        System.out.println("请输入你的成绩:");
        String score = sc.nextLine();
        Document docx = new Document("assignment", assignment).append("weight", weight).append("score", score);
        Document doc = new Document("sname", name).append("sTitle", title)
        .append("Details", docx);
        List<Document> documents = new ArrayList<Document>();
        // 将doc加入到documents列表中
        documents.add(doc);
        // 将documents插入集合
        collection.insertMany(documents);
        System.out.println("插入成功");
        } catch (Exception e) {
        System.err.println(e.getClass().getName() + ": " + e.getMessage());
        }
        }
        /**
        * 查询数据
        */
        public static void find() {
        try {
        MongoCollection<Document> collection = getCollection("School", "student"); // 数据库名:school 集合名:student
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你的姓名:");
        String name = sc.nextLine();
        // 通过游标遍历检索出的文档集合
        MongoCursor<Document> cursor= collection.find(new Document("sname", name)).projection(new Document("Details",1)).iterator();
        // //find查询条件:sname='Mary'。projection筛选:显示sname和sage,不显示_id(_id默认会显示)
        // 查询所有数据
        // MongoCursor<Document> cursor = collection.find().iterator();
        while (cursor.hasNext()) {
        System.out.println(cursor.next().toJson());
        }
        } catch (Exception e) {
        System.err.println(e.getClass().getName() + ": " + e.getMessage());
        }
        }
        }


        IP属地:辽宁5楼2018-04-23 21:20
        回复
          高原红


          IP属地:辽宁来自iPhone客户端6楼2018-05-25 16:18
          收起回复