Is it possible to have facet to return as an object instead of an array? It seems a bit counter intuitive to need to access result[0].total
instead of just result.total
code (using mongoose):
Model
.aggregate()
.match({
"name": { "$regex": name },
"user_id": ObjectId(req.session.user.id),
"_id": { "$nin": except }
})
.facet({
"results": [
{ "$skip": start },
{ "$limit": finish },
{
"$project": {
"map_levels": 0,
"template": 0
}
}
],
"total": [
{ "$count": "total" },
]
})
.exec()
Each field you get using $facet
represents separate aggregation pipeline and that's why you always get an array. You can use $addFields to overwrite existing total
with single element. To get that first item you can use $arrayElemAt
Model
.aggregate()
.match({
"name": { "$regex": name },
"user_id": ObjectId(req.session.user.id),
"_id": { "$nin": except }
})
.facet({
"results": [
{ "$skip": start },
{ "$limit": finish },
{
"$project": {
"map_levels": 0,
"template": 0
}
}
],
"total": [
{ "$count": "total" },
]
})
.addFields({
"total": {
$arrayElemAt: [ "$total", 0 ]
}
})
.exec()
So i have a object like this: let obj = {'0': {x: 25, y: 12}, '1': {x:55, y:6} ,...} Now How can I push an other object like {x: 12, y: 15} in to this object?
So i have a object like this: let obj = {'0': {x: 25, y: 12}, '1': {x:55, y:6} ,...} Now How can I push an other object like {x: 12, y: 15} in to this object?
I have a small bug in my small coding playground and I just can't find any solution to my error after hours of struggling. I simply want to create an object of a Post class in a app.js file (required ...
I have a small bug in my small coding playground and I just can't find any solution to my error after hours of struggling. I simply want to create an object of a Post class in a app.js file (required ...
I've a problem with my App, normally , ng-class displays a different background according to the time indicated in the database. It works fine on other views ( Home, and when creating ), but in the ...
I've a problem with my App, normally , ng-class displays a different background according to the time indicated in the database. It works fine on other views ( Home, and when creating ), but in the ...
Let's say I have this string: <i id="1"></i><i id="2"></i><i id="3"></i> and this object: { "1": "Red", "2": "Green", "3": "Blue" } Then I want to obtain ...
Let's say I have this string: <i id="1"></i><i id="2"></i><i id="3"></i> and this object: { "1": "Red", "2": "Green", "3": "Blue" } Then I want to obtain ...