MongoDB aggregation get value from path containing variable - mongodb

I have a collection datas like this:
[
{
"_id": 0,
"languages": { "en": true },
"translated_data": { "en": { "title": "eng title 1" } }
},
{
"_id": 1,
"languages": { "en": true },
"translated_data": { "en": { "title": "eng title 2" } }
}
]
And I need to transform it to a format like this:
[
{
"_id": 0,
"translations": [{ "language": "en", "data": { "title": "eng title 1" } }]
},
{
"_id": 1,
"translations": [{ "language": "en", "data": { "title": "eng title 2" } }]
}
]
I tried this update query but stuck
db.datas.updateMany({}, [
{
$set: {
translations: {
$map: {
input: {
$filter: {
input: {
$objectToArray: "$languages",
},
as: "language",
cond: { $eq: ["$$language.v", true] },
},
},
as: "language",
in: {
language: "$$language.k",
data: "???", // get value from $translated_data[$$language.k]
},
},
},
},
},
{
$unset: ["languages", "translated_data"],
},
]);

Try this:
db.datas.updateMany({},
[
{
$set: {
"translate_ok": {
$first: {
$map: {
input: { $objectToArray: "$languages" },
as: "language",
in: { $eq: ["$$language.v", true] },
}
}
}
}
},
{
$set: {
translations: {
$map: {
input: { $objectToArray: "$translated_data" },
as: "language",
in: {
$cond: [
"$translate_ok",
{
language: "$$language.k",
data: "$$language.v"
},
{
$arrayToObject: [["$$language"]]
}
]
}
}
}
}
},
{
$unset: ["languages", "translated_data", "translate_ok"]
}
]
);
Output:
/* 1 */
{
"_id" : 1,
"translations" : [
{
"language" : "en",
"data" : {
"title" : "eng title 1"
}
}
]
},
/* 2 */
{
"_id" : 2,
"translations" : [
{
"en" : {
"title" : "eng title 2"
}
}
]
}
My test data:
/* 1 */
{
"_id" : 1,
"languages" : {
"en" : true
},
"translated_data" : {
"en" : {
"title" : "eng title 1"
}
}
},
/* 2 */
{
"_id" : 2,
"languages" : {
"en" : false
},
"translated_data" : {
"en" : {
"title" : "eng title 2"
}
}
}

Related

find the string from mongo sub array

I have a collection with documents that look like this:
{
"_id" : ObjectId("56b53b7ddd81cc134fac76a5"),
"name_info" : [
"name c" : [
{
"aliases": ["bill", "william"]
}
],
"name e" : [
{
"aliases": ["robert", "bill"]
}
],
]
}
{
"_id" : ObjectId("87653b745481cc134fac7235"),
"name_info" : [
"name b" : [
{
"aliases": ["stan", "stanley"]
}
],
"name c" : [
{
"aliases": ["robert", "bill"]
}
],
]
}
{
"_id" : ObjectId("65433b7563a1cc134fac7634"),
"name_info" : [
"name b" : [
{
"aliases": ["tom", "tommy"]
}
]
]
}
What query should it be used in a find() command to return all objects that have alias containing 'bill'?
check this
db.collection.aggregate([
{
$project: {
"results": {
"$map": {
"input": "$name_info",
"as": "el",
"in": {
"data": {
$objectToArray: "$$el"
},
}
}
}
}
},
{
$unwind: "$results"
},
{
$unwind: "$results.data"
},
{
$unwind: "$results.data.v"
},
{
$unwind: "$results.data.v.aliases"
},
{
$match: {
"results.data.v.aliases": "bill"
}
},
])
example:
https://mongoplayground.net/p/VTNXMLNNXis
check your collection also

Mongodb $unwind removes objects. if $lookup doesn't have reference [duplicate]

This question already has an answer here:
Handling unwind for the non existing embedded document [duplicate]
(1 answer)
Closed 3 years ago.
If country doesn't have reference states and cities. $unwind removes country name from the collections.
Expected Output will be Mongodb should return country name even if the country doesn't any states and cities reference.
Country Collection:
[
{
"_id": "5d052c76df076d23a48d4a3b",
"name": "India"
},
{
"_id": "5d052c76df076d23a48d4b07",
"name": "Indonesia"
},
{
"_id": "5d052c76df076d23a48d22f4",
"name": "Iran"
}
]
State Collection:
[
{
"_id": "5d2236c37ed1112b3cc41397",
"name": "Andaman and Nicobar Islands",
"countryId": "5d052c76df076d23a48d4a3b"
},
{
"_id": "5d2236c37ed1112b3cc41398",
"name": "Andhra Pradesh",
"countryId": "5d052c76df076d23a48d4a3b"
}
]
City Collection:
[
{
"name": "Port Blair",
"stateId": "5d2236c37ed1112b3cc41397"
},
{
"name": "Adoni",
"stateId": "5d2236c37ed1112b3cc41398"
}
]
Query:
Country.aggregate([
{
$lookup:{
from: 'states',
localField:'_id',
foreignField:'countryId',
as:'states'
}
},
{
$unwind: {
path: "$states"
}
},
{
$lookup:{
from: 'cities',
localField:'states._id',
foreignField:'stateId',
as:'states.cities'
}
},
{
$group: {
_id: {
_id: '$_id',
name: '$name'
},
states: {
$push: '$states'
}
}
},
{
$project: {
_id: '$_id._id',
name: '$_id.name',
states: 1
}
}
])
Output:
[
{
"_id":"5d052c76df076d23a48d4a3b",
"name":"India",
"states":[
{
"_id":"5d2236c37ed1112b3cc41397",
"name":"Andaman and Nicobar Islands",
"countryId":"5d052c76df076d23a48d4a3b",
"cities":[
{
"name":"Port Blair",
"stateId":"5d2236c37ed1112b3cc41397"
}
]
},
{
"_id":"5d2236c37ed1112b3cc41398",
"name":"Andhra Pradesh",
"countryId":"5d052c76df076d23a48d4a3b",
"cities":[
{
"name":"Adoni",
"stateId":"5d2236c37ed1112b3cc41398"
}
]
}
]
}
]
Expected Output:
[
{
"_id":"5d052c76df076d23a48d4a3b",
"name":"India",
"states":[
{
"_id":"5d2236c37ed1112b3cc41397",
"name":"Andaman and Nicobar Islands",
"countryId":"5d052c76df076d23a48d4a3b",
"cities":[
{
"name":"Port Blair",
"stateId":"5d2236c37ed1112b3cc41397"
}
]
},
{
"_id":"5d2236c37ed1112b3cc41398",
"name":"Andhra Pradesh",
"countryId":"5d052c76df076d23a48d4a3b",
"cities":[
{
"name":"Adoni",
"stateId":"5d2236c37ed1112b3cc41398"
}
]
}
]
},
{
"_id":"5d052c76df076d23a48d4b07",
"name":"Indonesia",
"states":[
]
},
{
"_id":"5d052c76df076d23a48d22f4",
"name":"Iran",
"states":[
]
}
]
just add " preserveNullAndEmptyArrays: true " to $unwind
Country.aggregate([
{
$lookup:{
from: 'states',
localField:'_id',
foreignField:'countryId',
as:'states'
}
},
{
$unwind: {
path: "$states",
preserveNullAndEmptyArrays: true
}
},
{
$lookup:{
from: 'cities',
localField:'states._id',
foreignField:'stateId',
as:'states.cities'
}
},
{
$group: {
_id: {
_id: '$_id',
name: '$name'
},
states: {
$push: '$states'
}
}
},
{
$project: {
_id: '$_id._id',
name: '$_id.name',
states: 1
}
}
])
output
[
{
"states" : [
{
"cities" : []
}
],
"_id" : "5d052c76df076d23a48d22f4",
"name" : "Iran"
},
{
"states" : [
{
"cities" : []
}
],
"_id" : "5d052c76df076d23a48d4b07",
"name" : "Indonesia"
},
{
"states" : [
{
"_id" : "5d2236c37ed1112b3cc41397",
"name" : "Andaman and Nicobar Islands",
"countryId" : "5d052c76df076d23a48d4a3b",
"cities" : [
{
"_id" : ObjectId("5d38ccb6f9c5fa48bf099027"),
"name" : "Port Blair",
"stateId" : "5d2236c37ed1112b3cc41397"
}
]
},
{
"_id" : "5d2236c37ed1112b3cc41398",
"name" : "Andhra Pradesh",
"countryId" : "5d052c76df076d23a48d4a3b",
"cities" : [
{
"_id" : ObjectId("5d38ccbcf9c5fa48bf09902a"),
"name" : "Adoni",
"stateId" : "5d2236c37ed1112b3cc41398"
}
]
}
],
"_id" : "5d052c76df076d23a48d4a3b",
"name" : "India"
}
]

Query datevalue of a inner Array element

Need help with some MongoDB query:
The document I have is below and I am trying to search based on 2 conditions
The meta.tags.code = "ABC"
Its LastSyncDateTime should
meta.extension.value == "" (OR)
the meta.extension.value is less than meta.lastUpdated
Data :
{
"meta" : {
"extension" : [
{
"url" : "LastSyncDateTime",
"value" : "20190206-00:49:25.694"
},
{
"url" : "RetryCount",
"value" : "0"
}
],
"lastUpdate" : "20190207-01:21:41.095",
"tags" : [
{
"code" : "ABC",
"system" : "type"
},
{
"code" : "XYZ",
"system" : "SourceSystem"
}
]
}
}
Query:
db.proc_patients_service.find({
"meta.tags.code": "ABC",
$or: [{
"meta.extension.value": ""
}, {
$expr: { "$lt": [{ "mgfunc": "ISODate", "params": [{ "$arrayElemAt": ["$meta.extension.value", 0] }] }, { "mgfunc": "ISODate", "params": ["$meta.lastUpdate"] }] }
}]
})
But it is only fetching ABC Patients whose LastSyncDateTime is empty and ignores the other condition.
Using MongoDB Aggregation, I have converted your string to date with operator $dateFromString and then compare the value as per your criteria.
db.proc_patients_service.aggregate([
{ $match: { "meta.tags.code": "ABC", } },
{ $unwind: "$meta.extension" },
{
$project: {
'meta.tags': '$meta.tags',
'meta.lastUpdate': { '$dateFromString': { 'dateString': '$meta.lastUpdate', format: "%Y%m%d-%H:%M:%S.%L" } },
'meta.extension.url': '$meta.extension.url',
'meta.extension.value': {
$cond: {
if: { $ne: ["$meta.extension.value", "0"] }, then: { '$dateFromString': { 'dateString': '$meta.extension.value', format: "%Y%m%d-%H:%M:%S.%L" } }, else: 0
}
}
}
},
{
$match: {
$or: [
{ "meta.extension.value": 0 },
{ $expr: { $lt: ["$meta.extension.value", "$meta.lastUpdate"] } }
]
}
},
{
$group: { _id: '_id', 'extension': { $push: '$meta.extension' }, "lastUpdate": { $first: '$meta.lastUpdate' }, 'tags': { $first: '$meta.tags' } }
},
{
$project: { meta: { 'extension': '$extension', lastUpdate: '$lastUpdate', 'tags': '$tags' } }
}
])

Mongodb unwind array nested in array

This is my collection:
db.questions.insertMany([
{
"content": [
{
"languageId": "en",
"text": "What are you planning to buy today at the supermarket?"
},
{
"languageId": "nl",
"text": "Wat ben je van plan om vandaag in de supermarkt te kopen?"
},
],
"type": "multipleChoice",
"multipleChoice": {
"numAnswers": { "min": 1, "max": 1 },
"possibleAnswers": [
{
"sequence": 1,
"content": [
{
"languageId": "en",
"text": "apples"
},
{
"languageId": "nl",
"text": "appels"
},
],
},
{
"sequence": 2,
"content": [
{
"languageId": "en",
"text": "peers"
},
{
"languageId": "nl",
"text": "peren"
},
],
},
],
}
},
{
"content": [
{
"languageId": "en",
"text": "How do you feel?"
},
{
"languageId": "nl",
"text": "Hoe voel je je?"
},
],
"type": "ranking1to5",
}
]);
I want to transform into one language that are in two content arrays. So I want to have the output:
{
"_id" : ObjectId("5abe4c09d3831890de28ec8f"),
"content" :
{
"languageId" : "en",
"text" : "What are you planning to buy today at the supermarket?"
},
"type" : "multipleChoice",
"multipleChoice" : {
"numAnswers" : {
"min" : 1.0,
"max" : 1.0
},
"possibleAnswers" : [
{
"sequence" : 1.0,
"content" :
{
"languageId" : "en",
"text" : "apples"
}
},
{
"sequence" : 2.0,
"content" :
{
"languageId" : "en",
"text" : "peers"
}
}
]
}
}
/* 2 */
{
"_id" : ObjectId("5abe4c09d3831890de28ec90"),
"content" :
{
"languageId" : "en",
"text" : "How do you feel?"
},
"type" : "ranking1to5"
}
I tried using $unwind, $match and $group to tackle this problem. I have come pretty far only the last piece does not work:
db.getCollection('questions').aggregate([
{ $unwind: "$content" },
{ $match: { "content.languageId": "en" } },
{ $unwind: { path: '$multipleChoice.possibleAnswers', preserveNullAndEmptyArrays: true } },
{ $unwind: { path: '$multipleChoice.possibleAnswers.content', preserveNullAndEmptyArrays: true } },
{ $match: { "multipleChoice.possibleAnswers.content.languageId": "en" } },
{ $group: { _id: "$_id", content: { $first: "$content" }, type: { $first: "$type" }, multipleChoice: { $addToSet: "$multipleChoice" } } }
])
The problem is multipleChoice gets repeated while this should be possibleAnswers. Also the question that has no multipleChoice object should be included.
Any help is particularly appreciated !!
It looks like a good fit for the $redact. Please try the following aggregation:
db.questions.aggregate(
[
{
$redact: {
$cond: {
if: {
$eq: [
{ $ifNull: [ "$languageId", "en" ] },
"en"
]
},
then: "$$DESCEND",
else: "$$PRUNE"
}
}
},
{
$addFields: {
"content": { $arrayElemAt: [ "$content", 0 ] },
"multipleChoice.possibleAnswers": {
$map: {
input: "$multipleChoice.possibleAnswers",
as: "possibleAnswer",
in: {
"sequence": "$$possibleAnswer.sequence",
"content": { $arrayElemAt: [ "$$possibleAnswer.content", 0 ] }
}
}
}
}
},
{
$redact: {
$cond: {
if: { $eq: [ "$possibleAnswers", null ] },
then: "$$PRUNE",
else: "$$DESCEND"
}
}
}
]
);

correctly grouping documents in aggregate pipeline in order to find setintersection

say I have these two documents:
{
"_id":"sampleA",
"value":{
"data":[
{
"thing":"A"
},
{
"thing":"B"
},
{
"thing":"C"
},
{
"thing":"D"
},
{
"thing":"E"
}
]
}
}
{
"_id":"sampleB",
"value":{
"data":[
{
"thing":"C"
},
{
"thing":"D"
},
{
"thing":"E"
},
{
"thing":"F"
}
]
}
}
and I want to group them into one document, retaining the label of "sampleA" or "sampleB" such as
{
"_id": null,
"sampleA": [
{
"thing": "A"
},
{
"thing": "B"
},
{
"thing": "C"
},
{
"thing": "D"
},
{
"thing": "E"
}
],
"sampleB": [
{
"thing": "C"
},
{
"thing": "D"
},
{
"thing": "E"
},
{
"thing": "F"
}
]
}
So that way I can use the set intersection operator. How do I go about doing this? I tried:
db.testz.aggregate(
[{
$match: {
_id: {
$in: ["sampleA", "sampleB"]
}
}
}, {
'$group': {
_id: null,
a: {
$push: "$value"
}
}
}]
);
which gives me
{
"_id": null,
"a": [
{
"data": [
{
"thing": "A"
},
{
"thing": "B"
},
{
"thing": "C"
},
{
"thing": "D"
},
{
"thing": "E"
}
]
},
{
"data": [
{
"thing": "C"
},
{
"thing": "D"
},
{
"thing": "E"
},
{
"thing": "F"
}
]
}
]
}
Presumably I could then use the set intersection operator if I could index the items in a?
db.testz.aggregate(
[{
$match: {
_id: {
$in: ["sampleA", "sampleB"]
}
}
}, {
'$group': {
_id: null,
a: {
$push: "$value"
}
}
}, {
'$project': {
int: {
$setIntersection: ["$a.0", "$a.1"]
}
}
}]
);
^^ obviously this last step here doesn't work, but I am trying to illustrate the point.
I think the only way to do this presently (MongoDB 2.6) is to unwind the arrays and then recollect in a set:
> db.testz.aggregate([
{ "$match" : { "_id" : { "$in" : ["sampleA", "sampleB"] } } },
{ "$unwind" : "$value.data" },
{ "$group" : { "_id" : 0, "intersection" : { "$addToSet" : "$value.data" } } }
])
It's not an efficient way to do it, but it gets the job done. I was probing you for more specific info to see if there was some way to avoid this answer :(