how to interrogate mongoDB data using java? - java

I need to program an OLAP cube in java, and i have a mongoDB DataBase, i want to interrogate my date using this request:
"select custkey from customer where region=afriqua"
the question is how can i use java instructions to get my data from the collections "customer" which is included in the global collection "cube"
PS: I want to get the resultat of the query.
i'm using a NetBeans IDE 8.2 for codding in java ,and my NoSQL database in MOngoDB 3.11.0.
{ "_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" } ] }
] }
I try whith this code,but it doesn't work
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();
}
}
}

Related

mongo $near command on Nested Array

{
"merchantId" : "M168976258",
"catalogTypeId" : "catalogTypeProduct",
"items" : [
{
"name" : "Product 1",
"location" : {
"type" : "Point",
"coordinates" : [
0,
0
]
}
},
{
"name" : "Product 2",
"location" : {
"type" : "Point",
"coordinates" : [
0,
0
]
}
},
{
"name" : "Product 3",
"location" : {
"type" : "Point",
"coordinates" : [
0,
0
]
}
}
}
I able to Perfrom mongo near command on document which is having single location
using below command
db.abc.find({
location :{
$near : {
$geometry : {
index : "Point" ,
coordinates : [19.1, 72.89]
},
$maxDistance : 10000
}
}
})
but i'm not able to perform on document which having nested array can anybody help me out to find out problem
By running this query i get following error
db.catalogForAdminAndMerchant.find({
"items.location" :{
$near : {
$geometry : {
index : "Point" ,
coordinates : [19.1, 72.89]
},
$maxDistance : 10000
}
}
})
o/p is
planner returned error: unable to find index for $geoNear query
but have created the index
by running db.catalogForAdminAndMerchant.getIndexes() i get
/* 1 */
{
"v" : 1,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "catalog-db.catalogForAdminAndMerchant"
},
/* 2 */
{
"v" : 1,
"key" : {
"_fts" : "text",
"_ftsx" : 1
},
"name" : "CatalogForAdminAndMerchant_TextIndex",
"ns" : "catalog-db.catalogForAdminAndMerchant",
"weights" : {
"items.name" : 3
},
"default_language" : "english",
"language_override" : "language",
"textIndexVersion" : 3
},
/* 3 */
{
"v" : 2,
"key" : {
"address.items[i].location" : "2dsphere"
},
"name" : "address.items[i].location_2dsphere",
"ns" : "catalog-db.catalogForAdminAndMerchant",
"2dsphereIndexVersion" : 3
}

mongodb lookup function returns an empty array result

I have two mongodb collections employee and department as follows
employee collection:
{
"_id" : ObjectId("58c4a35ac2e604024321788e"),
"id_employee" : 19,
"employee_name" : "Lene Vestergaard Hau",
"employee_address" : "Allerton",
"hours" : 279,
"id_department" : 1,
"projects" : [
285,
453,
499,
804,
956
],
"Children" : [
{
"id_child" : 38,
"child_name" : "Caroline Herschel"
}
]
}
department collection:
{
"_id" : ObjectId("58c49c48c2e669d6555aa5a7"),
"id_department" : 1,
"department_name" : "dept1",
"projects" : [
{
"id_project" : 285,
"project_name" : "1project 285",
"duration" : "211"
},
{
"id_project" : 453,
"project_name" : "1project453",
"duration" : "214"
},
{
"id_project" : 499,
"project_name" : "1project499",
"duration" : "224"
},
{
"id_project" : 804,
"project_name" : "1project804",
"duration" : "217"
}
{
"id_project" : 956,
"project_name" : "1project956",
"duration" : "217"
}
]
I am trying to perform a lookup function to get all the details about the
projects for the employee. The java code I am using :
coll.aggregate(Arrays.asList(
Aggregates.match(Filters.eq("id_employee",19)),
Aggregates.unwind("$projects"),
Aggregates.lookup("department", "projects", "projects.id_project", "lookupData")
)).forEach(printBlock);
}
the code above return lookupData as an empty list. Can any one help please?

Elasticsearch aggregation return always empty buckets [] (elasticsearch version 2.4.1)

I have the following code:
final String index = ElasticSearchUtils.getIndexNameForExecution(queryId);
SearchRequestBuilder query = client.prepareSearch(index);
query.setTypes(indexType.toString());
query.addAggregation(terms("errors").field("code").size(NUMBER_OF_HITS).order(Terms.Order.count(false)));
int pageStart = getFrom(page) * size;
SearchResponse response = query.setFrom(pageStart).setSize(getPageSize(size)).execute().actionGet();
return response.toString();
and part of response is:
{
"took" : 78,
"timed_out" : false,
"_shards" : {
"total" : 2,
"successful" : 2,
"failed" : 0
},
"hits" : {
"total" : 2,
"max_score" : 1.0,
"hits" : [ {
"_index" : "index_587e1e34e4b040c63c49137f",
"_type" : "ERROR",
"_id" : "AVmspgsKa7ZIkZu1p32G",
"_score" : 1.0,
"_source" : {
"agent_id" : "{8668b249-9443-e611-87c6-005056aa41d1}",
"_v" : "1",
"host" : "RHEL65-X86-DEMO",
"created_at" : "2017-01-17T13:37:58.496Z",
"qid" : "587e1e34e4b040c63c49137f",
"errors" : [ {
"code" : 769,
"module" : "FileHashing",
"function" : "FindFiles"
} ]
}
}, {
"_index" : "index_587e1e34e4b040c63c49137f",
"_type" : "ERROR",
"_id" : "AVmspgsKa7ZIkZu1p32H",
"_score" : 1.0,
"_source" : {
"agent_id" : "{7238f027-fbfc-47cf-85b0-c69838e26a2a}",
"_v" : "1",
"host" : "W8-X64-DEMO",
"created_at" : "2017-01-17T13:37:58.501Z",
"qid" : "587e1e34e4b040c63c49137f",
"errors" : [ {
"code" : 769,
"module" : "FileHashing",
"function" : "FindFiles"
} ]
}
} ]
},
"aggregations" : {
"errors" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [ ]
}
}
}
And the result of search execution is:
As you can see the buckets is empty [], aggregation is not working, but any exception is throwing. This is occurring with elasticsearch 2.4.1, the same code is working with elasticsearch 1.4.1
You simply need to fix this line
query.addAggregation(terms("errors").field("errors.code").size(NUMBER_OF_HITS).order(Terms.Order.count(false)));
^
|
add this

How to use MongoDB $let with Spring-Data?

I have a MongoDB collection of places. A typical place has most of the following fields:
{
"_id" : ObjectId("575014dc6b028f07bef53681"),
"_class" : "domain.model.PlaceV1",
"name" : "Γιασεμί",
"primary_photo_url" : "https://irs0.4sqi.net/img/general/original/34666238_STHSh6CHiC7hpAuB4rztRVg6cFc5ylfi15aRaR7zUuQ.jpg",
"seenDetails" : NumberLong(0),
"foursquare_checkins" : 646,
"foursquare_tips" : 28,
"keywords" : [
""
],
"verified" : 1,
"location" : {
"loc" : {
"type" : "Point",
"coordinates" : [
25.898318,
36.831486
]
},
"formattedAddress" : "Χώρα",
"locality" : "Amorgos",
"first_neighbourhood" : "Katapola",
"greek_locality" : "Αμοργός",
"greek_first_neighbourhood" : "Κατάπολα"
},
"contact" : {
"phone_numbers" : [
"+30 2285 074017"
]
},
"price" : {
"priceVotes" : NumberLong(0),
"price" : 0,
"priceVotesSum" : NumberLong(0)
},
"rating" : {
"rating" : 8,
"ratingVotes" : NumberLong(0),
"ratingVotesSum" : NumberLong(0)
},
"categories" : [
{
"cat_id" : NumberLong(10310061000),
"category" : "Café",
"greek_category" : "Καφετέρια",
"weight" : 4
},
{
"cat_id" : NumberLong(11610021000),
"category" : "Bar",
"greek_category" : "Μπαρ",
"weight" : 4
}
]
}
I want to make queries where the sorting will be based on a score that is a result of some expressions and conditions. From the mongo shell I have tried this:
db.place.aggregate([
{$match:{"location.locality":"Athens"}},
{$project:
{name:1, location:1, score:{
$let: {
vars:{ foursquare: {
$cond: { if: { $gte: [ "$foursquare_checkins", 500 ] }, then: 500, else: "$foursquare_checkins" }
},
rating: {$multiply:["$rating.rating", 100]},
},
in:{$add:["$$foursquare", "$$rating", "$seenDetails"]}
}
}
}
},
{$sort: {score: -1}}]).pretty();
This is a simple example of my queries. The score will contain more complex expressions like the distance from a location. The problem is that I cannot find a way to use the $let and the $cond operator in my Java code with Spring. Could anybody help?
You should be able to do this using nested DBObject and a Custom Aggregation Operation.
For Example:
Map operations = new HashMap();
operations.put("name", 1);
operations.put("location", 1);
operations.put("score", new BasicDBObject("$let", new BasicDBObject("vars", new BasicDBObject())));
Then you can create a CustomAggregationOperation to add this to your project
CustomAggregationOperation project = new CustomAggregationOperation(new BasicDBObject("$project", operation));
This will give you the following pipeline:
{ "$project" : { "score" : { "$let" : { "vars" : { }}} , "name" : 1 , "location" : 1}}
Then you can add your other stages:
Aggregation aggregate = Aggregation.newAggregation(match, project, sort);
public class CustomAggregationOperation implements AggregationOperation {
private DBObject operation;
public CustomAggregationOperation (DBObject operation) {
this.operation = operation;
}
#Override
public DBObject toDBObject(AggregationOperationContext context) {
return context.getMappedObject(operation);
}
}

How to get object from MongoDb using criteria api in java

I want to get result where varifiedDate inside secondaryParents is not 0. How can I achieve this?
DB Structure:
{
"_id" : ObjectId("577a151859defb33c0f8bf4a"),
"_class" : "com.nv.tracker.db.model.User",
"parentId" : NumberLong(1000004),
"userId" : NumberLong(1000005),
"firstName" : "Pranav",
"lastName" : "Rathore",
"addr" : {
"address" : "",
"landmark" : "",
"city" : "",
"state" : "",
"country" : ""
},
"email" : "ashish.dubey#newvisionsoftware.in",
"mobile" : "7879066069",
"createdDate" : ISODate("2016-07-04T07:49:44.000Z"),
"activeStatus" : false,
"imagePath" : "http://172.20.0.210:8080/nvtracker/rest/resource/image/1000005",
"deviceId" : "78 79 066069",
"isSuperUser" : false,
"dob" : "1468348200000",
"checkAndriodOrIos" : 0,
"isLogin" : false,
"otp" : 0,
"batteryLevel" : 30,
"locationInterval" : 60,
"deviceSpeed" : 45,
"primaryMobile" : "9039101994",
"secondaryMobile1" : "9039101994",
"secondaryMobile2" : "",
"primaryEmail" : "ashish.dubey#newvisionsoftware.in",
"secondaryEmail1" : "abb#hh.con",
"secondaryEmail2" : "",
"isVerified" : false,
"currentBatteryLevel" : 0,
"currentSpeed" : 0,
"subscriptionExpDate" : NumberLong(1498132426000),
"enableNotification" : 1,
"enableTakeOffAlert" : 0,
"deleted" : false,
"hasExternalDevice" : false,
"secondaryParents" : {
"1000003" : {
"secondaryparentId" : NumberLong(1000003),
"varifiedDate" : NumberLong(14676286792586),
"addedDate" : NumberLong(1467628679286),
"secreteCode" : "18716"
},
"1000004" : {
"secondaryparentId" : NumberLong(1000004),
"varifiedDate" : NumberLong(0),
"addedDate" : NumberLong(1467628679286),
"secreteCode" : "18716"
}
}
}
I want to get the user where varifiedDate inside secondaryParents is not 0.
Please suggest how I can achieve this. Also provide the best tutorial for 'mongodb using java' with criteria API.

Categories

Resources