如何使用Java询问mongoDB数据?

时间:2019-05-18 21:44:55

标签: java mongodb

我需要用Java编程OLAP多维数据集,并且我有一个mongoDB数据库,我想使用此请求查询日期:

“从区域= afriqua的客户中选择保管员” 问题是如何使用Java指令从全局集合“多维数据集”中包含的“客户”集合中获取数据?

PS:我想获取查询的结果。

我在Java中使用NetBeans IDE 8.2进行编码,在MOngoDB 3.11.0中使用我的NoSQL数据库。

{ "_id" : ObjectId("5cdc6510af5e07bbde44cade"), 
  "LineOrder" : [ 
     { "CustKey" : 10, "SupKey" : 2, "PartKey" : 1360, "DateKey" : 200124, "Quantity" : 201578, "Tax" : 700 }, 
     { "Orderkey" : 20165, "Linenumber" : 5487, "Custkey" : 12, "Partkey" : 102, "Supkey" : 3, "Orderdate" : "May14,2016", "Shippriority" : 40, "Quantity" : 105, "Extebdedprice" : 1480620, "Ordertotalprice" : 11689695, "Discount" : 21, "Revenue" : 95981, "Supplycost" : 4, "Tax" : 198754, "Commdate" : 201647, "shipmode" : "mail" } 
  ] }
{ "_id" : ObjectId("5cdc653eaf5e07bbde44cae3"), 
  "Date" : [ 
    { "DateKey" : 201948, "Date" : "April8,2019", "Dayofweek" : "Monday", "Month" : "April", "Year" : 2019, "Yearmonthnum" : 20194, "Yearmonth" : "Apr2019", "Daynuminweek" : 2, "Daynuminmonth" : 8, "Daynuminyear" : 98, "Monthnuminyear" : 4, "Weeknuminyear" : 15, "Lastdayinweekfl" : 2, "Lastdayinmonthfl" : 3, "Sellingseason" : "winter", "Holidayfl" : 1, "Weekdayfl" : 1 }, 
    { "DateKey" : 201965, "Date" : "May21,2019", "Dayofweek" : "Thusdey", "Month" : "May", "Year" : 2019, "Yearmonthnum" : 20195, "Yearmonth" : "May2019", "Daynuminweek" : 3, "Daynuminmonth" : 15, "Daynuminyear" : 101, "Monthnuminyear" : 5, "Weeknuminyear" : 21, "Lastdayinweekfl" : 2, "Lastdayinmonthfl" : 3, "Sellingseason" : "spring", "Holidayfl" : 2, "Weekdayfl" : 1 } 
] }
{ "_id" : ObjectId("5cdc6550af5e07bbde44cae8"), 
  "customer" : [ 
    { "CustKey" : 5, "Name" : "Aleksender Bill", "Address" : "jFKRE3kiytrdf", "citys" : [ { "city" : "Vietnam xyz", "Nation" : "Vietnam", "Region" : "Asia" } ] }, 
    { "CustKey" : 10, "Name" : "Mohamed Dawed", "citys" : [ { "City" : "Draria", "Nation" : "Algeria", "Region" : "Afriqua" } ] }, 
    { "CustKey" : 12, "Name" : "George", "Nation" : "Canada" } 
] }
{ "_id" : ObjectId("5cdc6563af5e07bbde44caed"), 
  "part" : [ 
    { "PartKey" : 1360, "Name" : "khaki chocolat", "Mfgr" : "mfgr#5", "size" : 31, "Color" : "medieum", " Categories" : [ { "Category" : "mfgr#56", "type" : "standar burnishe steel", "container" : "jumbo case" } ] }, 
    { "PartKey" : 1400, "Name" : "zzd nnn", "Mfgr" : "fgr#8", "size" : 10, "Categories" : [ { "Category" : "fgr#10", "type" : "xxxx", "container" : "jumbo" } ] }, 
    { "PartKey" : 102, "Color" : "edieum", "Categories" : [ { "Category" : "fgr#56", "type" : "yyy", "container" : "jj ccc" } ] } 
] }

我尝试使用此代码,但是不起作用

import com.mongodb.MongoClient;
import com.mongodb.*;

public class Mongoconn {

    public static void main(String[] args) {
        // connect java to mongoDB
        MongoClient mongoClient = new MongoClient("localhost", 27017);
        System.out.println("server connection successfully done");
        //choisir la base de données 
        DB dbs=mongoClient.getDB("ssb");
        System.out.println("connected to database:"+dbs.getName());
        // spécifier la collection
        DBCollection coll= dbs.getCollection("cube");



           DBCursor cursor = coll.find(); 
                    while(cursor.hasNext()) {
          int i=1;
          System.out.println(cursor.next());
          i++;
   }
                    System.out.println("********************************************* ");
                    // Select Where region = afriqua

                     BasicDBObject query = new BasicDBObject("Region", "Afriqua");
                     DBCursor curs = coll.find(query);
           try {
   while(cursor.hasNext()) {
       int i=1;
           DBObject obj = curs.next();
           System.out.println(obj.get("Region") + " => " + obj.get("info"));
           i++;
   }
} finally {
   curs.close();
}
    }
}

0 个答案:

没有答案