Please see: Mongo Playground for the data and query.
Trying to figure out the equivalent SpringData MongoDB query for:
"idPlusVals": {
"values": {
"$slice": [
"$sa",
0,
4
]
}
}
Have tried the following but could not get the $slice in:
project().and("idPlusCount").nested(fields("totalCount"))
.and("idPlusVals").nested(bind("values", "sa"));
Related
I am using the $project stage in an aggregation pipeline using Pymongo on AWS DocumentDB, like so:
{ '$project': { 'foo': 0 } }
This results in the following error:
$projection requires at least one output field
As far as I can tell, the mongo docs state that as of Mongo version 3.4 you can use $project to specify exclusions of fields (see: https://www.mongodb.com/docs/v4.0/reference/operator/aggregation/project/). The example they provide looks like:
db.books.aggregate( [ { $project : { "lastModified": 0 } } ] )
Does docdb not match this part of the API spec? or am I missing something?
EDIT: I'm also unable to use the _id filter specifically, which is also highlighted in mongo docs.
{'$project': {'_id': 0}}
Excluding just _id is not currently supported, i.e. { '$project': { '_id': 0 } }
But, projecting a non existing field can enforce exclusion: { '$project': { '_id': 0, 'foo_field': 0 } }
I am trying to implement a custom sort with MongoDB and Spring Data according to Asya Kamsky's post:
List<AggregationOperation> operations = new ArrayList<>();
operations.add(Aggregation.addFields().addField("scorrrz")
.withValueOfExpression("{ \"$indexOfArray\" : [ [\"John\", \"Bill\"], \"$name\" ] }").build());
When I try to execute this, I get:
ERROR a.insurance.misc.ErrorAttributes - /api/v1/insurance/opportunity/all
org.springframework.expression.spel.SpelParseException: Expression [{ "$indexOfArray" : [ ["John", "Bill"], "$name" ] }] #29: EL1043E: Unexpected token. Expected 'rsquare(])' but was 'comma(,)'
Is this not the right syntaxt? How can this be done with Spring Data?
Collection<String> nameList = Arrays.asList("John", "Bill");
Aggregation agg = newAggregation(
addFields()
.addField("scorrrz").withValue(arrayOf(nameList).indexOf("$name"))
.build()
);
The aggregation's projection is an $addFields stage with a $indexOfArray aggregation array operation. This will return a field scorrrz, and it will have index value or -1 when there is no match. This ran okay with Spring Boot v2.3.10 and MongoDB v4.2.8.
The run this aggregation pass the pipeline agg to the MongoTemplate#aggregate method.
If you want to calculate $indexOfArray using the array from the DB document itself, you need the aggregate pipeline to perform the calculation instead of the Java code.
Here is my solution
Aggregation.addFields()
.addField("scorrrz")
.withValueOf(MongoExpression.create(" $indexOfArray : [ [ 'John', 'Bill' ], '$name' ]"))
.build()
This creates the following pipeline stage
{
"$addFields": {
"scorrrz": {
"$indexOfArray": [
[
"John",
"Bill"
],
"$name"
]
}
}
}
I am using Azure Cosmos MongoDB and storing API Request logs. I wanted to know how many request were made per second. So I am grouping them on basis of timestamp after dividing the timestamp by 1000 then rounding off with 0 decimal places
1593096483234 to 1593096483.234 to 1593096483
and taking count of documents furnished by above grouping then sorting on basis of count in decreasing order.
Query
db.server_api_logs.aggregate([
{
"$group": {
"_id": "$timestamp"
}
},
{
"$project": {
"summation": {
"$round": {
"$toDouble": {
"$divide": ["$_id",1000]
}
}
}
}
},
{
"$group": {
"_id": "$summation",
"summer": {"$sum": 1}
}
},
{
"$sort": {"summer": -1}
}
]
Getting this error when try to run above query
Unable to execute the selected commands
Mongo Server error (MongoCommandException): Command failed with error 168 (168): 'unknown operator: $round' on server <server address>
The full response is:
{
"ok" : 0.0,
"errmsg" : "unknown operator: $round",
"code" : NumberInt(168),
"codeName" : "168"
}
It is working totally fine on local using compass but not on Cosmos using Robo3T. Am I doing something wrong?
As stated in MongoDB documentation, $round operator was introduced in version 4.2 of MongoDB.
As stated in Azure Cosmos DB documentation, for this moment, Azure Cosmos DB's API for MongoDB is compatible with MongoDB server version 3.6.
This is the reason, why your query works on local MongoDB instance (I believe 4.2 or newer) but fails for Azure Cosmos DB.
I have a MongoDB aggregation query, I need the Spring boot mongo aggregation object example for the Following query.
db.case.aggregate([
{ $project: { item: { $concatArrays: [ "$workApproval.partItems", "$warrantyClaims.items.items" ] } } }
,{ $unwind : "$item"}
])
I am stuck on the concatArray portion, I am not sure how to write the above query in Spring Boot Mongo aggregation, any help is appreciated.
Here you are:
List<AggregationOperation> operations = new ArrayList<>();
operations.add(
Aggregation.project()
.and("workApproval.partItems").concatArrays("warrantyClaims.items.items").as("item")
);
operations.add(Aggregation.unwind("item"));
Aggregation aggregation = Aggregation.newAggregation(operations);
My document structure looks like
{
"users":[
{
"email":"user#company.com"
"messages":[
{
"id":"1",
"text":"A",
"from":"Jessy",
"isDeleted":"false"
},{
"id":"2",
"text":"B",
"from":"Jessy",
"isDeleted":"false"
},{
"id":"3",
"text":"C",
"from":"Alan",
"isDeleted":"false"
},{
"id":"4",
"text":"D",
"from":"Amy",
"isDeleted":"true"
},{
"id":"5",
"text":"E",
"from":"Amy",
"isDeleted":"false"
}
]
}
]
}
I want to query on sub document using or & and operations.So my query can be as follow.
select all those messages from the users collection having from as "Amy" and text is "E" or some other Criteria.
I have an idea about the Aggregation Framework supported by Spring data
AggregationOperation match =Aggregation.match(Criteria.where("from").is("Amy").and("text").is("E"));
AggregationOperation unwind = Aggregation.unwind("messages");
Aggregation aggregation = Aggregation.newAggregation(match);
AggregationResults<User> result = this.mongoTemplate.aggregate(aggregation, "users", User.class);
But my problem is that i want to dynamically populate the Criteria like Criteria is at clients end he or she can pass any query to the database i gonna parse that query into Criteria. Any help will be appreciated.