ElasticSearch Boolean Query Result Mismatch - java

This is my index mapping
"index":{
"mappings":{
"patient":{
"properties":{
"LastName":{
"type":"string"
},
"accountType":{
"type":"string"
},
"civilStatus":{
"type":"string"
},
"consultations":{
"type":"nested",
"properties":{
"deleted":{
"type":"boolean"
},
"diagnosis":{
"type":"string",
"index":"not_analyzed"
},
"documentDate":{
"type":"date",
"format":"dateOptionalTime"
},
"firstName":{
"type":"string"
},
"lastName":{
"type":"string"
},
"middleName":{
"type":"string"
},
"prescriptions":{
"type":"string"
}
}
},
"firstName":{
"type":"string"
},
"gender":{
"type":"string"
},
"id":{
"type":"string",
"index":"not_analyzed"
},
"lastName":{
"type":"string"
},
"middleName":{
"type":"string"
},
"occupation":{
"type":"string"
},
"owner":{
"type":"string",
"index":"not_analyzed"
},
"patientPin":{
"type":"string"
}
}
}
}
}
}
Here's the only saved data on ElasticSearch
{
"_index":"index",
"_type":"patient",
"_id":"TENANT1100066",
"_score":1.0,
"_source":{
"id":"100066",
"firstName":"Johnny",
"patientPin":"201408000001",
"middleName":"John ",
"consultations":[
{
"id":null,
"prescriptions":[
],
"diagnosis":[
"headache of unknown origin"
],
"documentDate":"2014-08-05T10:10:00.000+08:00",
"deleted":false,
"lastName":"David",
"firstName":"Johnny ",
"middleName":"John "
}
],
"owner":"TENANT1",
"gender":"MALE",
"occupation":"Unspecified",
"accountType":"INDIVIDUAL",
"civilStatus":"SINGLE",
"lastName":"David"
}
}
And here's the sample query I built to check how boolean query works.
{
"nested" : {
"query" : {
"bool" : {
"must" : [ {
"match" : {
"consultations.diagnosis" : {
"query" : "Kawasaki's Disease",
"type" : "phrase"
}
}
}, {
"match" : {
"consultations.diagnosis" : {
"query" : "Alcohol Intoxication",
"type" : "phrase"
}
}
} ],
"must_not" : {
"match" : {
"consultations.deleted" : {
"query" : "true",
"type" : "boolean"
}
}
},
"should" : {
"match" : {
"consultations.diagnosis" : {
"query" : "headache of unknown origin",
"type" : "phrase"
}
}
}
}
},
"path" : "consultations"
}
Apparently, Kawasaki's Disease and Fibriasis does not exist but headache of unknown origin exists but no
results are returned(Which is Johnny John David) what am I missing here? The operation I had in my mind was
(Kawasaki's Disease AND Fibriasis) OR headache of unknown origin.
What I had in mind was if there was no patients with Kawasakis Disease AND Fibriasis search for Patients with "headache of unknown origin". Which clearly we have, but my query is returning 0 results. what Am I missing here

In your query, you require that matching documents have both (Kawasaki's Disease AND Fibriasis) as you added these 2 conditions in the must clause.
Your document only match your should clause, so it doesn't appear in the search results.
To achieve what you want :
(Kawasaki's Disease AND Fibriasis) OR headache of unknown origin
You can embed the two diseases in another bool query and add this query in the should section of the root query, like this :
{
"query": {
"nested": {
"path": "consultations",
"query": {
"bool": {
"should": [
{
"bool": {
"must": [{
"match_phrase": {
"consultations.diagnosis": "Kawasaki's Disease"
}
},
{
"match_phrase": {
"consultations.diagnosis": "Alcohol Intoxication"
}
}
]
}
},
{
"match_phrase": {
"consultations.diagnosis": "headache of unknown origin"
}
}
],
"minimum_number_should_match": 1
}
}
}
}
}
Which outputs the previously indexed patient :
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.3007646,
"hits": [
{
"_index": "test",
"_type": "patient",
"_id": "TENANT1100066",
"_score": 0.3007646,
"_source": {
"id": "100066",
"firstName": "Johnny",
"patientPin": "201408000001",
"middleName": "John ",
"consultations": [
{
"id": null,
"prescriptions": [],
"diagnosis": [
"headache of unknown origin"
],
"documentDate": "2014-08-05T10:10:00.000+08:00",
"deleted": false,
"lastName": "David",
"firstName": "Johnny ",
"middleName": "John "
}
],
"owner": "TENANT1",
"gender": "MALE",
"occupation": "Unspecified",
"accountType": "INDIVIDUAL",
"civilStatus": "SINGLE",
"lastName": "David"
}
}
]
}
}

Related

ElasticSearch Aggregation Query to Select Top 1 for each Group base on Terms Array values

I have written below ES Query for fetching data Using filter and Must:
I can use only ElasticSearch V5 for writing queries.
{
"bool" : {
"must" : [
{
"term" : {
"Name" : {
"value" : "Self",
"boost" : 1.0
}
}
}
],
"filter" : [
{
"terms" : {
"Number" : [
"111",
"222"
]
}
}
]
}
}
I am getting output Like this with Duplicate value. I want to use aggregate query.
"hits": [
{ "Name": "Self","Number": 111},
{ "Name": "Self","Number": 222},
{ "Name": "Self","Number": 222},
{ "Name": "Self","Number": 111}
]
I want to select only Top 1 value for Each Number,So my Result out will be less.
O/P Should be:
"hits": [
{ "Name": "Self","Number": 222},
{ "Name": "Self","Number": 111}
]
You can use field collapsing to remove duplicate results
{
"query": {
"bool": {
"must": [
{
"term": {
"Name": {
"value": "Self",
"boost": 1.0
}
}
}
],
"filter": [
{
"terms": {
"Number": [
"111",
"222"
]
}
}
]
}
},
"collapse": {
"field": "Number"
}
}
Search Results will be
"hits": [
{
"_index": "68849562",
"_type": "_doc",
"_id": "1",
"_score": 0.10536051,
"_source": {
"Name": "Self",
"Number": 111
},
"fields": {
"Number": [
111
]
}
},
{
"_index": "68849562",
"_type": "_doc",
"_id": "2",
"_score": 0.10536051,
"_source": {
"Name": "Self",
"Number": 222
},
"fields": {
"Number": [
222
]
}
}
]

How to Group and get Count using morphia in mongoDB?

I'm using Morphia with MongoDB in Java, and i have collection like this
{"_id":"5d5e7ce7869eef030869e85c",
"ip":"66.249.79.181",
"date":"2019-08-19T18:30:00.000Z",
"request_url":"https://www.example.com/home",
"status_code":"200",
"bot":"Google Android",
"type":"type/html",
"domain":"https://www.example.com"},
{"_id":"5d5e7ce7869eef030869e85c",
"ip":"66.249.79.181",
"date":"2019-08-19T18:30:00.000Z",
"request_url":"https://www.example.com/home",
"status_code":"200",
"bot":"Google",
"type":"type/html",
"domain":"https://www.example.com"},
{"_id":"5d5e7ce7869eef030869e85c",
"ip":"66.249.79.181",
"date":"2019-08-19T18:30:00.000Z",
"request_url":"https://www.example.com/home",
"status_code":"200",
"bot":"bing",
"type":"type/html",
"domain":"https://www.example.com"}
i need to using grouping("request_url") and get count of "bot" field what if I need result like this
{"request_url":"https://www.example.com/home",
"status_code":"200",
"Google": 1,
"Google Android": 1,
"bing": 1,
"type":"type/html", }
How can i do this group by "request_url" Field and get Count of each "bot" Field have
Using aggregate below as:
db.collection.aggregate([
{
$group: {
_id: {
request_url: "$request_url",
bot: "$bot"
},
type: {
$max: "$type"
},
status_code: {
$max: "$status_code"
},
count: {
$sum: 1
}
}
},
{
$group: {
_id: "$_id.request_url",
type: {
$max: "$type"
},
status_code: {
$max: "$status_code"
},
counts: {
$push: {
bot: "$_id.bot",
count: "$count"
}
}
}
}
])
Giving input:
[
{
"ip": "66.249.79.181",
"date": "2019-08-19T18:30:00.000Z",
"request_url": "https://www.example.com/home",
"status_code": "200",
"bot": "Google Android",
"type": "type/html",
"domain": "https://www.example.com"
},
{
"ip": "66.249.79.181",
"date": "2019-08-19T18:30:00.000Z",
"request_url": "https://www.example.com/home",
"status_code": "200",
"bot": "Google",
"type": "type/html",
"domain": "https://www.example.com"
},
{
"ip": "66.249.79.181",
"date": "2019-08-19T18:30:00.000Z",
"request_url": "https://www.example.com/home",
"status_code": "200",
"bot": "bing",
"type": "type/html",
"domain": "https://www.example.com"
}
]
And output being:
[
{
"_id": "https://www.example.com/home",
"counts": [
{
"bot": "bing",
"count": 1
},
{
"bot": "Google",
"count": 1
},
{
"bot": "Google Android",
"count": 1
}
],
"status_code": "200",
"type": "type/html"
}
]
The following query can get us the expected output:
db.collection.aggregate([
{
$group:{
"_id":{
"request_url":"$request_url",
"bot":"$bot"
},
"request_url":{
$first:"$request_url"
},
"k":{
$first:"$bot"
},
"v":{
$sum:1
}
}
},
{
$group:{
"_id":"$request_url",
"request_url":{
$first:"$request_url"
},
"bots":{
$push:{
"k":"$k",
"v":"$v"
}
}
}
},
{
$project:{
"info.request_url":"$request_url",
"bots":{
$arrayToObject:"$bots"
}
}
},
{
$project:{
"info":{
$mergeObjects:["$info","$bots"]
}
}
},
{
$replaceRoot:{
newRoot:"$info"
}
}
]).pretty()
Data set:
{
"_id" : ObjectId("5d6d0f456bc2ad3b23f7dfcf"),
"ip" : "66.249.79.181",
"date" : "2019-08-19T18:30:00.000Z",
"request_url" : "https://www.example.com/home",
"status_code" : "200",
"bot" : "Google Android",
"type" : "type/html",
"domain" : "https://www.example.com"
}
{
"_id" : ObjectId("5d6d0f456bc2ad3b23f7dfd0"),
"ip" : "66.249.79.181",
"date" : "2019-08-19T18:30:00.000Z",
"request_url" : "https://www.example.com/home",
"status_code" : "200",
"bot" : "Google",
"type" : "type/html",
"domain" : "https://www.example.com"
}
{
"_id" : ObjectId("5d6d0f456bc2ad3b23f7dfd1"),
"ip" : "66.249.79.181",
"date" : "2019-08-19T18:30:00.000Z",
"request_url" : "https://www.example.com/home",
"status_code" : "200",
"bot" : "bing",
"type" : "type/html",
"domain" : "https://www.example.com"
}
Output:
{
"request_url" : "https://www.example.com/home",
"bing" : 1,
"Google" : 1,
"Google Android" : 1
}
Explanation: The data is grouped on the request_url and distinct key-value pairs are calculated. The key(k) would hold the bot name and the value(v) would hold the occurrence count. Later on, each pair is pushed into an array and then the array is converted into an object.

Mongo db java driver query convert

I have the following data structure
[{
"id": "1c7bbebd-bc3d-4352-9ac0-98c01d13189d",
"version": 0,
"groups": [
{
"internalName": "Admin group",
"fields": [
{
"internalName": "Is verified",
"uiProperties": {
"isShow": true
}
},
{
"internalName": "Hide",
"uiProperties": {
"isHide": false
}
},
...
]
},
...
]
},
{
"id": "2b7bbebd-bc3d-4352-9ac0-98c01d13189d",
"version": 0,
"groups": [
{
"internalName": "User group",
"fields": [
{
"internalName": "Is verified",
"uiProperties": {
"isShow": true
}
},
{
"internalName": "Blocked",
"uiProperties": {
"isBlocked": true
}
},
...
]
},
...
]
},
...
]
Internal names of the fields can be repeated. I want to group by group.field.internalName and cut the array(for pagination) and get the output like:
{
"totalCount": 3,
"items": [
{
"internalName": "Blocked"
},
{
"internalName": "Hide"
},
{
"internalName": "Is verified"
}
]}
I wrote a query that works,
db.layouts.aggregate(
{
$unwind : "$groups"
},
{
$unwind : "$groups.fields"
},
{
$group: {
"_id" : {
"internalName" : "$groups.fields.internalName",
},
"internalName" : {
$first : "$groups.fields.internalName"
}
}
},
{
$group: {
"_id" : null,
"items" : {
$push : "$$ROOT"
},
"totalCount" : {
$sum : 1
}
}
},
{
$project: {
"items" : {
$slice : [ "$items", 0, 20 ]
},
"totalCount": 1
}
})
but I have the problem of translating it to java api. Notice that i need to use mongoTemplate approach. Here is what i have and where i'm struck
final List<AggregationOperation> aggregationOperations = new ArrayList<>();
aggregationOperations.add(unwind("groups"));
aggregationOperations.add(unwind("groups.fields"));
aggregationOperations.add(
group("groups.fields.internalName")
.first("groups.fields.internalName").as("internalName")
);
aggregationOperations.add(
group()
.push("$$ROOT").as("fields")
.sum("1").as("totalCount") // ERROR only string ref can be placed, but i need a number?
);
aggregationOperations.add(
project()
.andInclude("totalCount")
.and("fields").slice(size, page * size)
);
final Aggregation aggregation = newAggregation(aggregationOperations);
mongoTemplate.aggregate(aggregation, LAYOUTS, FieldLites.class).getMappedResults()
With this query i have the problem with sum(), because i can place only a String ref by api(but need a number) and with project operation - got an exception
java.lang.IllegalArgumentException: Invalid reference 'totalCount'!] with root cause
Can you help me with this query translation?
You can use count
group()
.push("$$ROOT").as("fields")
.count().as("totalCount")

Mongodb query by Date field using Spring / Java

I have following Mongodb document. Would like to fetch document where participant = 'xxx' and message.lastmodifiedDate > dt and (message.touserid = 'xxx' or message.fromuserid = 'xxx').
{
"_id": {
"$oid": "575161ea02758f067057b8a8"
},
"_class": "com.idearealty.product.shopchat.persistence.model.Discussion",
"participants": "56d314a8e4b04d7f98cfd0c6,56d5d48ee4b0cc330f512a47,56d9d599e4b0cc330f512aaa,57130299e4b08c554c1092c7,56841002eceefce22f455c7f",
"messages": [
{
"_id": {
"$oid": "575161e802758f067057b8a4"
},
"formuserId": "56841002eceefce22f455c7f",
"fromuser": "9674642375",
"touserId": "56d314a8e4b04d7f98cfd0c6",
"touser": "debopam_r",
"message": "User Creating Discussion",
"isMute": false,
"index": 1,
"createDate": {
"$date": "2016-06-03T10:54:32.428Z"
},
"lastModifiedDate": {
"$date": "2016-06-03T10:54:32.428Z"
},
"createdBy": "9674642375",
"lastModifiedBy": "9674642375"
},
{
"_id": {
"$oid": "575161e902758f067057b8a5"
},
"formuserId": "56841002eceefce22f455c7f",
"fromuser": "9674642375",
"touserId": "56d5d48ee4b0cc330f512a47",
"touser": "Raushan",
"message": "User Creating Discussion",
"isMute": false,
"index": 2,
"createDate": {
"$date": "2016-06-03T10:54:33.006Z"
},
"lastModifiedDate": {
"$date": "2016-06-03T10:54:33.006Z"
},
"createdBy": "9674642375",
"lastModifiedBy": "9674642375"
},
{
"_id": {
"$oid": "575161e902758f067057b8a6"
},
"formuserId": "56841002eceefce22f455c7f",
"fromuser": "9674642375",
"touserId": "56d9d599e4b0cc330f512aaa",
"touser": "anirbanshop1",
"message": "User Creating Discussion",
"isMute": false,
"index": 3,
"createDate": {
"$date": "2016-06-03T10:54:33.572Z"
},
"lastModifiedDate": {
"$date": "2016-06-03T10:54:33.572Z"
},
"createdBy": "9674642375",
"lastModifiedBy": "9674642375"
},
{
"_id": {
"$oid": "575161ea02758f067057b8a7"
},
"formuserId": "56841002eceefce22f455c7f",
"fromuser": "9674642375",
"touserId": "57130299e4b08c554c1092c7",
"touser": "dummyshop",
"message": "User Creating Discussion",
"isMute": false,
"index": 4,
"createDate": {
"$date": "2016-06-03T10:54:34.208Z"
},
"lastModifiedDate": {
"$date": "2016-06-03T10:54:34.208Z"
},
"createdBy": "9674642375",
"lastModifiedBy": "9674642375"
}
],
"productId": "56841004eceefce22f455c9b",
"product": {
"_id": {
"$oid": "56841004eceefce22f455c9b"
},
"category": "Services",
"productName": "Driving School",
"imageurl": "service_icon.png",
"createDate": {
"$date": "2015-12-30T17:10:28.644Z"
},
"lastModifiedDate": {
"$date": "2015-12-30T17:10:28.644Z"
},
"createdBy": "UnAuntenticatedUser",
"lastModifiedBy": "UnAuntenticatedUser"
},
"userToRetailer": {
"57130299e4b08c554c1092c7": {
"_id": {
"$oid": "5713029ae4b08c554c1092c8"
},
"shopName": "dummyshop",
"user": {
"$ref": "IdeaRealtyUser",
"$id": {
"$oid": "57130299e4b08c554c1092c7"
}
}
},
"56d314a8e4b04d7f98cfd0c6": {
"_id": {
"$oid": "56d314a9e4b04d7f98cfd0c7"
},
"shopName": "Test Shop",
"user": {
"$ref": "IdeaRealtyUser",
"$id": {
"$oid": "56d314a8e4b04d7f98cfd0c6"
}
}
},
"56d9d599e4b0cc330f512aaa": {
"_id": {
"$oid": "56d9d59ae4b0cc330f512aab"
},
"shopName": "anirbanshop1",
"user": {
"$ref": "IdeaRealtyUser",
"$id": {
"$oid": "56d9d599e4b0cc330f512aaa"
}
}
},
"56d5d48ee4b0cc330f512a47": {
"_id": {
"$oid": "56d5d48fe4b0cc330f512a48"
},
"shopName": "Kolkata Shop1",
"user": {
"$ref": "IdeaRealtyUser",
"$id": {
"$oid": "56d5d48ee4b0cc330f512a47"
}
}
}
},
"messageCount": 4,
"createDate": {
"$date": "2016-06-03T10:54:34.215Z"
},
"lastModifiedDate": {
"$date": "2016-06-03T10:54:34.215Z"
},
"createdBy": "9674642375",
"lastModifiedBy": "9674642375"
}
I am using following java code to fetch the data, which translates to query
{ "aggregate" : "discussion" , "pipeline" : [ { "$match" : { "participants" : { "$regex" : "56d314a8e4b04d7f98cfd0c6"}}} , { "$unwind" : "$messages"} , { "$match" : { "$and" : [ { "messages.lastModifiedDate" : { "$gte" : { "$date" : "2016-02-28T16:06:11.960Z"}}} , { "$or" : [ { "messages.touserId" : "56d314a8e4b04d7f98cfd0c6"} , { "messages.formuserId" : "56d314a8e4b04d7f98cfd0c6"}]}]}} , { "$sort" : { "messages.lastModifiedDate" : -1}} , { "$skip" : 0} , { "$limit" : 10} , { "$group" : { "_id" : { "_id" : "$_id" , "productId" : "$productId"} , "data" : { "$push" : "$messages"}}} , { "$project" : { "productId" : "$_id.productId" , "data" : 1}}]}
But this query doesn't fecth any record. If I change the Date to ISODate it fetches expected result.
db.discussion.aggregate( [{ "$match" : { "participants" : { "$regex" : "56841002eceefce22f455c7f"}}} , { "$unwind" : "$messages"}, { "$match" : { "$and" : [ { "messages.lastModifiedDate" : { "$gte" : ISODate("2016-02-28T16:38:48.632Z")}} , { "$or" : [ { "messages.touserId" : "56841002eceefce22f455c7f"} , { "messages.formuserId" : "56841002eceefce22f455c7f"}]}]}}]);
Could you please let me know what changes is required so that it fetches using ISODate or while inserting the document it uses normal java.util.Date object? I tried with commented code as well but it didn't work.
public List<Discussion> findInbox(String userid,Date lastloginDate,int skip, int limit){
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
Aggregation aggr = newAggregation(
match(Criteria.where("participants").regex(Pattern.compile(userid))),
unwind("messages"),
match(new Criteria().andOperator(Criteria.where("messages.lastModifiedDate").gte(lastloginDate),new Criteria().orOperator(Criteria.where("messages.touserId").is(userid),Criteria.where("messages.formuserId").is(userid)))),
//match(new Criteria().andOperator(Criteria.where("messages.lastModifiedDate").is(new BasicDBObject("$gte","ISODate("+format.format(lastloginDate)+")")),new Criteria().orOperator(Criteria.where("messages.touserId").is(userid),Criteria.where("messages.formuserId").is(userid)))),
sort(Direction.DESC, "messages.lastModifiedDate"),
skip(skip),
limit(limit),
group("_id","productId").push("messages").as("data"),
project("_id","productId","data")
//project("product","participants","messages")
);
AggregationResults<Discussion> results = mongoTemplate.aggregate(aggr, "discussion", Discussion.class);
List<Discussion> discussions = results.getMappedResults();
return discussions;
}
I was using a Date field from another document. Changing it to Calendar solved the problem.
// Calling method
Calendar cal = Calendar.getInstance();
cal.setTime(loginInfo.getCreateDate());
return customDiscussionRepository.findInbox(activeUser.getId(), cal.getTime(), pageNumber > 0?pageSize*(pageNumber-1):0, pageSize);
// Repository (lastloginDate is the method argument passed from calling method).
Criteria.where("messages.lastModifiedDate").gte(lastloginDate)

$project is not working in mongo query [duplicate]

I need to export customer records from database of mongoDB. Exported customer records should not have duplicated values. "firstName+lastName+code" is the key to DE-duped the record and If there are two records present in database with same key then I need to give preference to source field with value other than email.
customer (id,firstName,lastName,code,source) collection is this.
If there are record 3 records with same unique key and 3 different sources then i need to choose only one record between 2 sources(TV,internet){or if there are n number of sources i need the one record only}not with the 'email'(as email will be choosen when only one record is present with the unique key and source is email)
query using:
db.customer.aggregate([
{
"$match": {
"active": true,
"dealerCode": { "$in": ["111391"] },
"source": { "$in": ["email", "TV", "internet"] }
}
},
{
$group: {
"_id": {
"firstName": "$personalInfo.firstName",
"lastName": "$personalInfo.lastName",
"code": "$vehicle.code"
},
"source": {
$addToSet: { "source": "$source" }
}
}
},
{
$redact:
{
$cond: [
{ $eq: [{ $ifNull: ["$source", "other"] }, "email"] },
"$$PRUNE",
"$$DESCEND"
]
}
},
{
$project:
{
"source":
{
$map:
{
"input": {
$cond: [
{ $eq: [{ $size: "$source" }, 0] },
[{ "source": "email" }],
"$source"
]
},
"as": "inp",
"in": "$$inp.source"
}
},
"record": { "_id": 1 }
}
}
])
sample output:
{ "_id" : { "firstName" : "sGI6YaJ36WRfI4xuJQzI7A==", "lastName" : "99eQ7i+uTOqO8X+IPW+NOA==", "code" : "1GTHK23688F113955" }, "source" : ["internet"] }
{ "_id" : { "firstName" : "WYDROTF/9vs9O7XhdIKd5Q==", "lastName" : "BM18Uq/ltcbdx0UJOXh7Sw==", "code" : "1G4GE5GV5AF180133" }, "source" : ["internet"] }
{ "_id" : { "firstName" : "id+U2gYNHQaNQRWXpe34MA==", "lastName" : "AIs1G33QnH9RB0nupJEvjw==", "code" : "1G4GE5EV0AF177966" }, "source" : ["internet"] }
{ "_id" : { "firstName" : "qhreJVuUA5l8lnBPVhMAdw==", "lastName" : "petb0Qx3YPfebSioY0wL9w==", "code" : "1G1AL55F277253143" }, "source" : ["TV"] }
{ "_id" : { "firstName" : "qhreJVuUA5l8lnBPVhMAdw==", "lastName" : "6LB/NmhbfqTagbOnHFGoog==", "code" : "1GCVKREC0EZ168134" }, "source" : ["TV", "internet"] }
This is a problem with this query please suggest :(
Your code doesn't work, because $cond is not an accumulator operator. Only these accumulator operators, can be used in a $group stage.
Assuming your records contain not more than two possible values of source as you mention in your question, you could add a conditional $project stage and modify the $group stage as,
Code:
db.customer.aggregate([
{
$group: {
"_id": {
"id": "$id",
"firstName": "$firstName",
"lastName": "$lastName",
"code": "$code"
},
"sourceA": { $first: "$source" },
"sourceB": { $last: "$source" }
}
},
{
$project: {
"source": {
$cond: [
{ $eq: ["$sourceA", "email"] },
"$sourceB",
"$sourceA"
]
}
}
}
])
In case there can be more that two possible values for source, then you could do the following:
Group by the id, firstName, lastName and code. Accumulate
the unique values of source, using the $addToSet operator.
Use $redact to keep only the values other than email.
Project the required fields, if the source array is empty(all the elements have been removed), add a
value email to it.
Unwind the source field to list it as a field and not an array.
(optional)
Code:
db.customer.aggregate([
{
$group: {
"_id": {
"id": "$id",
"firstName": "$firstName",
"lastName": "$lastName",
"code": "$code"
},
"sourceArr": { $addToSet: { "source": "$source" } }
}
},
{
$redact: {
$cond: [
{ $eq: [{ $ifNull: ["$source", "other"] }, "email"] },
"$$PRUNE",
"$$DESCEND"
]
}
},
{
$project: {
"source": {
$map: {
"input":
{
$cond: [
{ $eq: [{ $size: "$sourceArr" }, 0] },
[{ "source": "item" }],
"$sourceArr"]
},
"as": "inp",
"in": "$$inp.source"
}
}
}
}
])

Categories

Resources