My mongo aggregation query is like this:
db.events.aggregate({
"$match" : { $or : [
{"event_state" : "live"},
{
$and: [
{"event_state" : "scheduled"},
{"schedule.start_time" : { "$gt" : ISODate("2016-12-15T14:06:00.000Z")}}
]
}
]
} },
{ "$sort" : { "schedule.start_time" : 1}} ,
{ "$project" : {
"registered_users_count" : { "$size" : [ "$registered_users"]} ,
"event_image" : 1 , "celebrity" : 1 , "name" : 1 , "category" : 1 ,
"schedule" : 1 , "online_moderator" : 1 , "offline_moderator" : 1 ,
"region" : 1 , "status" : 1 , "event_state" : 1 , "recorder_id" : 1 ,
"webcast_url" : 1 , "replay_url" : 1
}})
I tried something like below
Variant 1:
matchCondition = Aggregation.match(Criteria.where("event_state")
.is("scheduled").and("schedule.end_time").gt(d)
.orOperator(Criteria.where("event_state").is("live")));
Variant 2:
matchCondition = Aggregation.match(Criteria
.where("event_state")
.is("live")
.orOperator(
Criteria.where("event_state").is("scheduled")
.and("schedule.end_time").gt(d)));
Sort and Projection Conditions
sortCondition = Aggregation.sort(Sort.Direction.ASC,
"schedule.start_time");
AggregationOperation projectValues = Aggregation.project()
.and("registered_users").size().as("registered_users_count")
.and("event_image").as("event_image").and("celebrity")
.as("celebrity").and("name").as("name").and("category")
.as("category").and("schedule").as("schedule")
.and("online_moderator").as("online_moderator")
.and("offline_moderator").as("offline_moderator").and("region")
.as("region").and("status").as("status").and("event_state")
.as("event_state").and("recorder_id").as("recorder_id")
.and("webcast_url").as("webcast_url").and("replay_url")
.as("replay_url");
Aggregation aggrigation = Aggregation.newAggregation(matchCondition,
sortCondition, projectValues);
None of the Variant 1 and Variant 2 are producing desired condition. So how to achieve this?
Form the spring-data-mongo API documentation Criteria.OrOperators take multiple Criteria as argument to form a or operation like.
So the solution for this would be as follows:
Aggregation.match(
new Criteria().orOperator(
Criteria.where("event_state").is("scheduled").and("schedule.start_time").gt(d),
Criteria.where("event_state").is("live")
)
)
Related
I have a specific json value as shown below,
{
"record_id" : "r01",
"teacherNstudents": [
{
"teacher" : {
"name" : "tony",
"tid" : "T01"
},
"student" : {
"name" : "steve",
"sid" : "S01"
}
},
{
"teacher" : {
"name" : "tony",
"tid" : "T01"
},
"student" : {
"name" : "natasha",
"sid" : "S02"
}
},
{
"teacher" : {
"name" : "tony",
"tid" : "T01"
},
"student" : {
"name" : "bruce",
"sid" : "S03"
}
},
{
"teacher" : {
"name" : "tony",
"tid" : "T01"
},
"student" : {
"name" : "victor",
"sid" : "S04"
}
},
{
"teacher" : {
"name" : "henry",
"tid" : "T02"
},
"student" : {
"name" : "jack",
"sid" : "S05"
}
},
{
"teacher" : {
"name" : "henry",
"tid" : "T02"
},
"student" : {
"name" : "robert",
"sid" : "S06"
}
}
]
}
I am trying to generate a map like the one below,
[ {"S01", "T01"} , {"S05","T02"} ]
This is by removing all duplicate values and selecting only one teacher and student. The current code I wrote for this is
var firstMap = records.getTeacherNstudents()
.stream()
.collect(Collectors.toMap(tS -> tS.getTeacher().getTid(),
tS -> tS.getStudent().getSid(),
(a1, a2) -> a1));
return firstMap.entrySet()
.stream()
.collect(Collectors.toMap(Map.Entry::getValue, Map.Entry::getKey));
I believe this can be improved, by using Collectors.groupingBy. I am still working on it, but if anyone has any good idea on how to solve this, please share.
Using Java 8 groupingBy
You can try the below approach in order to have the Map<String,List<String>> or Map<String,Set<String>>(avoid duplicates) where key of map will be the teacher id and value as List or Set of Students corresponding to each teacher.
I have used groupingBy feature from java 8 and did the grouping based on the tId and before collecting it, I have downstream it to List or Set of student Ids corresponding to each tId.
Approach A: Map<String,Set< String >> (Uniques)
data.getTeacherStudentMappingList()
.stream()
.collect(Collectors.groupingBy(x -> x.getTeacher().getTid(), LinkedHashMap::new,
Collectors.mapping(y -> y.getStudent().getSid(),Collectors.toSet())));
Approach B : Map<String,List< String >> (Non-uniques, duplicates)
data.getTeacherStudentMappingList()
.stream()
.collect(Collectors.groupingBy(x -> x.getTeacher().getTid(), LinkedHashMap::new,
Collectors.mapping(y -> y.getStudent().getSid(),Collectors.toList())));
Here,
data is the converted object from the given json.
LinkedHashmap::new is used to preserve the order of student data from the json in the output.
collectors.mapping is used to convert the values corresponding to each key into the student ids.
Collectors.toList() will collect the list of student ids in the list.
Collectors.toSet() will collect the unique student ids in the set.
Output:
{T01=[S01, S02, S03, S04], T02=[S05, S06]}
I have a CosmosDb - MongoDb collection that I'm using purely as a key/value store for arbitrary data where the _id is the key for my collection.
When I run the query below:
globaldb:PRIMARY> db.FieldData.find({_id : new BinData(3, "xIAPpVWVkEaspHxRbLjaRA==")}).explain(true)
I get this result:
{
"_t" : "ExplainResponse",
"ok" : 1,
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "data.FieldData",
"indexFilterSet" : false,
"parsedQuery" : {
"$and" : [ ]
},
"winningPlan" : {
},
"rejectedPlans" : [ ]
},
"executionStats" : {
"executionSuccess" : true,
"nReturned" : 1,
"executionTimeMillis" : 106,
"totalKeysExamined" : 0,
"totalDocsExamined" : 3571,
"executionStages" : {
},
"allPlansExecution" : [ ]
},
"serverInfo" : #REMOVED#
}
Notice that the totalKeysExamined is 0 and the totalDocsExamined is 3571 and the query took over 106ms. If i run without .explain() it does find the document.
I would have expected this query to be lightning quick given that the _id field is automatically indexed as a unique primary key on the collection. As this collection grows in size, I only expect this problem to get worse.
I'm definitely not understanding something about the index and how it works here. Any help would be most appreciated.
Thanks!
I am working on Spring MVC with MongoDB.
I have added criteria for some fields with orOperator, Each Fields has value in database, but List returning is Empty.
criteriaQuery.addCriteria(Criteria.where("First_Name").is(docName.trim())
.orOperator(Criteria.where("Middle_Name").is(docName.trim())
.orOperator(Criteria.where("Last_Name").is(docName.trim()))));
Query printing in console is
Query: { "First_Name" : "ESTHER" , "$or" : [ { "Middle_Name" : "ESTHER" , "$or" : [ { "Last_Name" : "ESTHER"}]}]}, Fields: null, Sort: null
I tried the Like query, but that also results the same
criteriaQuery.addCriteria(Criteria.where("First_Name").regex(docName)
.orOperator(Criteria.where("Middle_Name").regex(docName))
.orOperator(Criteria.where("Last_Name").regex(docName)));
Mongo Query
db.doctor_details.find({ "$or" : {"First_Name" : { "$regex" : "ESTHER"} }, "$or" : [ { "Middle_Name" : { "$regex" : "ESTHER"} , "$or" : [ { "Last_Name" : { "$regex" : "ESTHER"}}]}]})
Help me to get the result, I dont know where my code goes wrong. Help is appreciated!
Thanks
As far as I remember in order to use orOperator you should do:
Query query = new Query(new Criteria().orOperator(criteriaV1,criteriaV2));
it is based on this answer
For mongodb 3.2 following query successfully worked.
db.user_log.aggregate([{ "$match" : { "user_id" : "1" , "page_id" : "678761252281073"}} , { "$sort" : { "meta_data.access_times" : -1}} , { "$group" : { "_id" : { "first_name" : "$meta_data.user_data.first_name" , "last_name" : "$meta_data.user_data.last_name" , "profile_pic" : "$meta_data.user_data.profile_pic" , "user_id" : "$user_id" , "star_value" : "$star_value" , "access.times" : "$meta_data.access_times"}}}])
Java code -
Aggregation aggregation = newAggregation(
match(Criteria.where("user_id").is("123")),
sort(Sort.Direction.DESC, "meta_data.access_times"),
group(Fields.fields().and("first_name", "$meta_data.user_data.first_name").and("last_name", "$meta_data.user_data.last_name").and("profile_pic", "$meta_data.user_data.profile_pic").and("user_id", "$user_id").and("star_value", "$star_value").and("access.times", "$meta_data.access_times"))
);
AggregationResults<UsersMongoResult> groupResults = mongoTemplate.aggregate(aggregation, "user_log", UsersMongoResult.class);
but after upgrading to 3.4 it occurs following exception
org.springframework.dao.InvalidDataAccessApiUsageException: Command
execution failed: Error [FieldPath field names may not contain '.'.]
what is the reason behind this and how do I fix the issue
?
Issue is access.times, It is a bad practice to have . in your fields as it collides with nested elements.
I am getting stuck getting while implementing search using elastic search nest.
I have to implement like query with elastic search.
for ex. select * from table where username like '%abc xyz%'
As you can see in above sql query I have applied like query with string "abc" space and another string "xyz". Similarly I want this query in elastic search. Can anyone help me to implement such query in elastic search nest?
Below is the query
Client.Search<Video>(s => s
.Query(q => q
.Match(m => m
.OnField("video_parent")
.Query("0")
) && q
.Match(m => m
.OnField("video_status")
.Query(objVideosFilterCriteria.VideoStatus.ToString())
) && q
.MatchPhrase(ff=>ff
.OnField("video_title")
.Query(objVideosFilterCriteria.SearchString)
) && q
.Range(r => r
.OnField(f => f.video_date)
.GreaterOrEquals(fromDate)
.LowerOrEquals(toDate)
)
)
.From(objVideosFilterCriteria.PageIndex)
.Size(objVideosFilterCriteria.PageSize)
);
Above is the query I am using. In this query I am using
q.MatchPhrase(ff=>ff
.OnField("video_title")
.Query(objVideosFilterCriteria.SearchString)
)
for like query. But it doesn't seem to work.
I am using below data set and want to filter data from below list.
"hits" : [
{
"_source" : {
"video_id" : 265006,
"video_title" : "nunchuk rockin roller II"
}
},
{
"_source" : {
"video_id" : 265013,
"video_title" : "?Shaggy?????Locks???7??????Alberto E. Interview {407} 967 ~ 8596?"
}
},
{
"_source" : {
"video_id" : 265014,
"video_title" : "Shakin' Stevens - Kalundborg Rocker"
}
},
{
"_source" : {
"video_id" : 265019,
"video_title" : "?Shaggy?????Locks? = 7??????Greg M. Interview {407} 967 ~ 8596?"
}
},
{
"_source" : {
"video_id" : 265023,
"video_title" : "?Shaggy?????Locks? = 7??????Jason M. Interview {407} 967 ~ 8596?"
}
}
]
For example I would like to search with the keyword "kin rol" in the "video_title" field so with the above data it should fetch one record which exists at first position in above list but in my current query I am getting. nothing.