$concat string with $cond in mongodb aggregation - mongodb

[
{
"user_id" : 12453,
"profile_type" : "demo_type_1",
"records" : [
{
"type" : "typ_11",
"value" : {
"high" : 115,
"low" : 78
},
"_meta" : {
"data_type" : "text"
}
},
{
"type" : "type_1",
"files" : [
{
"title" : "path_prescription_1",
"url" : "/file_name.extension"
},
{
"title" : "path_prescription_2",
"url" : "/file_name__1.extension"
}
],
"_meta" : {
"data_type" : "file"
}
}
]
},
{
"user_id" : 12455,
"profile_type" : "demo_type_1",
"records" : [
{
"type" : "typ_11",
"value" : {
"high" : 115,
"low" : 78
},
"_meta" : {
"data_type" : "text"
}
},
{
"type" : "type_1",
"files" : [
{
"title" : "path_prescription_1",
"url" : "/file_name.extension"
},
{
"title" : "path_prescription_2",
"url" : "/file_name__1.extension"
}
],
"_meta" : {
"data_type" : "file"
}
}
]
},
...
]
i want to append prefix the url value to make it an absolute path while retrieving from the database only when the _meta fields data_type is file not for text. The is stored in the above format and retrieved in the same format only with the appended url.
is there any way to do this by using aggregation pipeline ?

You can try using $addFields like this:
db.getCollection('test').aggregate(
[
{
$unwind: {
path:"$records",
preserveNullAndEmptyArrays: true
}
},
{
$unwind: {
path:"$records.files",
preserveNullAndEmptyArrays: true
}
},
{
$addFields: {
"records.files.url":{
$cond: {
if: {
$eq: ['$records.files', undefined]
},
then: null,
else: {$concat: ["some_prefix", "$records.files.url"]}
}
}
}
}
]
)
This will give you:
/* 1 */
{
"_id" : ObjectId("5b6c484b9a8ea6a11c508520"),
"user_id" : 12453.0,
"profile_type" : "demo_type_1",
"records" : {
"type" : "typ_11",
"value" : {
"high" : 115.0,
"low" : 78.0
},
"_meta" : {
"data_type" : "text"
},
"files" : {
"url" : null
}
}
}
/* 2 */
{
"_id" : ObjectId("5b6c484b9a8ea6a11c508520"),
"user_id" : 12453.0,
"profile_type" : "demo_type_1",
"records" : {
"type" : "type_1",
"files" : {
"title" : "path_prescription_1",
"url" : "some_prefix/file_name.extension"
},
"_meta" : {
"data_type" : "file"
}
}
}
/* 3 */
{
"_id" : ObjectId("5b6c484b9a8ea6a11c508520"),
"user_id" : 12453.0,
"profile_type" : "demo_type_1",
"records" : {
"type" : "type_1",
"files" : {
"title" : "path_prescription_2",
"url" : "some_prefix/file_name__1.extension"
},
"_meta" : {
"data_type" : "file"
}
}
}
/* 4 */
{
"_id" : ObjectId("5b6c484b9a8ea6a11c508521"),
"user_id" : 12455.0,
"profile_type" : "demo_type_1",
"records" : {
"type" : "typ_11",
"value" : {
"high" : 115.0,
"low" : 78.0
},
"_meta" : {
"data_type" : "text"
},
"files" : {
"url" : null
}
}
}
/* 5 */
{
"_id" : ObjectId("5b6c484b9a8ea6a11c508521"),
"user_id" : 12455.0,
"profile_type" : "demo_type_1",
"records" : {
"type" : "type_1",
"files" : {
"title" : "path_prescription_1",
"url" : "some_prefix/file_name.extension"
},
"_meta" : {
"data_type" : "file"
}
}
}
/* 6 */
{
"_id" : ObjectId("5b6c484b9a8ea6a11c508521"),
"user_id" : 12455.0,
"profile_type" : "demo_type_1",
"records" : {
"type" : "type_1",
"files" : {
"title" : "path_prescription_2",
"url" : "some_prefix/file_name__1.extension"
},
"_meta" : {
"data_type" : "file"
}
}
}
But remember to ignore records.files.url field when the record is type text.

Related

Return specific array value field in aggregate

I have a issue in MongoDB i'm trying to build a very complex aggregate query, and its work almost as i want it, but i still have trobles, and the problems is i need to move a spefiect field so i can use it later.
My aggregate look like this right now.
db.getCollection('travel_sights').aggregate([{
'$match': {
'preview.photo' : {
'$exists':true
},
'_id': {
'$in' : [ObjectId("5b7af9701fbad410e10f32f7")]
}
}
},{
'$unwind' : '$preview.photo'
}, {
'$lookup':{
'from' : 'media_data',
'localField' : '_id',
'foreignField':'bind',
'as':'media'
}
}])
and it will return data like this.
{
"_id" : ObjectId("5b7af9701fbad410e10f32f7"),
"preview" : {
"photo" : {
"id" : ObjectId("5b7affea1fbad441494a663b"),
"sort" : 0
}
},
"media" : [
{
"_id" : ObjectId("5b7affea1fbad441494a663b")
},
{
"_id" : ObjectId("5b7b002d1fbad441494a663c")
},
{
"_id" : ObjectId("5b7b00351fbad441494a663d")
},
{
"_id" : ObjectId("5b7d9baa1fbad410de638bbb")
},
{
"_id" : ObjectId("5b7d9bae1fbad410e10f32f9")
},
{
"_id" : ObjectId("5b7d9bb11fbad441494a663e")
},
{
"_id" : ObjectId("5b7d9bb41fbad4ff97273402")
},
{
"_id" : ObjectId("5b7d9bb71fbad4ff99527e82")
},
{
"_id" : ObjectId("5b7d9bbb1fbad410de638bbc")
},
{
"_id" : ObjectId("5b7d9bbe1fbad410e10f32fa")
},
{
"_id" : ObjectId("5b7d9bc11fbad441494a663f")
},
{
"_id" : ObjectId("5b7d9bc41fbad4ff97273403")
},
{
"_id" : ObjectId("5b7d9bc71fbad4ff99527e83")
},
{
"_id" : ObjectId("5b7d9bca1fbad410de638bbd")
},
{
"_id" : ObjectId("5b7d9bcd1fbad441494a6640")
},
{
"_id" : ObjectId("5b7d9bd01fbad4ff97273404")
}
]
}
{
"_id" : ObjectId("5b7af9701fbad410e10f32f7"),
"preview" : {
"photo" : {
"id" : ObjectId("5b7b002d1fbad441494a663c"),
"sort" : 0
}
},
"media" : [
{
"_id" : ObjectId("5b7affea1fbad441494a663b")
},
{
"_id" : ObjectId("5b7b002d1fbad441494a663c")
},
{
"_id" : ObjectId("5b7b00351fbad441494a663d")
},
{
"_id" : ObjectId("5b7d9baa1fbad410de638bbb")
},
{
"_id" : ObjectId("5b7d9bae1fbad410e10f32f9")
},
{
"_id" : ObjectId("5b7d9bb11fbad441494a663e")
},
{
"_id" : ObjectId("5b7d9bb41fbad4ff97273402")
},
{
"_id" : ObjectId("5b7d9bb71fbad4ff99527e82")
},
{
"_id" : ObjectId("5b7d9bbb1fbad410de638bbc")
},
{
"_id" : ObjectId("5b7d9bbe1fbad410e10f32fa")
},
{
"_id" : ObjectId("5b7d9bc11fbad441494a663f")
},
{
"_id" : ObjectId("5b7d9bc41fbad4ff97273403")
},
{
"_id" : ObjectId("5b7d9bc71fbad4ff99527e83")
},
{
"_id" : ObjectId("5b7d9bca1fbad410de638bbd")
},
{
"_id" : ObjectId("5b7d9bcd1fbad441494a6640")
},
{
"_id" : ObjectId("5b7d9bd01fbad4ff97273404")
}
]
}
{
"_id" : ObjectId("5b7af9701fbad410e10f32f7"),
"preview" : {
"photo" : {
"id" : ObjectId("5b7b00351fbad441494a663d"),
"sort" : 0,
"primary" : false
}
},
"media" : [
{
"_id" : ObjectId("5b7affea1fbad441494a663b")
},
{
"_id" : ObjectId("5b7b002d1fbad441494a663c")
},
{
"_id" : ObjectId("5b7b00351fbad441494a663d")
},
{
"_id" : ObjectId("5b7d9baa1fbad410de638bbb")
},
{
"_id" : ObjectId("5b7d9bae1fbad410e10f32f9")
},
{
"_id" : ObjectId("5b7d9bb11fbad441494a663e")
},
{
"_id" : ObjectId("5b7d9bb41fbad4ff97273402")
},
{
"_id" : ObjectId("5b7d9bb71fbad4ff99527e82")
},
{
"_id" : ObjectId("5b7d9bbb1fbad410de638bbc")
},
{
"_id" : ObjectId("5b7d9bbe1fbad410e10f32fa")
},
{
"_id" : ObjectId("5b7d9bc11fbad441494a663f")
},
{
"_id" : ObjectId("5b7d9bc41fbad4ff97273403")
},
{
"_id" : ObjectId("5b7d9bc71fbad4ff99527e83")
},
{
"_id" : ObjectId("5b7d9bca1fbad410de638bbd")
},
{
"_id" : ObjectId("5b7d9bcd1fbad441494a6640")
},
{
"_id" : ObjectId("5b7d9bd01fbad4ff97273404")
}
]
}
and what you can se the last data have preview.photo.primary on it, and this field i want to return when i'm done with my aggregate query.
My final query look like this:
db.getCollection('travel_sights').aggregate([{
'$match': {
'preview.photo' : {
'$exists':true
},
'_id': {
'$in' : [ObjectId("5b7af9701fbad410e10f32f7")]
}
}
},{
'$unwind' : '$preview.photo'
}, {
'$lookup':{
'from' : 'media_data',
'localField' : '_id',
'foreignField':'bind',
'as':'media'
}
},{
'$unwind':'$media'
},{
'$project' : {
'preview' : 1,
'media': 1,
}
}, {
'$group': {
'_id':'$media._id',
'primary': {
'$first':'$preview'
}
}
}])
The problem here is when i want $preview return so i can find the primary about it, its allways only return the first where the value not exists, if i use $push the problem is i get every thing.
is there a way so i can pick the right primary value in my return? have trying $addFields to but whitout eny kind of lock.
Travel_sights data:
{
"_id" : ObjectId("5b7af9701fbad410e10f32f7"),
"city_id" : ObjectId("5b6d0cb6222d4c70b803eaeb"),
"activated" : true,
"deleted" : false,
"url" : "url is here",
"name" : "title of it here",
"updated_at" : ISODate("2018-08-22T17:22:27.000Z"),
"content" : "content here",
"preview" : {
"photo" : [
{
"id" : ObjectId("5b7affea1fbad441494a663b"),
"sort" : 0
},
{
"id" : ObjectId("5b7b002d1fbad441494a663c"),
"sort" : 0
},
{
"id" : ObjectId("5b7b00351fbad441494a663d"),
"sort" : 0,
"primary" : true
},
{
"id" : ObjectId("5b7d9baa1fbad410de638bbb"),
"sort" : 0
},
{
"id" : ObjectId("5b7d9bae1fbad410e10f32f9"),
"sort" : 0
},
{
"id" : ObjectId("5b7d9bb11fbad441494a663e"),
"sort" : 0
},
{
"id" : ObjectId("5b7d9bb41fbad4ff97273402"),
"sort" : 0,
"primary" : false
},
{
"id" : ObjectId("5b7d9bb71fbad4ff99527e82"),
"sort" : 0,
"primary" : false
},
{
"id" : ObjectId("5b7d9bbb1fbad410de638bbc"),
"sort" : 0
},
{
"id" : ObjectId("5b7d9bbe1fbad410e10f32fa"),
"sort" : 0
},
{
"id" : ObjectId("5b7d9bc11fbad441494a663f"),
"sort" : 0
},
{
"id" : ObjectId("5b7d9bc41fbad4ff97273403"),
"sort" : 0,
"primary" : false
},
{
"id" : ObjectId("5b7d9bc71fbad4ff99527e83"),
"sort" : 0,
"primary" : false
},
{
"id" : ObjectId("5b7d9bca1fbad410de638bbd"),
"sort" : 0,
"primary" : false
},
{
"id" : ObjectId("5b7d9bcd1fbad441494a6640"),
"sort" : 0,
"primary" : false
},
{
"id" : ObjectId("5b7d9bd01fbad4ff97273404"),
"sort" : 0
}
]
}
}
3 sample foto bind data here:
{
"_id" : ObjectId("5b7affea1fbad441494a663b"),
"file-name" : "55575110311__0F115282-B5A0-4654-AA44-B7DC2C682992.jpeg",
"options" : [
ObjectId("5b6fb855222d4c70b8041093")
],
"type" : "images",
"files" : [
{
"width" : 70,
"height" : 53
},
{
"width" : 400,
"height" : 300
},
{
"width" : 800,
"height" : 600
},
{
"width" : 1600,
"height" : 1200
}
],
"bind" : [
ObjectId("5b7af9701fbad410e10f32f7")
]
}
{
"_id" : ObjectId("5b7b002d1fbad441494a663c"),
"file-name" : "55575110748__E7B07EFD-9F7E-40D6-8B57-38F708E4C0C0.jpeg",
"options" : [
ObjectId("5b6fb855222d4c70b8041093")
],
"type" : "images",
"files" : [
{
"width" : 70,
"height" : 53
},
{
"width" : 400,
"height" : 300
},
{
"width" : 800,
"height" : 600
},
{
"width" : 1600,
"height" : 1200
}
],
"bind" : [
ObjectId("5b7af9701fbad410e10f32f7")
],
"description" : "this is secoudn demo!",
"title" : "demo 3"
}
{
"_id" : ObjectId("5b7b00351fbad441494a663d"),
"file-name" : "paris2.jpg",
"options" : [
ObjectId("5b6fb855222d4c70b8041093")
],
"type" : "images",
"files" : [
{
"width" : 70,
"height" : 53
},
{
"width" : 400,
"height" : 300
},
{
"width" : 800,
"height" : 600
},
{
"width" : 1600,
"height" : 1200
}
],
"bind" : [
ObjectId("5b7af9701fbad410e10f32f7")
],
"description" : "this is a demo1 :)",
"title" : "demo"
}
You can filter out the element from the array where the primary field exists using $filter aggregation and then easily $group with the media._id field and get the $first document value.
Finally your query will be
db.getCollection("travel_sights").aggregate([
{ "$match": {
"preview.photo" : { "$exists":true },
"_id": { "$in" : [ ObjectId("5b7af9701fbad410e10f32f7") ] }
}},
{ "$addFields": {
"preview.photo": {
"$arrayElemAt": [
{ "$filter": {
"input": "$preview.photo",
"as": "photo",
"cond": { "$ne": [ "$$photo.primary", undefined ] }
}}, 0
]
}
}},
{ "$lookup":{
"from" : "media_data",
"localField" : "_id",
"foreignField": "bind",
"as": "media"
}},
{ "$unwind":"$media" },
{ "$project" : { "preview" : 1, "media": 1, }},
{ "$group": {
"_id": "$media._id",
"primary": { "$first": "$preview" }
}}
])

How to join deeply nested array?

Here is my actual database schema.
company_id is reference object of companies collection and booking_days.consultants.consultant_id is reference object of users collection.
I want to join embedded document with company_id and booking_days.consultants.consultant_id.
{
"_id" : ObjectId("5a7040d664544e1bb877deae"),
"company_id" : ObjectId("5a6eb43f437e6a0d9e00c92f"),
"booking_days" : [
{
"booking_date" : ISODate("2018-01-31T00:00:00.000Z"),
"_id" : ObjectId("5a7040d664544e1bb877deca"),
"consultants" : [
{
"consultant_id" : ObjectId("5a6f2854ce7d6938de1dd52c"),
"_id" : ObjectId("5a7040d664544e1bb877decc")
},
{
"consultant_id" : ObjectId("5a6f2854ce7d6938de1dd52f"),
"_id" : ObjectId("5a7040d664544e1bb877decb")
}
]
},
{
"booking_date" : ISODate("2018-02-01T00:00:00.000Z"),
"_id" : ObjectId("5a7040d664544e1bb877dec6"),
"consultants" : [
{
"consultant_id" : ObjectId("5a6f2854ce7d6938de1dd52f"),
"_id" : ObjectId("5a7040d664544e1bb877dec9")
},
{
"consultant_id" : ObjectId("5a6f2854ce7d6938de1dd52c"),
"_id" : ObjectId("5a7040d664544e1bb877dec8")
},
{
"consultant_id" : ObjectId("5a6f2854ce7d6938de1dd52c"),
"_id" : ObjectId("5a7040d664544e1bb877dec7")
}
]
},
{
"booking_date" : ISODate("2018-02-02T00:00:00.000Z"),
"_id" : ObjectId("5a7040d664544e1bb877dec4"),
"consultants" : [
{
"consultant_id" : ObjectId("5a6f2854ce7d6938de1dd52c"),
"_id" : ObjectId("5a7040d664544e1bb877dec5")
}
]
},
],
"__v" : 0
}
I am using below query.
db.getCollection('booking_days').aggregate(
[
{ $match: { company_id:ObjectId("5a6eb43f437e6a0d9e00c92f") } },
{
$lookup: {
localField: "company_id",
from: "companies",
foreignField: "_id",
as: "companies"
},
},
{
$lookup: {
localField: "booking_days.consultants.consultant_id",
from: "users",
foreignField: "_id",
as: "userssss"
},
},
{
$unwind:"$companies"
},
]
)
Actual Output
{
"_id" : ObjectId("5a7040d664544e1bb877deae"),
"company_id" : ObjectId("5a6eb43f437e6a0d9e00c92f"),
"booking_days" : [
{
"booking_date" : ISODate("2018-01-31T00:00:00.000Z"),
"_id" : ObjectId("5a7040d664544e1bb877deca"),
"consultants" : [
{
"consultant_id" : ObjectId("5a6f2854ce7d6938de1dd52c"),
"_id" : ObjectId("5a7040d664544e1bb877decc")
},
{
"consultant_id" : ObjectId("5a6f2854ce7d6938de1dd52f"),
"_id" : ObjectId("5a7040d664544e1bb877decb")
}
]
},
{
"booking_date" : ISODate("2018-02-01T00:00:00.000Z"),
"_id" : ObjectId("5a7040d664544e1bb877dec6"),
"consultants" : [
{
"consultant_id" : ObjectId("5a6f2854ce7d6938de1dd52f"),
"_id" : ObjectId("5a7040d664544e1bb877dec9")
},
]
},
],
"__v" : 0,
"companies" : {
"_id" : ObjectId("5a6eb43f437e6a0d9e00c92f"),
"first_name" : "Adrienne Runolfsson",
},
"users" : [
{
"_id" : ObjectId("5a6f2854ce7d6938de1dd52c"),
"first_name" : "Christ Hamill",
},
{
"_id" : ObjectId("5a6f2854ce7d6938de1dd52e"),
"first_name" : "Miss Dina Kovacek",
},
]
}
Excepted output. consultant data will come in booking_days.consultants array.
{
"_id" : ObjectId("5a7040d664544e1bb877deae"),
"company_id" : ObjectId("5a6eb43f437e6a0d9e00c92f"),
"booking_days" : [
{
"booking_date" : ISODate("2018-01-31T00:00:00.000Z"),
"_id" : ObjectId("5a7040d664544e1bb877deca"),
"consultants" : [
{
"consultant_id" : {
"_id" : ObjectId("5a6f2854ce7d6938de1dd52c"),
"first_name" : "Christ Hamill",
},
"_id" : ObjectId("5a7040d664544e1bb877decc")
},
{
"consultant_id" : {
"_id" : ObjectId("5a6f2854ce7d6938de1dd52e"),
"first_name" : "Miss Dina Kovacek",
},
"_id" : ObjectId("5a7040d664544e1bb877decb")
}
]
},
{
"booking_date" : ISODate("2018-02-01T00:00:00.000Z"),
"_id" : ObjectId("5a7040d664544e1bb877dec6"),
"consultants" : [
{
"consultant_id" : {
"_id" : ObjectId("5a6f2854ce7d6938de1dd52e"),
"first_name" : "Miss Dina Kovacek",
},
"_id" : ObjectId("5a7040d664544e1bb877dec9")
},
]
},
],
"__v" : 0,
"companies" : {
"_id" : ObjectId("5a6eb43f437e6a0d9e00c92f"),
"first_name" : "Adrienne Runolfsson",
},
}
As such you have to $unwind the localField when it is an embedded document array expect in some cases where localField is an array of scalar ids.
$unwind twice as consultant array is two levels deep followed by $lookup to get the name and $group to get back the expected output.
db.getCollection('booking_days').aggregate([
{"$match":{"company_id":ObjectId("5a6eb43f437e6a0d9e00c92f")}},
{"$lookup":{"localField":"company_id","from":"companies","foreignField":"_id","as":"companies"}},
{"$unwind":"$companies"},
{"$unwind":"$booking_days"},
{"$unwind":"$consultants"},
{"$lookup":{
"localField":"booking_days.consultants.consultant_id",
"from":"users",
"foreignField":"_id",
"as":"booking_days.consultants.consultant_id"
}},
{"$group":{
"_id":{"_id":"$_id","booking_days_id":"$booking_days._id"},
"company_id":{"$first":"$company_id"},
"booking_date":{"$first":"$booking_days.booking_date"},
"companies":{"$first":"$companies"},
"consultants":{"$push":"$booking_days.consultants"}
}},
{"$group":{
"_id":"$_id._id",
"company_id":{"$first":"$company_id"},
"companies":{"$first":"$companies"},
"booking_days":{
"$push":{
"_id":"$_id.booking_days_id",
"booking_date":"$booking_date",
"consultants":"$consultants"
}
}
}}
])
{"Id": "5b87a4c79a9c3feac943fc6c",
"comments" : [
{
"likes" : [],
"_id" : ObjectId("5b87a4c79a9c3feac943fc6c"),
"comment" : "string",
"accountId" : "a426d0da-ac72-4932-828e-3af99a998bc7",
"commentId" : "7d2a05d1-2026-4a13-a5c1-318ed80d1b38",
"reply" : [
{
"_id" : ObjectId("5b87b61e97585ef1d0d22108"),
"comment" : "string",
"accountId" : "a426d0da-ac72-4932-828e-3af99a998bc7",
"replyId" : "ec220fd7-3440-44dc-9178-7a1183879463"
},
{
"_id" : ObjectId("5b87b61e97585ef1d0d22108"),
"comment" : "string klllll",
"accountId" : "a426d0da-ac72-4932-828e-3af99a998bc7",
"replyId" : "ec220fd7-3440-44dc-9178-7a1183879463"
}
]
},
{
"likes" : [],
"_id" : ObjectId("5b87c301c8a07efa2599c29e"),
"comment" : "testing",
"accountId" : "cfd29f53-d73e-480c-9cfa-ea42b4119266",
"commentId" : "0676047b-1712-4f70-89d5-29c1abe03eaf",
"reply" : [
{
"_id" : ObjectId("5b87b61e97585ef1d0d22108"),
"comment" : "string",
"accountId" : "a426d0da-ac72-4932-828e-3af99a998bc7",
"replyId" : "ec220fd7-3440-44dc-9178-7a1183879463"
},
{
"_id" : ObjectId("5b87b61e97585ef1d0d22108"),
"comment" : "string klllll",
"accountId" : "a426d0da-ac72-4932-828e-3af99a998bc7",
"replyId" : "ec220fd7-3440-44dc-9178-7a1183879463"
}
]
}
]
}
accountId is in differnt connection
// Expected Out Put
{"Id": "5b87a4c79a9c3feac943fc6c",
"comments" : [
{
"likes" : [],
"_id" : ObjectId("5b87a4c79a9c3feac943fc6c"),
"comment" : "string",
"name" : "apple",
"reply" : [
{
"_id" : ObjectId("5b87b61e97585ef1d0d22108"),
"comment" : "string",
"name" : "apple",
},
{
"_id" : ObjectId("5b87b61e97585ef1d0d22108"),
"comment" : "string klllll",
"name" : "apple",
}
]
},
{
"likes" : [],
"_id" : ObjectId("5b87c301c8a07efa2599c29e"),
"comment" : "testing",
"name" : "ball",
"reply" : [
{
"_id" : ObjectId("5b87b61e97585ef1d0d22108"),
"comment" : "string",
"name" : "apple",
},
{
"_id" : ObjectId("5b87b61e97585ef1d0d22108"),
"comment" : "string klllll",
"name" : "apple", }
]
}
]
}

Mongodb find subdocument all values

I want to find all the values from a subdocument like this:
{ "_id" : ObjectId("XXXXXXXXXXXX"), "consumers" : { "AAAAAAAA" : { "CLIENT" : { "AA" : true } } }, "country" : "ES", "history" : [ ], "last_time_updated" : ISODate("2014-11-28T13:32:19.948Z"), "msisdn" : "123", "operator" : "ES", "time_created" : ISODate("2014-11-28T13:32:19.948Z") }
{ "_id" : ObjectId("XXXXXXXXXXXX"), "consumers" : { "AAAAAAAA" : { "CLIENT" : { "BB" : true } } }, "country" : "ES", "history" : [ ], "last_time_updated" : ISODate("2014-11-28T13:32:19.971Z"), "msisdn" : "123", "operator" : "ES", "time_created" : ISODate("2014-11-28T13:32:19.971Z") }
{ "_id" : ObjectId("XXXXXXXXXXXX"), "consumers" : { "AAAAAAAA" : { "CLIENT" : { "CC" : false } } }, "country" : "ES", "history" : [ ], "last_time_updated" : ISODate("2014-11-28T13:32:19.977Z"), "msisdn" : "123", "operator" : "ES", "time_created" : ISODate("2014-11-28T13:32:19.977Z") }
That include all the values from "CLIENT" that i don't know, i am triying with:
db.collection.find({"consumers" : { "AAAAAAAA" : { "CLIENT" : { $exists : true } } }})
But is not a valid query, please some help?
Thank you very much.
Dot notation can be used to match by fields in a sub document.
db.collection.find({"consumers.AAAAAAAA.CLIENT": {"$exists":true}})

Mongodb native query and equivalent java code

JSON DO
{
"_id" : "t6Y596WHx44S",
"pos_txn_type" : "CREDIT_AUTHORIZATION",
"pos_payment_method" : "CREDIT_CARD",
"processing_status" : "APPROVED",
"route" : NumberInt(7),
"audit_info" : {
"version" : "1.0.0.0",
"created_on" : ISODate("2016-08-08T07:26:57.000+0000"),
"updated_on" : ISODate("2016-08-08T07:26:57.000+0000")
},
"inflight_transactions" : [
{
"message_exchange" : {
"service_type" : "GATEWAY_SERVICE",
"adapter_id" : "adpIsoUms",
"route1" : NumberInt(7),
"request" : {
"type" : "com.renovite.ripps.kernel.msg.GatewayRequest",
"acquirer_inst_id" : "00000000001",
"host_address" : "127.0.0.1:2000",
"client_address" : "127.0.0.1:58577",
"domain_request" : {
"type" : "com.renovite.ripps.kernel.msg.CardRequestMessage",
"card" : {
"masked_pan" : "411111xxxxx1111",
"card_type" : "VISA"
},
"transactionAmount" : {
"amount" : NumberInt(100),
"amount_type" : "TXN_AMOUNT",
"currency" : "USD",
"currency_iso" : "840",
"currency_minor_unit" : NumberInt(2)
},
"additionalAmountMap" : {
},
"pos" : {
"stan" : "470641215751",
"entry_mode" : "SWIPED",
"pos_environment" : "ATTENDED",
"card_holder_verification_method" : "UNKNOWN",
"pos_card_holder_verification_method" : "MANUAL_SIGNATURE",
"terminal_capability" : "MSR_MANUAL",
"pos_terminal_id" : "0000000000000001",
"pos_transaction_time" : ISODate("2016-08-08T07:26:55.000+0000"),
"pos_transaction_date" : ISODate("2016-08-08T07:26:55.000+0000"),
"pos_function_code" : "100",
"cp" : true
},
"billTo" : {
"postal_code" : "94538"
}
},
"merchant_details" : {
"merchant_code" : "00000000001",
"merchant_category_code" : "5965",
"merchant_postal_code" : "4567",
"device_code" : "0000000000000001"
},
"auditInfo" : {
"version" : "1.0.0.0",
"created_on" : ISODate("2016-08-08T07:26:57.000+0000")
},
"additionalAttributes" : {
},
"raw_header" : "ISO.UMS.03",
"retrival_reference_number" : "000001215751"
},
"response" : {
"type" : "com.renovite.ripps.kernel.msg.GatewayResponse",
"auditInfo" : {
"version" : "1.0.0.0",
"created_on" : ISODate("2016-08-08T07:27:01.000+0000")
},
"additionalAttributes" : {
},
"domain_response" : {
"type" : "com.renovite.ripps.kernel.msg.CardResponseMessage",
"auth_code" : "831000",
"amount" : 9601.0,
"currency" : "USD",
"transaction_time" : ISODate("2016-08-08T01:57:00.000+0000"),
"processor_ref_number" : "4706412203976900504007",
"approval_code" : "0000",
"avs_response_code" : "2",
"reconciliation_id" : "4706412203976900504007",
"partial_auth" : false
}
}
}
},
{
"message_exchange" : {
"service_type" : "FRAUD_SERVICE",
"adapter_id" : "cartFRAUD",
"request" : {
"type" : "com.renovite.ripps.kernel.msg.ProviderRequest",
"auditInfo" : {
"version" : "1.0.0.0",
"created_on" : ISODate("2016-08-08T07:26:58.000+0000")
},
"additionalAttributes" : {
"MerchantTransactionIdentifier" : "t6Y596WHx44S"
},
"retrivalReferenceNumber" : "000001215751",
"merchantDetails" : {
"merchant_code" : "00000000001",
"merchant_category_code" : "5965",
"partial_auth" : "Y"
},
"domain_request" : {
"type" : "com.renovite.ripps.kernel.msg.fraud.FraudRequestMessage",
"client_address" : "127.0.0.1:58577"
}
},
"route" : "6",
"processing_status" : "APPROVED",
"response" : {
"type" : "com.renovite.ripps.kernel.msg.ProviderResponse",
"domain_response" : {
"type" : "com.renovite.ripps.kernel.msg.fraud.FraudResponseMessage",
"fraud_score" : NumberInt(22)
},
"auditInfo" : {
"version" : "1.0.0.0",
"created_on" : ISODate("2016-08-08T07:26:58.000+0000")
},
"additionalAttributes" : {
}
}
}
},
{
"message_exchange" : {
"service_type" : "AUTH_SERVICE",
"adapter_id" : "cartVACP",
"request" : {
"type" : "com.renovite.ripps.kernel.msg.ProviderRequest",
"auditInfo" : {
"version" : "1.0.0.0",
"created_on" : ISODate("2016-08-08T07:26:58.000+0000")
},
"additionalAttributes" : {
"COMMERCEINDICATOR" : "retail",
"MerchantTransactionIdentifier" : "t6Y596WHx44S",
"ThirdPartyCertificationNumber" : "575357012698"
},
"retrivalReferenceNumber" : "000001215751",
"merchantDetails" : {
"merchant_code" : "00000000004",
"merchant_category_code" : "5965",
"partial_auth" : "Y"
},
"domain_request" : {
"type" : "com.renovite.ripps.kernel.msg.CardRequestMessage",
"card" : {
"masked_pan" : "411111xxxxx1111",
"card_type" : "VISA"
},
"transactionAmount" : {
"amount" : 9601.0,
"amount_type" : "TXN_AMOUNT",
"currency" : "USD",
"currency_iso" : "840",
"currency_minor_unit" : NumberInt(2)
},
"additionalAmountMap" : {
},
"pos" : {
"entry_mode" : "SWIPED",
"pos_terminal_id" : "0000000087654321",
"cp" : true
},
"billTo" : {
"postal_code" : "94538"
}
},
"merchant_code" : "v5p234p575357"
},
"route" : "3",
"processing_status" : "APPROVED",
"response" : {
"type" : "com.renovite.ripps.kernel.msg.ProviderResponse",
"status" : "ACCEPT",
"domain_response" : {
"type" : "com.renovite.ripps.kernel.msg.CardResponseMessage",
"auth_code" : "831000",
"amount" : 9601.0,
"currency" : "USD",
"transaction_time" : ISODate("2016-08-08T01:57:00.000+0000"),
"processor_ref_number" : "4706412203976900504007",
"approval_code" : "100",
"avs_response_code" : "2",
"reconciliation_id" : "4706412203976900504007",
"partial_auth" : false
},
"auditInfo" : {
"version" : "1.0.0.0",
"created_on" : ISODate("2016-08-08T07:27:01.000+0000")
},
"additionalAttributes" : {
"PYMT_NETWORK_TXN_ID" : "016153570198200",
"RECEIPT_NUMBER" : "138115",
"REQUEST_TOKEN" : "Ahj/7wSR/kUuB8C84NOOelmjdg2aMWTJgzct2zlgwasGjBg3S36RRSYgFLfpFFJi0gp1MyMJsMmkmW6QHhMgQJyP8ilwPgXnBpxwvBkj",
"REASONCODE" : NumberInt(100),
"PROCESSORRESPONSE" : "00",
"CARDCATEGORY" : "A",
"MERCHANTREFERENCECODE" : "000001215751",
"CARDGROUP" : "0"
}
}
}
},
{
"message_exchange" : {
"service_type" : "LOYALTY_SERVICE",
"adapter_id" : "cartLOYALTY",
"request" : {
"type" : "com.renovite.ripps.kernel.msg.ProviderRequest",
"auditInfo" : {
"version" : "1.0.0.0",
"created_on" : ISODate("2016-08-08T07:27:01.000+0000")
},
"additionalAttributes" : {
"MerchantTransactionIdentifier" : "t6Y596WHx44S"
},
"retrivalReferenceNumber" : "000001215751",
"merchantDetails" : {
"merchant_code" : "00000000001",
"merchant_category_code" : "5965",
"partial_auth" : "Y"
},
"domain_request" : {
"type" : "com.renovite.ripps.kernel.msg.loyalty.LoyaltyRequestMessage",
"amount" : 9601.0,
"client_address" : "127.0.0.1:58577"
}
},
"route" : "7",
"processing_status" : "APPROVED",
"response" : {
"type" : "com.renovite.ripps.kernel.msg.ProviderResponse",
"domain_response" : {
"type" : "com.renovite.ripps.kernel.msg.loyalty.LoyaltyResponseMessage",
"loyaltyPoint" : NumberInt(677)
},
"auditInfo" : {
"version" : "1.0.0.0",
"created_on" : ISODate("2016-08-08T07:27:01.000+0000")
},
"additionalAttributes" : {
}
}
}
}
]
}
Below is the native mongo query -
db.txnlog.aggregate([
{
$group : {_id : "$pos_txn_type", count : {$sum : 1}, route_sum : {$push : "$inflight_transactions.0.message_exchange.request.domain_request.transactionAmount.amount"}}
}
])
and sheel output-
{ "_id" : "RECONCILE_REQUEST", "count" : 9, "route_sum" : [ [ ], [ ], [ ], [ ], [ ], [ ], [ ], [ ], [ ] ] }
{ "_id" : "CREDIT_CAPTURE", "count" : 2, "route_sum" : [ [ ], [ ] ] }
{ "_id" : "CREDIT_AUTHORIZATION", "count" : 2, "route_sum" : [ [ ], [ ] ] }
Problem i am not getting amount sum ? Please provide some input.
You can not use the dot notation to access an array element on the group stage, you have to add an extra $project stage to $slice the inflight_transactions array. Here is the query:
db.txnlog.aggregate([
{$project: {
pos_txn_type: 1,
inflight_transaction: {$slice: ["$inflight_transactions", 1]}
}},
{$group: {_id: "$pos_txn_type",
count: {$sum: 1},
route_sum: {$push: "$inflight_transaction.message_exchange.request.domain_request.transactionAmount.amount"}
}
}
]);
// output
{ "_id" : "CREDIT_AUTHORIZATION", "count" : 1, "route_sum" : [ [ 100 ] ] }

How query on some subdocument mongodb

There is a colloction, that have multi subdocument per document as follow:
db.test.insert(
{
"username" : " user1",
"Items" : [
{
"name" : "Total",
"value" : 53951
},
{
"name" : "n1",
"value" : 5711
},
{
"name" : "n2",
"value" : 5477
},
{
"name" : "n3",
"value" : 11444
}
]
})
db.test.insert(
{
"username" : " user2",
"Items" : [
{
"name" : "Total",
"value" : 43951
},
{
"name" : "n1",
"value" : 5011
},
{
"name" : "n2",
"value" : 5117
},
{
"name" : "n3",
"value" : 1444
}
]
})
db.test.insert(
{
"username" : " user3",
"Items" : [
{
"name" : "Total",
"value" : 51421
},
{
"name" : "n1",
"value" : 5241
},
{
"name" : "n2",
"value" : 1477
},
{
"name" : "n3",
"value" : 9244
}
]
})
I want to select Items that its name is "Total" as following:
{
"username" : " user1",
"Items" : [
{
"name" : "Total",
"value" : 53951
}
]
}
{
"username" : " user2",
"Items" : [
{
"name" : "Total",
"value" : 43951
}
]
}
{
"username" : " user3",
"Items" : [
{
"name" : "Total",
"value" : 51421
}
]
}
I used some queries like db.test.find({"Items.name":"Total"},{"Items.name":1,"Items.value":1}) . But its not correct.
Do is any way, or I should change Data Structure?
db.test.find({Items:{$elemMatch:{name:'Total'}}});
For more information check MongoDB docs.