i have to find the max value in spring boot with mongoDB. Here is my sample code which i have tried and successfully executed in mongoDB console
db.ecle_audit_transaction.aggregate(
[
{
$group:
{
_id: "$rootId",
logMessageType: { $max: "$logMessageType" }
}
}
]
)
could you please help me how to write this query in spring bootmongoTemplate
Related
I am new to mongo DB with JPA. I have a document object like below:
{
'_id': ObjectId("A"),
customer:[
0: {
id: 21,
order:
{
id:23,
orderName:'orderName1'
}
},
1: {
id: 22,
order:
{
id:12,
orderName:'Electronics'
}
},
]
}
Here I want to get all the orders with orderName starting with "Ele"(example). I've tried:
#Query(value="{'order.orderName':{'$regex':'?0','$options':'i'}}")
#Query("{'order':{'orderName':{'$regex':'?0','$options':'i'}}}")
None of these has worked for me.
Please suggest how to do this.
Demo - https://mongoplayground.net/p/k8Au7Bnmkvg
$regex
db.collection.find({
"customer.order.orderName": {
"$regex": "Ele.+",
"$options": "i"
}
})
you're trying to query order.orderName but as per your schema it's inside the customer object so use customer.order.orderName
How to implement the following Mongo DB query using Spring Data?
db.getCollection('docs').aggregate([
{
$project: {
values: [ "$a", "$b" ]
}
}
])
AggregationOperation projectStage = Aggregation.project()
.andArrayOf("$a","$b").as("values");
db.collection.aggregate([
{$match : { name : "name" } },
{$project: {
name: 1,
sent: {
$size: {
"$ifNull": [ "$audience", [] ]
}
}
}
}]);
How can I do the above mongo aggregation with Spring data?
I know this is an old post and you might have found the answer, but, just for the sake of others, I'm posting it here.
Aggregation aggregation = Aggregation.newAggregation(
.match(Criteria.where("name").is("name"))
.project("name")
.and(ArrayOperators.Size.lengthOfArray(ConditionalOperators.ifNull("audience").then(Collections.emptyList()))).as("sent")
);
I need to count number of records in mongodb distincting by one field.
I use spring data with mongo repository.
Record in mapping collection looks:
{
"_id" : "1",
"productId" : 123,
"variantId" : 321
},
{
"_id" : "2",
"productId" : 123,
"variantId" : 3211
},
{
"_id" : "3",
"productId" : 13,
"variantId" : 32
}
I have tried in many ways and with no results.
#Query(value = "{distinct: 'productId'}", count = true)
Long countDistinctByProductId();
Code above give me always 0
Code below gave me some errors
Long distinctProductId();
Long findDistinctProductId();
Long distinctProductIdByProductId();
In mongo I i found query that gave me record with array of distincted ids, but i cant find how to do it in java.
Mongo Aggregation can provide the functionality that you are looking for
Here is a sample aggregation query in the mongo shell
db.mycollection.aggregate([
{ $group: {_id: '$productId', count: { $sum: 1 } }}
])
Here is a link to the Mongo Java driver documentation and how to use Aggregation
Mongo Java Driver and Aggregation Framework
I am using mongodb java driver thru maven repository (as below in pom.xml) to query transactions between date range with aggregate framwork. The java driver generates following $match that I tried to validate on mongo console and found that it does not work:
db.transactions.aggregate(
{ "$match" :
{
"created_at" : { "$gt" : { "$date" : "2001-04-12T12:00:00.000Z"} , "$lte" : { "$date" : "2020-04-13T12:00:00.000Z"}}
}
}
)
If I remove $date block and replace it with ISOdate function and date string then it seem to be working. I failed to understand why it does not work in java ($match JSON - I had fetched from eclipse to try in mongo console and that do not work as well.)
pom.xml
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>2.11.0</version>
</dependency>
does any one know why $date is not working with aggregate using MongoDB v2.4.0?
You have to format the date before passing onto $match aggregate.
Order.aggregate([
{
$match: {
createdAt: {
$gte: new Date(req.body.startDate),
$lt: new Date(req.body.endDate)
}
}
},
{
$lookup: {
from: 'acbinstallerpayments',
localField: "_id",
foreignField: 'customerObjectID',
as: 'installerPaymentDetails'
}
}
]);
I got it solved by removing the "" & $ prefix on the $date field of in $match.
For you remove the same for $date, $gt & $lte
So that it should look like
db.transactions.aggregate(
{ "$match" :
{
'created_at': {
$gt: "2001-04-12T12:00:00.000Z",
$lt: "2020-04-13T12:00:00.000Z"
}
}
});