"or" condition in embedded document java - java

What is the correct syntax for creating $or condition in embedded document in MongoDB Java driver?
Is it actually possible to get a cursor to embedded documents?
Suppose I have the following document:
{
statuses: [{
{
streamName: A
}{
statusA: 0
}{
statusB: 1
}
},
{
{
streamName: B
}{
statusA: 0
}{
statusB: 1
}
}]
}
I would also like to get cursor to sub documents (in array of statuses) that has at least one status bigger than 0.
This is how I did it but it didn't work:
List<BasicDBObject> obj = new ArrayList<BasicDBObject>();
DBObject query = new BasicDBObject();
obj.add(new BasicDBObject ("statuses",
new BasicDBObject ("statusA",
new BasicDBObject ("$gt",0 ) )));
obj.add(new BasicDBObject ("statuses",
new BasicDBObject ("statusB" ,
new BasicDBObject ("$gt",0 ) )));
query.put("$or",obj)
db.find(collectionName,query)
I didn't find any documentation on that.

What you have translates to
{ "$or" : [ { "statuses" : { "statusA" : { "$gt" : 0}}} , { "statuses" : { "statusB" : { "$gt" : 0}}}]}
which is used for whole document comparison.
For comparing fields inside an embedded arrays/doc you've to use dot notation.
{ "$or" : [ { "statuses.statusA" : { "$gt" : 0}} , { "statuses.statusB" : { "$gt" : 0}}]}
The equliavent java code is below
List<BasicDBObject> obj = new ArrayList<BasicDBObject>();
DBObject query = new BasicDBObject();
obj.add(new BasicDBObject ("statuses.statusA", new BasicDBObject ("$gt",0 ) ));
obj.add(new BasicDBObject ("statuses.statusB" , new BasicDBObject ("$gt",0 ) ));
query.put("$or",obj);
Alternatively you can use $elemMatch to run matches on embedded arrays. Similar to what you've but $elemMatch applies condition to each fields.
Something like
{ "statuses" : { "$elemMatch" : { "$or" : [ { "statusA" : { "$gt" : 0}} , { "statusB" : { "$gt" : 0}}]}}}
Java Code
BasicDBList obj = new BasicDBList();
obj.add(new BasicDBObject ("statusA",new BasicDBObject ("$gt",0 ) ));
obj.add(new BasicDBObject ("statusB",new BasicDBObject ("$gt",0 ) ));
DBObject query = new BasicDBObject("statuses", new BasicDBObject("$elemMatch", new BasicDBObject("$or",obj)));
Count the no of matching occurrences.
Bson count = new Document("statuses", Document.parse("{$size:{\n" +
" $filter: {\n" +
" input: \"$statuses\",\n" +
" as: \"status\",\n" +
" cond: { \"$or\" : [ {$gt:[\"$$status.statusA\", 0]} , {$gt:[\"$$status.statusB\", 0]}]}\n" +
" }\n" +
" }}"));
Bson project = new Document("$project", count);
col.aggregate(Arrays.asList(project));

Related

How to pass list of generic fields with their corresponding first operator to Aggregation.group method

I want to write a generic code for aggregation using mongoTempalte and Aggregation.group() method. So I have the problem of passing generic fields into the group method with the first() operator
here is my demo native query as follows:
db.subscriberProfile.aggregate([{"$unwind":"$usage_history"},
{ "$group" : { "_id" :"$_id" ,"birthdate" : { "$first":"$birthdate"} , "category" : { "$first":"$category"} , "control_group" : { "$first":"$control_group"} , "sumOfTotalUsage" : { "$sum" :{"$cond": [ { "$gte" :[ "$usage_history.date" , ISODate( "2017-01-13T10:43:55.306Z")] }, "$usage_history.total_usage", 0]}}}},
{ "$match" : { "$and" : [ { "birthdate" : { "$lte" : ISODate( "2017-07-12T10:43:55.306Z")}} , { "birthdate" : { "$gte" : ISODate( "1917-07-12T10:20:35.306Z")}} , { "category" : { "$in" : [ "Prepaid"]}} , { "control_group" : false} , { "sumOfTotalUsage" : { "$gte" : 0}}]}}])
And here is my Sample code in Java.
UnwindOperation unwind = Aggregation.unwind("usage_history");
GroupOperation group = Aggregation.group(fields.toArray(new String[fields.size()])).sum("usage_history.total_usage").as("sumOfTotalUsage");
I just want to know how to add multiple fields in group operation with $first operator.
So, Is there any way to pass list of fields with list of first operator to the group method.
Thanks,
Try this code,I hope this will help you
UnwindOperation unwind = Aggregation.unwind("usage_history");
BasicDBObject object = new BasicDBObject("_id", "$_id");
for (String string : fields) {
object.append(string, new BasicDBObject("$first", "$" + string));
}
object.append("total", new BasicDBObject("$sum", new BasicDBObject("$cond",
new Object[] { new BasicDBObject("$gte", new Object[] { "$usage_history.date", calendarMin.getTime() }),
"$usage_history.total_usage", 0 })));
BasicDBObject groupObject = new BasicDBObject("$group", object);
DBObject groupOperation = (DBObject) groupObject;
MatchOperation matchMain = Aggregation
.match(new Criteria().andOperator(criteriaList.toArray(new Criteria[criteriaList.size()])));
Aggregation aggregation = Aggregation.newAggregation(unwind, new CustomGroupOperation(groupOperation),
matchMain);

How do I group the results of mongodb query and implement in Java?

I have a collection in a Mongo database that looks like this:
{
"_id" : ObjectId("561b42d4e4b0d4227d011d2c"),
"product_related_data" : {
"depth" : 6,
"height" : 23,
"product_barcode" : "54491472",
"product_name" : "xyz product",
"product_uuid" : "009b9846-b3ad-49f7-a7a0-d35a04f83480",
"width" : 6
},
"sensostrip_data" : {
"barcode" : "130150208299",
"battery_level" : 2.894
},
"stock_related_data" : {
"calculated_max_product_percentage" : 15.625,
"calculated_min_product_percentage" : 12.5,
"current_stock" : 2,
"max_stock" : 6,
"stock_difference_percentage" : 0,
"stock_difference_value" : 0,
"stock_percentage" : 37.5
},
"store_data" : {
"city_uuid" : "cbb4dfe8-172b-11e4-a1f0-00163ed23ec2",
"ip" : "10.0.1.1",
"retailer_uuid" : "8c33c32c-5903-11e4-a1f0-00163ed23ec2",
"store_name" : "xyz store",
"store_uuid" : "15a6cc90-081f-11e5-b213-001e6745ff8d"
},
"time" : {
"check_date" : "2015-10-11 11:53:55",
"previous_check_date" : "2015-10-11 11:48:57"
},
"id" : "6be54bef-0aa3-456c-b912-1731f8154e7d"
}
The mongo query I'm currently executing returns all the documents for a list of store_uuid's and for a product_uuid, and looks like this:
db.readings
.find({ $and:[ {"store_data.store_uuid": {$in:["15a6cc90-081f-11e5-b213-001e6745ff8d","217b983b-5904-11e4-a1f0-00163ed23ec2","5337d78d-5904-11e4-a1f0-00163ed23ec2"]}}
,{"product_related_data.product_uuid": "f44aa29d-09ce-4902-bf12-d45d44b3dfd0"}]})
My current Java implementation (where I make use of projection) looks like this:
DBCollection table = databaseConncetion().getCollection("readings");
BasicDBObject sensorReturn = new BasicDBObject("sensostrip_data.barcode",1);
BasicDBObject clause1 = new BasicDBObject("store_data.store_uuid", new BasicDBObject("$in", StoreIds));
BasicDBObject clause2 = new BasicDBObject("product_related_data.product_uuid", productId);
BasicDBList and = new BasicDBList();
and.add(clause1);
and.add(clause2);
DBObject query = new BasicDBObject("$and", and);
DBCursor cursor = table.find(query, sensorReturn
.append("stock_related_data.stock_percentage",1)
.append("store_data.store_uuid",1)
.append("time.check_date", 1))
.sort(new BasicDBObject("time.check_date", -1))
.limit(100);
However I need this query to group the results for the latest check_date by barcode
The aggregation framework is at your disposal. Running the following aggregation pipeline will give you the desired result:
Mongo shell:
pipeline = [
{
"$match": {
"store_data.store_uuid": {
"$in": [
"15a6cc90-081f-11e5-b213-001e6745ff8d",
"217b983b-5904-11e4-a1f0-00163ed23ec2",
"5337d78d-5904-11e4-a1f0-00163ed23ec2"
]
},
"product_related_data.product_uuid": "f44aa29d-09ce-4902-bf12-d45d44b3dfd0"
}
},
{ "$sort": { "time.check_date": -1 } },
{
"$group": {
"_id": "$sensostrip_data.barcode",
"stock_percentage": { "$first": "$stock_related_data.stock_percentage" },
"store_uuid": { "$first": "$store_data.store_uuid" },
"check_date": { "$first": "$time.check_date" }
}
},
{ "$limit": 100 }
];
db.readings.aggregate(pipeline);
Java test implementation
public class JavaAggregation {
public static void main(String args[]) throws UnknownHostException {
MongoClient mongo = new MongoClient();
DB db = mongo.getDB("test");
DBCollection coll = db.getCollection("readings");
// create the pipeline operations, first with the $match
DBObject match = new BasicDBObject("$match",
new BasicDBObject("store_data.store_uuid", new BasicDBObject("$in", StoreIds))
.append("product_related_data.product_uuid", productId)
);
// sort pipeline
DBObject sort = new BasicDBObject("$sort",
new BasicDBObject("time.check_date", -1)
);
// build the $group operations
DBObject groupFields = new BasicDBObject( "_id", "$sensostrip_data.barcode"); // group by barcode
groupFields.put("stock_percentage", new BasicDBObject( "$first", "$stock_related_data.stock_percentage")); // get the first when ordered documents are grouped
groupFields.put("store_uuid", new BasicDBObject( "$first", "$store_data.store_uuid"));
groupFields.put("check_date", new BasicDBObject( "$first", "$time.check_date"));
// append any other necessary fields
DBObject group = new BasicDBObject("$group", groupFields);
// limit step
DBObject limit = new BasicDBObject("$limit", 100);
// put all together
List<DBObject> pipeline = Arrays.asList(match, sort, group, limit);
AggregationOutput output = coll.aggregate(pipeline);
for (DBObject result : output.results()) {
System.out.println(result);
}
}
}

Remove [ "" : from mongoDb result using Java

To remove the "_id" from the mongo result I use :
DBObject allQuery = new BasicDBObject();
DBObject removeIdProjection = new BasicDBObject("_id", 0);
data.addAll(collection.find(allQuery , removeIdProjection).toArray());
The results of this query is :
{ "" : { [
{
"test1" : "test1"
{
}]}
How to remove { "" : so result is of format :
[
{
"test1" : "test1"
}
]
You are trying to put result in json object which add extra brackets here.
toArray() converts type cursor to list so you need to store it in list. You can iterate this list to access elements. You should use following code to get expected result:
DBObject allQuery = new BasicDBObject();
DBObject removeIdProjection = new BasicDBObject("_id", 0);
List cursor = collection.find(allQuery , emoveIdProjection).toArray();
System.out.println("result: " + cursor);

update in sharded environment with JAVA

I am trying to update a filed 'Content' of a document in sharded environment. My shard key is 'asset' which is included in the query. But I am getting error saying
"com.mongodb.WriteConcernException: { "serverUsed" :
"/10.102.10.190:27017" , "err" : "update { q: { $and: [ { asset:
\"/1/01/01.m3u8\" }, { recList: \"REC-1-1418208180000-1418208300000\"
} ] }, u: { $set: { Content: BinData } }, multi: false, upsert: false
} does not contain _id or shard key for pattern { asset: 1 }" , "code"
: 61 , "n" : 0 , "shards" : [ ] , "shardRawGLE" : { } , "ok" : 1.0} "
BasicDBObject newDocument = new BasicDBObject();
newDocument.append("$set", new BasicDBObject().append("Content", content));
BasicDBObject andQuery = new BasicDBObject();
List<BasicDBObject> obj = new ArrayList<BasicDBObject>();
obj.add(new BasicDBObject("asset", assetf));
obj.add(new BasicDBObject("recList", recId));
andQuery.put("$and", obj);
WriteResult result = collection.update(andQuery, newDocument);
Please help me.

How to calculate unique elements in collections?

I have collection like
id : 1, url : youtube.com
also one url can be many times,
I need get whole collections and count unique elements
like
youtube 10
google 8
lycos 5
here is the code
public List<URLEntity> findAll() {
List<URLEntity> list = new ArrayList<URLEntity>();
String sort = "searchDate";
String order = "desc";
DBObject sortCriteria = new BasicDBObject(sort, "desc".equals(order) ? -1 : 1);
BasicDBObject query = new BasicDBObject();
DBCursor cursor = mongoCoreService.getDomainCollection().find(query).sort(sortCriteria);
try {
while (cursor.hasNext()) {
DBObject document = cursor.next();
URLEntity entity = new URLEntity();
entity = Converter.toObject(URLEntity.class, document);
list.add(entity);
}
} finally {
cursor.close();
}
return list;
}
thanks
You can achieve this by using the aggregate framework from MongoDB.
db.yourcollection.aggregate({ $group: { _id: '$url', total: {$sum: 1} }})
You'll obtain something like this, that you can manipulate in JAVA:
{
"result" : [
{
"_id" : "youtube.com",
"total" : 10
},
{
"_id" : "google.com",
"total" : 8
},
{
"_id" : "lycos",
"total" : 5
}
],
"ok" : 1
}
Today I found and test answer in java, here is a code
DBCollection mycoll= db.getCollection("domain");
DBObject fields = new BasicDBObject("domain", 1);
DBObject project = new BasicDBObject("$project", fields );
// Now the $group operation
DBObject groupFields = new BasicDBObject( "_id", "$domain");
groupFields.put("total", new BasicDBObject( "$sum", 1));
DBObject group = new BasicDBObject("$group", groupFields);
// run aggregation
AggregationOutput output = mycoll.aggregate(project, group);
System.out.println(output.getCommand().toString());
for (DBObject dbObject : output.results()) {
System.out.println(dbObject);
}

Categories