Short question:
Is there any solution from the box if log is an array of objects to do something like this (in mongo shell or nodejs or meteor minimongo, anything)?
var log = db.getCollection('myCollection').findOne({_id: 'someId'}).log
var collection = useAsCollection(log);
collection.find(anyValidMongoSelector);
Long question:
I have the document like this one:
{
"_id": "someId",
"log": [{
"operation": "trade",
"date": ISODate("2010-11-12T17:59:04.332+03:00")
}, {
"operation": "sell",
"date": ISODate("2011-11-14T08:53:22.937+03:00")
}, {
"operation": "buy",
"date": ISODate("2014-11-14T12:44:37.202+03:00")
}, {
"operation": "sell",
"date": ISODate("2012-11-15T12:32:40.910+03:00")
}, {
"operation": "buy",
"date": ISODate("2013-11-17T17:43:15.705+03:00")
}, {
"operation": "trade",
"date": ISODate("2018-11-18T08:51:42.518+03:00")
}]
}
I often have to know some specific information associated with log array. For example, the earliest operation.
If the array itself was a collection, I'd just do a search:
db.getCollection('log').find().sort({date: 1})
But such a search can not be made in my case. I know, there is $elemMatch projection, but it returns only the first element matching the condition and have some other limitations.
My solution is to write down in myscript.js some code:
function getEarlier() {
var h1 = db.getCollection('myCollection').findOne({_id: 'someId'}).log,
earliestDate = new Date();
for (var i in h1) {
if (h1[i].date < earliestDate) {
earliestDate = h1[i].date;}}
print('The earliest document: ' + earliestDate);}
getEarlier();
and then execute
mongo myDB myscript.js
The actual doc structure is more complex as well as standing in front of me problems, so i have a lot of such functions iterating this doc.
And the question one more time. Is there any solution from the box to do something like this (in mongo shell or nodejs or meteor minimongo, anything)?
var log = db.getCollection('myCollection').findOne({_id: 'someId'}).log
var collection = useAsCollection(log);
collection.find().sort({date: 1})