How to get arrays on output from MongoDB aggregate - node.js

I have documents in MongoDB, like so:
{
"_id" : ObjectId("5a748c8b178227d602ec9ce8"),
"dateHour" : ISODate("2018-02-02T16:00:00.000Z"),
"data" : [
{
"date" : ISODate("2018-02-02T16:06:35.033Z"),
"cap" : 437105726836.0
},
{
"date" : ISODate("2018-02-02T16:09:25.127Z"),
"cap" : 437316498502.0
},
...
]
}
Using aggregate method (in NodeJS):
db.getCollection('hourly').aggregate([
{$match: {}},
{$unwind: "$data"},
{$project: {_id: 0, date: "$data.date", cap: "$data.cap" } }
])
I get output like:
[
{
"date" : ISODate("2018-02-02T16:06:35.033Z"),
"cap" : 437105726836.0
},
{
"date" : ISODate("2018-02-02T16:09:25.127Z"),
"cap" : 437316498502.0
}
]
QUESTION: What is the most effective way to get output like so:
[
[ISODate("2018-02-02T16:06:35.033Z"), 437105726836.0],
[ISODate("2018-02-02T16:09:25.127Z"), 437316498502.0]
]
?
I can simply add .map(function(item) {return [item.date, item.cap]}) but is this most effective way when working with huge amount of data?

try $project with $map or $reduce
$map
db.col.aggregate(
[
{$project : {
_id : 0,
data : {$map : {input : "$data", as : "d", in : ["$$d.date", "$$d.cap"]}}
}
}
]
)
$reduce
db.col.aggregate(
[
{$project : {
_id : 0,
data : {$reduce : {input : "$data", initialValue : [], in : {$concatArrays : ["$$value", [["$$this.date", "$$this.cap"]]]}}}
}
}
]
).pretty()
output
{
"data" : [
[
ISODate("2018-02-02T16:06:35.033Z"),
437105726836
],
[
ISODate("2018-02-02T16:09:25.127Z"),
437316498502
]
]
}

The root has to be a document, proof:
db.test.aggregate([
{$unwind: "$data"},
{ $replaceRoot: { newRoot: ["$data.date", "$data.cap"] } }
]);
assert: command failed: {
"ok" : 0,
"errmsg" : "'newRoot' expression must evaluate to an object, but resulting value was: [null, null]. Type of resulting value: 'array'. Input document: {date: 2018-02-02T16:06:35.033Z, cap: 4.37106e+11}",
"code" : 40228,
"codeName" : "Location40228"
} : aggregate failed
You could, however, project it into an array within a document:
> db.test.aggregate([
... {$unwind: "$data"},
... { $replaceRoot: { newRoot: {a:["$data.date", "$data.cap"] } }}
... ])
{ "a" : [ ISODate("2018-02-02T16:06:35.033Z"), 437105726836 ] }
{ "a" : [ ISODate("2018-02-02T16:09:25.127Z"), 437316498502 ] }

It's in the projection. Try this:
db.getCollection('hourly').aggregate([
{$match: {}},
{$unwind: "$data"},
{$project: {_id: 0, date: ["$data.date", "$data.cap"] } }
]);
Just in case my syntax is a little off, here is MongoDb documentation to project a new array.
I don't see why you need aggregate.
Why not:
db.getCollection('hourly').find({}, {data: 1}, (err, results) => {
// manage results here.
});

Related

how to get data in mongoose where last element in array?

how to get data in mongoose where last element in array?
I have data looks like this:
[
{
"_id" : ObjectId("5b56eb3deb869312d85a8e69"),
"transactionStatus" : [
{
"status" : "pending",
"createdAt" : ISODate("2018-07-24T09:02:53.347Z")
},
{
"status" : "process",
"createdAt" : ISODate("2018-07-24T09:02:53.347Z")
}
]
},
{
"_id" : ObjectId("5b56eb3deb869312d8589765"),
"transactionStatus" : [
{
"status" : "pending",
"createdAt" : ISODate("2018-07-24T09:02:53.347Z")
},
{
"status" : "process",
"createdAt" : ISODate("2018-07-24T09:03:30.347Z")
},
{
"status" : "done",
"createdAt" : ISODate("2018-07-24T09:04:22.347Z")
}
]
}
]
And, I want to get data above where last object transactionStatus.status = process, so the result should be:
{
"_id" : ObjectId("5b56eb3deb869312d85a8e69"),
"transactionStatus" : [
{
"status" : "pending",
"createdAt" : ISODate("2018-07-24T09:02:53.347Z")
},
{
"status" : "process",
"createdAt" : ISODate("2018-07-24T09:02:53.347Z")
}
]
}
how to do that with mongoose?
You can use $expr (MongoDB 3.6+) inside of match. Using $let and $arrayElemAt passing -1 as second argument you can get the last element as a temporary variable and then you can compare the values:
db.col.aggregate([
{
$match: {
$expr: {
$let: {
vars: { last: { $arrayElemAt: [ "$transactionStatus", -1 ] } },
in: { $eq: [ "$$last.status", "process" ] }
}
}
}
}
])
The same result can be achieved for lower versions of MongoDB using $addFields and $match. You can add $project then to remove that temporary field:
db.col.aggregate([
{
$addFields: {
last: { $arrayElemAt: [ "$transactionStatus", -1 ] }
}
},
{
$match: { "last.status": "process" }
},
{
$project: { last: 0 }
}
])
//Always update new status at Position 0 using $position operator
db.update({
"_id": ObjectId("5b56eb3deb869312d85a8e69")
},
{
"$push": {
"transactionStatus": {
"$each": [
{
"status": "process",
"createdAt": ISODate("2018-07-24T09:02:53.347Z")
}
],
"$position": 0
}
}
}
)
//Your Query for checking first element status is process
db.find(
{
"transactionStatus.0.status": "process"
}
)
refer $position, $each

MongoDB find documents which have multiple keys in a field

This is how a documents looks like in my dataset:
{
username: 'stack',
attempts: { 1517761701: false, 1512341532: true }
}
{
username: 'overflow',
attempts: { 1217563721: false }
}
Now, I want to retrieve every document in my dataset where attempts contains more than óne key. So the query should return the document of user 'stack' but not of user 'overflow'. What query can I apply here?
try $objectToArray to convert object to array and count the number of keys if you are using mongo 3.6+
db.cols.aggregate(
[
{$addFields: {count : {$size : {$ifNull : [{$objectToArray : "$attempts"}, []]}}}},
{$match: {count : {$gt : 1}}},
{$project: {count : 0}}
]
)
Use $redact for a single pipeline:
db.collection.aggregate([
{
"$redact": {
"$cond": [
{
"$gt": [
{ "$size": {
"$objectToArray": {
"$ifNull": [
"$attempts",
{ }
]
}
} },
1
]
},
"$$KEEP",
"$$PRUNE"
]
}
}
])
Convert attempts to an array, $unwind and then use $sortByCount to do all the work..
db.collection_name.aggregate( [
{ $addFields : { array : { $objectToArray : "$attempts"} } },
{ $unwind : "$array" },
{ $sortByCount : "$username" },
{ $match : { count : { $gte : 2 } } }
])
Outputs:
{
"_id" : "stack",
"count" : 2
}

Searching value in 2 different fields mongodb + node.js

I am newbie. But I try to learn the most logical ways to write the queries.
Assume I have collection which is as;
{
"id" : NumberInt(1),
"school" : [
{
"name" : "george",
"code" : "01"
},
{
"name" : "michelangelo",
"code" : "01"
}
],
"enrolledStudents" : [
{
"userName" : "elisabeth",
"code" : NumberInt(21)
}
]
}
{
"id" : NumberInt(2),
"school" : [
{
"name" : "leonarda da vinci",
"code" : "01"
}
],
"enrolledStudents" : [
{
"userName" : "michelangelo",
"code" : NumberInt(25)
}
]
}
I want to list occurence of a key with their corresponding code values.
As an example key : michelangelo
To find the occurence of the key, I wrote two differen aggregation queries as;
db.test.aggregate([
{$unwind: "$school"},
{$match : {"school.name" : "michelangelo"}},
{$project: {_id: "$id", "key" : "$school.name", "code" : "$school.code"}}
])
and
db.test.aggregate([
{$unwind: "$enrolledStudents"},
{$match : {"enrolledStudents.userName" : "michelangelo"}},
{$project: {_id: "$id", "key" : "$enrolledStudents.userName", "code" : "$enrolledStudents.code"}}
])
the result of these 2 queries return what I want as;
{ "_id" : 1, "key" : "michelangelo", "code" : "01" }
{ "_id" : 2, "key" : "michelangelo", "code" : 25 }
One of them to search in enrolledStudents, the other one is searching in school field.
Can these 2 queries reduced into more logical query? Or is this the only way to do it?
ps: I am aware that database structure is not logical, but I tried to simulate.
edit
I try to write a query with find.
db.test.find({$or: [{"enrolledStudents.userName" : "michelangelo"} , {"school.name" : "michelangelo"}]}).pretty()
but this returns the whole documents as;
{
"id" : 1,
"school" : [
{
"name" : "george",
"code" : "01"
},
{
"name" : "michelangelo",
"code" : "01"
}
],
"enrolledStudents" : [
{
"userName" : "elisabeth",
"code" : 21
}
]
}
{
"id" : 2,
"school" : [
{
"name" : "leonarda da vinci",
"code" : "01"
}
],
"enrolledStudents" : [
{
"userName" : "michelangelo",
"code" : 25
}
]
}
Mongo 3.4
$match - This stage will keep all the school array and enrolledStudents where there is atleast one embedded document matching both the query condition
$group - This stage will combine all the school and enrolledStudents array to 2d array for each _id in a group.
$project - This stage will $filter the merge array for matching query condition and $map the array to with new labels values array.
$unwind - This stage will flatten the array.
$addFields & $replaceRoot - This stages will add the id field and promote the values array to the top.
db.collection.aggregate([
{$match : {$or: [{"enrolledStudents.userName" : "michelangelo"} , {"school.name" : "michelangelo"}]}},
{$group: {_id: "$id", merge : {$push:{$setUnion:["$school", "$enrolledStudents"]}}}},
{$project: {
values: {
$map:
{
input: {
$filter: {
input: {"$arrayElemAt":["$merge",0]},
as: "onef",
cond: {
$or: [{
$eq: ["$$onef.userName", "michelangelo"]
}, {
$eq: ["$$onef.name", "michelangelo"]
}]
}
}
},
as: "onem",
in: {
key : { $ifNull: [ "$$onem.userName", "$$onem.name" ] },
code : "$$onem.code"}
}
}
}
},
{$unwind: "$values"},
{$addFields:{"values.id":"$_id"}},
{$replaceRoot: { newRoot:"$values"}}
])
Sample Response
{ "_id" : 2, "key" : "michelangelo", "code" : 25 }
{ "_id" : 1, "key" : "michelangelo", "code" : "01" }
Mongo <= 3.2
Replace last two stages of above aggregation with $project to format the response.
{$project: {"_id": 0 , id:"$_id", key:"$values.key", code:"$values.code"}}
Sample Response
{ "_id" : 2, "key" : "michelangelo", "code" : 25 }
{ "_id" : 1, "key" : "michelangelo", "code" : "01" }
You can use $redact instead of $group & match and add $project with $map to format the response.
$redact to go through a document level at a time and perform $$DESCEND and $$PRUNE on the matching criteria.
The only thing to note is usage of $ifNull in the first document level for id so that you can $$DESCEND to embedded document level for further processing.
db.collection.aggregate([
{
$redact: {
$cond: [{
$or: [{
$eq: ["$userName", "michelangelo"]
}, {
$eq: ["$name", "michelangelo"]
}, {
$ifNull: ["$id", false]
}]
}, "$$DESCEND", "$$PRUNE"]
}
},
{
$project: {
id:1,
values: {
$map:
{
input: {$setUnion:["$school", "$enrolledStudents"]},
as: "onem",
in: {
key : { $ifNull: [ "$$onem.userName", "$$onem.name" ] },
code : "$$onem.code"}
}
}
}
},
{$unwind: "$values"},
{$project: {_id:0,id:"$id", key:"$values.key", code:"$values.code"}}
])

Mongo Node driver how to get all fields of $max aggregate from an array of objects

I have a collection called "products" which has an array of "bids" objects.
I want to find out the Maximum bid for each product, for this I am aggregating Products on $max with $bids.bidamount field. However this is only giving me the largest bid amount. How do I project all the bid fields for the max aggregation.
Here is a sample document
{
"_id" : ObjectId("58109a5138fe12215cfdc064"),
"product_id" : 2,
"item_name" : "Auction Item1",
"item_description" : "Test",
"seller_name" : "ak#gmail.com",
"item_price" : "20",
"item_quantity" : 7,
"sale_type" : "Auction",
"posted_at" : "2016:10:26 04:58:09",
"expires_at" : "2016:10:30 04:58:09",
"bids" : [
{
"bid_id" : 1,
"bidder" : "ak#gmail.com",
"bid_amount" : 300,
"bit_time" : "2016:10:26 22:36:29"
},
{
"bid_id" : 2,
"bidder" : "ak#gmail.com",
"bid_amount" : 100,
"bit_time" : "2016:10:26 22:37:29"
}
],
"orders" : [
{
"buyer" : "ak#gmail.com",
"quantity" : "2"
},
{
"buyer" : "ak#gmail.com",
"quantity" : "3"
}
]
}
Here is my mongo query:
db.products.aggregate([
{
$project: {
bidMax: { $max: "$bids.bid_amount"}
}
}
])
which gives the following result:
{
"_id" : ObjectId("58109a5138fe12215cfdc064"),
"bidMax" : 300
}
db.products.aggregate([{$unwind:"$bids"},{$group:{_id:"$_id", sum:{$sum:"$bids.bid_amount"}}},{$project:{doc:"$$ROOT", _id:1, sum:1}, {$sort:{"sum":-1}},{$limit:1}]),
which return something like { "_id" : ObjectId("5811b667c50fb1ec88227860"), "sum" : 600, doc:{your document....} }
This should do it:
db.products.aggregate([{
$unwind: '$bids'
}, {
$group: {
_id: '$products_id',
maxBid: {
$max: '$bids.bid_amount'
}
}
}])
db.collectionName.aggregate(
[
{
$group:
{
_id: "$product_id",
maxBidAmount: { $max: "$bids.bid_amount" }
}
}
]
)
Hey use this query, you will get the result.

Find sub-documents using $in with MongoDB

My task is to find individual authors(comments.user_id) comment on the article (_id)
{
"_id" : ObjectId("56479d9c8510369a4ecea3a9"),
"comments" : [
{
"text" : "222",
"user_id" : ObjectId("563f2db0e2bf6c431b297d45"),
},
{
"text" : "333",
"user_id" : ObjectId("563f2db0e2bf6c431b297d45"),
},
{
"text" : "444",
"user_id" : ObjectId("563f2db0e2bf6c431b297d45"),
},
{
"text" : "55555",
"user_id" : ObjectId("563e3337e2bf6c431b297d41"),
},
{
"text" : "00000",
"user_id" : ObjectId("563f7c0a8db7963420cd5732"),
},
{
"text" : "00001",
"user_id" : ObjectId("563f7c0a8db7963420cd5732"),
}
]
}
My query looks as follows
db.getCollection('messages').find({
'_id': ObjectId("56479d9c8510369a4ecea3a9"),
'comments.user_id': {$in : [
ObjectId("563e3337e2bf6c431b297d41"),
ObjectId("563f7c0a8db7963420cd5732")
]}
})
It returns all comments. Please help to understand why it happens.
Expected Result
{
"_id" : ObjectId("56479d9c8510369a4ecea3a9"),
"comments" : [
{
"text" : "55555",
"user_id" : ObjectId("563e3337e2bf6c431b297d41"),
},
{
"text" : "00000",
"user_id" : ObjectId("563f7c0a8db7963420cd5732"),
},
{
"text" : "00001",
"user_id" : ObjectId("563f7c0a8db7963420cd5732"),
}
]
}
update query (hopelessness)
db.getCollection('messages').find(
{'_id': ObjectId("56479d9c8510369a4ecea3a9")},
{'comments.user_id': {$in: ["563f2db0e2bf6c431b297d45", "563e3337e2bf6c431b297d41"]}},
{'comments.user_id': {$elemMatch: {$in: ["563f2db0e2bf6c431b297d45", "563e3337e2bf6c431b297d41"]}}}
)
db.getCollection('messages').find(
{'_id': ObjectId("56479d9c8510369a4ecea3a9")},
{comments: {$elemMatch: {'user_id': {$in : [ObjectId("563f2db0e2bf6c431b297d45"), ObjectId("563f7c0a8db7963420cd5732")]}}}}
)
I return only 1 record, and I have all the records from these authors
As you've seen, the $ and $elemMatch projection operators only include the first matching element.
To include multiple, filtered array elements in your projection of the comment array, your can use aggregate with the $redact operator instead of find:
db.getCollection('test').aggregate([
{$match: {
'_id': ObjectId("56479d9c8510369a4ecea3a9"),
'comments.user_id': {$in : [
ObjectId("563e3337e2bf6c431b297d41"),
ObjectId("563f7c0a8db7963420cd5732")
]},
}},
{$redact: {
$cond: {
if: {
$or: [
{$eq: ['$user_id', ObjectId("563e3337e2bf6c431b297d41")]},
{$eq: ['$user_id', ObjectId("563f7c0a8db7963420cd5732")]},
{$not: '$user_id'}
]
},
then: '$$DESCEND',
else: '$$PRUNE'
}
}}
])
$redact iterates over each doc like a tree, keeping or trimming the fields of each doc as it's $cond expression dictates.
It gets a bit tricky to wrap your head around $redact, but it's basically saying that if the level's user_id field matches either of the two ObjectIds in your $in, or it's not present (i.e. as it is in the top level of the doc), include the data, otherwise remove it.

Resources