Return field of column in MongoDB - mongodb

I have this table, and I want to filter some fields of it. The table looks like this:
/* 1 */
{
"_id" : ObjectId("5ce7db93db1ec10d08941d44"),
"name" : "john",
"age" : "22",
"group" : "A",
"nodes" : [
{
"name1" : "some_name1",
"status" : "completed"
}
]
}
/* 2 */
{
"_id" : ObjectId("5ce7e2fd726ed9434c32aaba"),
"name" : "mike",
"age" : "23",
"group" : "B",
"nodes" : [
{
"dev_name" : "some_name_dev1",
"status" : "not completed"
},
{
"dev_name" : "some_name_dev2",
"status" : "completed"
}
]
}
/* 3 */
{
"_id" : ObjectId("5ce7e36c726ed9434c32aabc"),
"name" : "anne",
"age" : "24",
"group" : "C",
"status" : "pending"
}
/* 4 */
{
"_id" : ObjectId("5ce7f05e726ed9434c32aabe"),
"name" : "jane",
"age" : "27",
"group" : "D",
"nodes" : [
{
"dev_name" : "some_name_dev6",
"status" : "not completed"
},
{
"dev_name" : "some_name_dev7"
}
]
}
And what I actually want to return is this (a simple object with "status" if nodes does not exist, and an array if nodes exists:
/* 1 */
[{
"status" : "completed"
}]
/* 2 */
[{
"status" : "not completed"
},
{
"status" : "completed"
}]
/* 3 */
{
"status" : "pending"
}
/* 4 */
[{
"status" : "not completed"
}]
I did this:
db.getCollection('user').find(
{
$or: [
{
"status": { $ne:null }
},
{
"nodes.status":{ $exists: true }
}
]
},
{
'status': 1,
'nodes.status': 1,
'_id': 0
}
)
and the result is this, and I am a little bit stuck:
/* 1 */
{
"nodes" : [
{
"status" : "completed"
}
]
}
/* 2 */
{
"nodes" : [
{
"status" : "not completed"
},
{
"status" : "completed"
}
]
}
/* 3 */
{
"status" : "pending"
}
/* 4 */
{
"nodes" : [
{
"status" : "not completed"
},
{}
]
}
How can I obtain what I want (getting rid of nodes field)? Thank you for your time!
EDIT:
db.getCollection('user').find(
{
$or: [
{
"status": { $ne:null }
},
{
"nodes.status":{ $exists: true }
}
]
},
{
'status': 1,
'nodes.status': 1,
'_id': 0
},
[ { "$project": { "status": { "$ifNull" : ["$nodes.status", ""] } } } ]
)

You are going to the right direction using {$project: <expression>} to change the format of your return document. But I don't think the $ifNull has been used correctly. According to the doc, the input of the $ifNull is
{ $ifNull: [ <expression>, <replacement-expression-if-null> ] }
Full Query:
db.getCollection('user').find(
{
$or: [
{
"status": { $ne:null }
},
{
"nodes.status":{ $exists: true }
}
]
},
{
'status': { "$ifNull" : ["$status", "$nodes.status"] },
'_id': 0
})
Edit: Using aggregation instead of simple find(), as $ifNull operator is only available using aggregation.
db.collection.aggregate([
{
$match: { #behaves like the find operator
$or: [
{
"status": {
$ne: null
}
},
{
"nodes.status": {
$exists: true
}
}
]
}
},
{
$project: {
"status": {
"$ifNull": [
"$status",
"$nodes.status"
]
},
"_id": 0
}
}
])
output:
[
{
"status": [
"completed"
]
},
{
"status": [
"not completed",
"completed"
]
},
{
"status": "pending"
},
{
"status": [
"not completed"
]
}
]

Related

How update string to number field

/* 1 */
{
"_id" : ObjectId("62622dd73905f04f59db2971"),
"array1" : [
{
"_id" : "21",
"array2" : [
{
"_id" : "123",
"answeredBy" : [
"success"
]
},
{
"_id" : "124",
"answeredBy" : []
}
]
}
]
}
/* 2 */
{
"_id" : ObjectId("626230e03905f04f59db29f5"),
"array1" : [
{
"_id" : "22",
"array2" : [
{
"_id" : "223",
"answeredBy" : []
},
{
"_id" : "220",
"answeredBy" : []
}
]
}
]
}
How to convert
"_id" : "21", and "_id" : "22",
to
"_id" : 21, and "_id" : 22,
Here's one way to do it.
db.collection.update({
"array1._id": { "$exists": true }
},
[
{
"$set": {
"array1": {
"$map": {
"input": "$array1",
"as": "elem",
"in": {
"$mergeObjects": [
"$$elem",
{ "_id": { "$toInt": "$$elem._id" } }
]
}
}
}
}
}
],
{
"multi": true
})
Try it on mongoplayground.net.

how to sort an array in a nested array which is located under an object in mongodb

I have a collection data like below.
{
"name": "Devices",
"exten": {
"parameters": [{
"name": "Date",
"value": ["5","2"]
}, {
"name": "Time",
"value": ["2"]
}, {
"name": "Season",
"value": ["6"]
}
]
}
}
I want to take all data which is name "Devices" and sort by first index of "Value" which is parameter name is "Date"
ex: mongo will get
name = "devices"
exten.parameters.name = "Date"
will sort it by
exten.parameters.value[0]
in this example it will be sorted by "5".
below query returns 0 record.
db.brand.aggregate(
{ $match: {
"name" : "Devices"
}},
{ $unwind: "$exten.parameters" },
{ $match: {
'exten.parameters.name': 'Date'
}},
{ $sort: {
'exten.parameters.value': -1
}}
)
The following query can get us the expected output:
db.collection.aggregate([
{
$match:{
"name":"Devices"
}
},
{
$unwind:"$exten.parameters"
},
{
$match:{
"exten.parameters.name":"Date"
}
},
{
$project:{
"name":1,
"exten":1,
"firstParam":{
$arrayElemAt:["$exten.parameters.value",0]
}
}
},
{
$sort:{
"firstParam":1
}
},
{
$project:{
"firstParam":0
}
}
]).pretty()
Data set:
{
"_id" : ObjectId("5da02fb86472ba670fd8c159"),
"name" : "Devices",
"exten" : {
"parameters" : [
{
"name" : "Date",
"value" : [
"5",
"2"
]
},
{
"name" : "Date",
"value" : [
"2",
"7"
]
},
{
"name" : "Time",
"value" : [
"2"
]
},
{
"name" : "Season",
"value" : [
"6"
]
}
]
}
}
Output:
{
"_id" : ObjectId("5da02fb86472ba670fd8c159"),
"name" : "Devices",
"exten" : {
"parameters" : {
"name" : "Date",
"value" : [
"2",
"7"
]
}
}
}
{
"_id" : ObjectId("5da02fb86472ba670fd8c159"),
"name" : "Devices",
"exten" : {
"parameters" : {
"name" : "Date",
"value" : [
"5",
"2"
]
}
}
}

MongoDB Keep path where a criteria is met

I'm new to MongoDB.
In the find query I'm using the following structure:
db.report.find({'accountList.transactionList.description': /.*aear.*/i})
However, accountList contains multiple values, and so does transaction list, the exact query would be:
db.report.find({'accountList[0].transactionList[4].description': /.*aear.*/i})
The problem is that accountList has multiple accounts, and only one of them has the value 'aear' in the description. When I'm executing the query it returns me both accounts, and I'd like to keep only the account where aear is in its description. Also, this MUST be iterable over many files, since it file has different transactionLists, therefore in some documents aear will not appear at all, and in others it might appear multiple types, always in different positions. I believe something must be done in projection, but setting it like this doesn't work:
.projection({"accountList.id":1,"accountList.transactionList.description":1})
Here's the output:
"accountList" : [
{
"id" : "1",
"type" : "xD",
"currency" : "EUR",
"transactionList" : [
{
"onDate" : ISODate("2019-08-25T21:00:00.000-03:00"),
"description" : "aear"
},
{
"onDate" : ISODate("2019-08-25T21:00:00.000-03:00"),
"description" : "bb"
},
{
"onDate" : ISODate("2019-08-25T21:00:00.000-03:00"),
"description" : "cc"
}
]
},
{
"id" : "2",
"type" : "xD",
"currency" : "USD",
"transactionList" : [
{
"onDate" : ISODate("2019-08-15T21:00:00.000-03:00"),
"description" : "aa",
},
{
"onDate" : ISODate("2019-08-14T21:00:00.000-03:00"),
"description" : "ee"
}
]
}
]
And I'd like something like this, where I''m only getting the path to where the condition is met:
"accountList" : [
{
"id" : "1",
"type" : "xD",
"currency" : "EUR",
"transactionList" : [
{
"onDate" : ISODate("2019-08-25T21:00:00.000-03:00"),
"description" : "aear"
},
To accomplish that you need to use aggregate. I believe this code will work in your case:
db.report.aggregate([
{ "$match": { "accountList.transactionList.description": { $regex: "aear", $options: "i"} } },
{ "$unwind": "$accountList" },
{ "$unwind": "$accountList.transactionList" },
{ "$match": { "accountList.transactionList.description": { $regex: "aear", $options: "i"} } },
{ "$group": {
"_id": {
"_id": "$_id",
"accountListId": "$accountList.id",
"accountListType": "$accountList.type",
"accountListCurrency": "$accountList.currency",
},
"transactionList": { "$push": "$accountList.transactionList" }
}},
{ "$group": {
"_id": "$_id._id",
"accountList": {
"$push": {
"id": "$_id.accountListId",
"type": "$_id.accountListType",
"currency": "$_id.accountListCurrency",
"transactionList": "$transactionList"
}
}
}}
])
Updating my answer as this question got updated with new required o/p :
Answer for New Question :
If you've only one transaction matching to given criteria /.*aear.*/i, let's say description is unique across accountList array of report document(exact for provided sample):
db.report.aggregate([{
$match: {
'accountList.transactionList.description': /.*aear.*/i
}
},{ $unwind: '$accountList' },{ $unwind: '$accountList.transactionList' },{$match :{ 'accountList.transactionList.description': /.*aear.*/i}}, { $project: { 'accountList': 1, _id: 0 } }])
But, if you've multiple descriptions (across multiple objects in accountsList array of a report document) matches to given criteria in accountList :
db.report.aggregate([{
$match: {
'accountList.transactionList.description': /.*aear.*/i
}
}, { $unwind: '$accountList' }, { $unwind: '$accountList.transactionList' }, { $match: { 'accountList.transactionList.description': /.*aear.*/i } },
{ $group: { _id: '$_id', accountList: { $push: '$accountList' }, data: { $first: '$$ROOT' } } }
, { $addFields: { 'data.accountList': '$accountList' } }, { $replaceRoot: { 'newRoot': '$data' } }, { $project: { 'accountList': 1, _id: 0 } }
])
Output :
/* 1 */
{
"accountList" : [
{
"id" : "1100",
"type" : "xD",
"currency" : "EUR",
"transactionList" : {
"onDate" : ISODate("2019-08-26T00:00:00.000Z"),
"description" : "aear"
}
},
{
"id" : "1200",
"type" : "xD",
"currency" : "USD",
"transactionList" : {
"onDate" : ISODate("2019-08-16T00:00:00.000Z"),
"description" : "aear"
}
}
]
}
/* 2 */
{
"accountList" : [
{
"id" : "1",
"type" : "xD",
"currency" : "EUR",
"transactionList" : {
"onDate" : ISODate("2019-08-26T00:00:00.000Z"),
"description" : "aear"
}
}
]
}
If in case you've multiple matching descriptions in transaction array & also in other objects of accounts array (this will work for all above scenarios as well but it might not be needed as per requirement, it can be bulky, Check document#3 in Output for clarification) :
db.report.aggregate([
{ "$match": { "accountList.transactionList.description": /.*aear.*/i } },
{ "$unwind": "$accountList" },
{ "$unwind": "$accountList.transactionList" },
{ "$match": { "accountList.transactionList.description": /.*aear.*/i } },
{
"$group": {
"_id": {
"docId": "$_id",
"accountsListObjId": "$accountList.id"
},
"transactionList": { "$push": "$accountList.transactionList" },
"accountList": { "$first": '$accountList' }
}
}
, { $addFields: { 'accountList.transactionList': '$transactionList' } },
{
"$group": {
"_id": "$_id.docId",
"accountList": { $push: '$accountList' }
}
}, { $project: { 'accountList': 1, _id: 0 } }
])
Output :
/* 1 */
{
"accountList" : [
{
"id" : "1100",
"type" : "xD",
"currency" : "EUR",
"transactionList" : [
{
"onDate" : ISODate("2019-08-26T00:00:00.000Z"),
"description" : "aear"
}
]
},
{
"id" : "1200",
"type" : "xD",
"currency" : "USD",
"transactionList" : [
{
"onDate" : ISODate("2019-08-16T00:00:00.000Z"),
"description" : "aear"
}
]
}
]
}
/* 2 */
{
"accountList" : [
{
"id" : "1",
"type" : "xD",
"currency" : "EUR",
"transactionList" : [
{
"onDate" : ISODate("2019-08-26T00:00:00.000Z"),
"description" : "aear"
}
]
}
]
}
/* 3 */
{
"accountList" : [
{
"id" : "00",
"type" : "xD",
"currency" : "EUR",
"transactionList" : [
{
"onDate" : ISODate("2019-08-26T00:00:00.000Z"),
"description" : "aear"
},
{
"onDate" : ISODate("2019-08-26T00:00:00.000Z"),
"description" : "aear"
}
]
},
{
"id" : "100",
"type" : "xD",
"currency" : "USD",
"transactionList" : [
{
"onDate" : ISODate("2019-08-16T00:00:00.000Z"),
"description" : "aear"
}
]
}
]
}
If you're looking for exact text, you can do this as well(cause regex is not allowed in cond) :
db.report.aggregate([
{
$match: {
'accountList.transactionList.description': 'aear'
}
}, { $unwind: '$accountList' }, {
$addFields: {
'accountList.transactionList': {
$filter: {
input: '$accountList.transactionList',
as: 'eachTransaction',
cond: { $eq: ["$$eachTransaction.description", 'aear'] }
}
}
}
}, { $match: { 'accountList.transactionList': { $ne: [] } } }, { $group: { _id: '$_id', accountList: { $push: '$accountList' }, data: { $first: '$$ROOT' } } }
, { $addFields: { 'data.accountList': '$accountList' } }, { $replaceRoot: { 'newRoot': '$data' } }, { $project: { 'accountList': 1, _id: 0 } }])
Output : Same as above.
Answer for Old Question :
Ok you've two options here, Please try these :
If you've only one object in accountList which does matches with the given filter then you can simply do this:
db.report.find({'accountList.transactionList.description': /.*aear.*/i}, {'accountList.$': 1})
Output :
/* 1 */
{
"_id" : ObjectId("5d6435145a0d22d3c86df0c7"),
"accountList" : [
{
"id" : "4474",
"transactionList" : [
{
"description" : "aear"
},
{
"description" : "koe"
}
]
}
]
}
/* 2 */
{
"_id" : ObjectId("5d6435145a0d22d3c86df0d7"),
"accountList" : [
{
"id" : "4400",
"transactionList" : [
{
"description" : "aear"
},
{
"description" : "koe"
}
]
}
]
}
/* 3 */
{
"_id" : ObjectId("5d6435145a0d22d3c86df077"),
"accountList" : [
{
"id" : "0000",
"transactionList" : [
{
"description" : "aear"
},
{
"description" : "koe"
}
]
}
]
}
/* 4 */
{
"_id" : ObjectId("5d6435145a0d22d3c86df1c7"),
"accountList" : [
{
"id" : "0101",
"transactionList" : [
{
"description" : "aear"
},
{
"description" : "koe"
}
]
}
]
}
Downside of above .find () query is it would get only first matching object in accountList, If you've multiple matching objects for given filter in accountList then you need to use aggregation (this aggregation query can be used for earlier scenario as well, Please check output for diff) :
db.report.aggregate([
{
$match: {
"accountList.transactionList.description": /.*aear.*/i
}
},
{ $unwind: "$accountList" },
{
$match: {
"accountList.transactionList.description": /.*aear.*/i
}
}, { $group: { _id: '$_id', accountList: { $push: '$accountList' }, doc: { $first: '$$ROOT' } } }, { $addFields: { 'doc.accountList': '$accountList' } },
{ $replaceRoot: { 'newRoot': '$doc' } }
])
Output :
// This first object is best example where you need aggregation
/* 1 */
{
"_id" : ObjectId("5d6435145a0d22d3c86df1c7"),
"accountList" : [
{
"id" : "0101",
"transactionList" : [
{
"description" : "aear"
},
{
"description" : "koe"
}
]
},
{
"id" : "1111",
"transactionList" : [
{
"description" : "aear"
},
{
"description" : "koe"
}
]
}
]
}
/* 2 */
{
"_id" : ObjectId("5d6435145a0d22d3c86df0d7"),
"accountList" : [
{
"id" : "4400",
"transactionList" : [
{
"description" : "aear"
},
{
"description" : "koe"
}
]
}
]
}
/* 3 */
{
"_id" : ObjectId("5d6435145a0d22d3c86df077"),
"accountList" : [
{
"id" : "0000",
"transactionList" : [
{
"description" : "aear"
},
{
"description" : "koe"
}
]
}
]
}
/* 4 */
{
"_id" : ObjectId("5d6435145a0d22d3c86df0c7"),
"accountList" : [
{
"id" : "4474",
"transactionList" : [
{
"description" : "aear"
},
{
"description" : "koe"
}
]
}
]
}
Try this query:
db.report.find({'accountList[0].transactionList[4].description': { $regex: /.*aear.*/i} })
OR - Which will return only the first matching document:
db.report.find({'accountList[0].transactionList[4].description': /.*aear.*/i}).limit(1)

Return Multiple values from distincts group filters - Mongodb

I want to return two types of group results in one query, but it doen't work.
If you have one idea please share with me.
I have this collection:
[
{
_id: "ABC00001",
results: [
{
_id: "C0001",
status: {
_id: "stj001",
name: "status1"
},
test:{
profession: [
{
"level" : "Pregrado",
"institution" : {
"_id" : "inst006",
"name" : "University 3"
}
},
{
"level" : "Pregrado",
"institution" : {
"_id" : "inst002",
"name" : "University 2"
}
}
]
}
},
{
_id: "C0002",
status: {
_id: "stj002",
name: "status1"
},
test:{
profession: [
{
"level" : "Pregrado",
"institution" : {
"_id" : "inst006",
"name" : "University 3"
}
}
]
}
},
]
},
{
_id: "ABC00002",
results: [
{
_id: "C0001",
status: {
_id: "stj002",
name: "status1"
},
test:{
profession: [
{
"level" : "Pregrado",
"institution" : {
"_id" : "inst002",
"name" : "University 2"
}
},
{
"level" : "Pregrado",
"institution" : {
"_id" : "inst006",
"name" : "University 3"
}
}
]
}
},
{
_id: "C0002",
status: {
_id: "stj003",
name: "status1"
},
test:{
profession: [
{
"level" : "Pregrado",
"institution" : {
"_id" : "inst006",
"name" : "University 3"
}
}
]
}
},
]
},
]
I wanna return only disctincts institutions and status in one group query like this:
institution: [
{"_id" : "inst006","name" : "University 3"},
{"_id" : "inst002", "name" : "University 2"},
]
status: [
{_id: "stj002", name: "status1"},
{_id: "stj003", name: "status1"}
]
I tried with this but doesnt work:
db.collection.aggregate(
[
{'$unwind' : '$results'},
{'$group' : { '_id' : { 'status' : {'_id'=>'$results.status._id', 'name' : '$results.status.name'}, 'count' : { '$sum' : 1 } } } },
{'$group' : { '_id' : { 'institution' : {'_id' :'$results.test.profession.institution._id', 'name':'$results.test.profession.institution.name'},
'count' : { '$sum' 1 } } }
]
)
If I work with two distincts querys with their own group it works but I need only one query returns all values, maybe I'll add more groups
If I understand your requirements correctly then this might work.
db.CollectionName.aggregate([
{"$group" : {_id : {statusid:"$results.status._id", statusname: "$results.status.name", instid:"$results.test.profession.institution._id", instname: "$results.test.profession.institution.name"}},
},
{ "$project": {
"results.status._id": 1,
"results.status.name": 1,
"results.test.profession.institution._id": 1,
"results.test.profession.institution.name": 1
}
},
{ "$sort": { "_id.statusid": 1 }},
])
Note: The JSON data needs to be formatted to make this work.
I found the solution:
db.collection.aggregate([
{'$match' : {'_id' : "CA0001"] ],
{'$unwind' : '$results'],
{'$unwind' : '$results.test'],
{'$unwind' : '$results.test.profession'],
{'$unwind' : '$results.test.skills'],
{'$group' : {
'_id' : {
"status" : {'_id':'$results.status.id', 'name':'$results.status.name'},
"institution" : {'_id':'$results.test.profession.institution._id', 'name':'$results.test.profession.institution.name'},
'profession' => {'_id':'$results.test.profession.education._id', 'description':'$results.test.profession.education.description'},
'availability' : {'_id':'$results.availability._id', 'name':'$results.availability.name'},
'skills' : {'_id':'$results.test.skills._id', 'description':'$results.test.skills.description'}
},
}
},
{'$project' : { 'status' : 1, 'institution': 1, 'profession': 1, 'skills': 1, 'availability': 1} },
{
'$group' : {
'_id' : null,
'status' : {
'$addToSet' : '$_id.status'
},
'institution' : {
'$addToSet' : '$_id.institution'
},
'profession' : {
'$addToSet' : '$_id.profession'
},
'availability' : {
'$addToSet' : '$_id.availability'
},
'skills' : {
'$addToSet' : '$_id.skills'
}
}
}
]);
it returns:
{
"_id": null,
"status": [
{
"_id": 1,
"name": "evaluado"
}
],
"institution": [
{
"_id": "inst078",
"name": "Universidad Privada del Norte"
},
{
"_id": "inst079",
"name": "Universidad San Ignacio de Loyola"
}
],
"profession": [
{
"_id": "fa059",
"description": "Estadística"
},
{
"_id": "fa063",
"description": "Ingeniería Informática"
},
"availability": [
{
"_id": "wo001",
"name": "Inmediata"
}
],
"skills": [
{
"_id": "sk366",
"description": "Pentaho"
}
]
}
All results are distincts.
I reduced time from 550ms to 43ms in programming language, comparing doing with database query and code programming using collections.

Aggregate using $group twice

I've read SO and questions like this one. However I'm not able to build the query I want...
Let's say I have the following data structure:
{
"CAUG" : "id1",
"action" : "actionA",
"date" : ISODate("2017-01-01"),
"hp" : 16
}
{
"CAUG" : "id1",
"action" : "actionB",
"date" : ISODate("2017-01-01"),
"hp" : 17
}
{
"CAUG" : "id1",
"action" : "actionC",
"date" : ISODate("2017-02-10"),
"hp" : 18
}
{
"CAUG" : "id2",
"action" : "actionX",
"date" : ISODate("2018-01-01"),
"hp" : 20
}...
The desired output is something like (not sure about brackets and other stuff...):
{
"CAUG" : "id1",
"timeline" : [
ISODate ("2017-01-01) {
{ "action" : "ActionA", hp : "..." }
{ "action" : "ActionB", hp : "..." }
},
ISODate ("2017-02-10) {
{ "action" : "ActionC", hp : "..." }
}
]
}
{
"CAUG" : "id2",
"timeline" : [
ISODate ("2018-01-01) {
{ "action" : "ActionX", hp : "..." }
}
]
}
At this time my (very limited) query is:
(I've tried many things like composite _id, but I'm alway stucked at some point).
db.aggregate(
[
{ $match: { something } },
{ $project: { something } },
{ $group: {
_id: '$CAUG',
"timeline": { "$push": "$$ROOT" }
}
}
]
)
The problem is I do not know how to do another $group inside timeline array... I'm stucked with the output below... Any clue please? Have a nice weekend.
{
"_id" : "1",
"timeline" : [
{
"CAUG" : "ca220491-ug43816",
"action" : "actionA",
"date" : ISODate("2016-12-21T23:00:00.000+0000")
},
{
"CAUG" : "ca220491-ug43816",
"action" : "actionB",
"date" : ISODate("2016-12-21T23:00:00.000+0000")
},
{
"CAUG" : "ca220491-ug43816",
"action" : "actionC",
"date" : ISODate("2017-02-21T23:00:00.000+0000")
}
]
}
Try running the following aggregate operation:
db.collection.aggregate([
{
"$group": {
"_id": {
"CAUG": "$CAUG",
"date": {
"$dateToString": {
"format": "%Y-%m-%d",
"date": "$date"
}
}
},
"docs": {
"$push": {
"action" : "$action",
"hp" : "$hp"
}
}
}
},
{
"$group": {
"_id": "$_id.CAUG",
"timeline": {
"$push": {
"date": "$_id.date",
"docs": "$docs"
}
}
}
}
])
which gives the sample output
/* 1 */
{
"_id" : "id1",
"timeline" : [
{
"date" : "2017-02-10",
"docs" : [
{
"action" : "actionC",
"hp" : 18.0
}
]
},
{
"date" : "2017-01-01",
"docs" : [
{
"action" : "actionA",
"hp" : 16.0
},
{
"action" : "actionB",
"hp" : 17.0
}
]
}
]
}
/* 2 */
{
"_id" : "id2",
"timeline" : [
{
"date" : "2018-01-01",
"docs" : [
{
"action" : "actionX",
"hp" : 20.0
}
]
}
]
}