wrong counting with $filter - node.js

My db cofiguration looks like:
{
"_id" : ObjectId("5ece47aa6510a611b47aac5a"),
"boats" : [
{
"_id" : ObjectId("5ece47aa6510a611b47aac6e"),
"model" : "Dufour",
"year" : 2019,
"about" : [
{
"_id" : ObjectId("5ece47aa6510a611b47aac71"),
"Capacity" : 14,
"characteristics" : [
{
"_id" : ObjectId("5ece47aa6510a611b47aac73"),
"fuel" : "petrol",
"fuelCap" : 200
},
{
"_id" : ObjectId("5ece47aa6510a611b47aac73"),
"fuel" : "petrol",
"fuelCap" : 120
},
]
},
{
"_id" : ObjectId("5ece47aa6510a611b47aac71"),
"Capacity" : 8,
"characteristics" : [
{
"_id" : ObjectId("5ece47aa6510a611b47aac73"),
"fuel" : "benzin",
"fuelCap" : 180
},
{
"_id" : ObjectId("5ece47aa6510a611b47aac73"),
"fuel" : "petrol",
"fuelCap" : 100
},
]
},
{...},
{...},
]
}
Now i am trying to count the number of boats which have "fuel" : "petrol", so i use the code bellow:
router.get('/boat', async(req, res)=>{
try{
const fuelData = await Boat.aggregate([
{
$project: {
fuelData: {
$filter: {
input: "$boats",
as: "boats",
cond: {
$filter:{
input:"$$boats.about",
as:"about",
cond:{
$filter:{
input:"$$about.characteristics",
as:"characteristics",
cond:{
$eq:["$$activity1.activity.type", "STILL"]
}
}
}
}
}
}
}
}
},
{
$project: {
boatsCount: {$size : "$fuelData" }
}
}
])
res.status(201).send(fuelData)
}catch(e){
res.send(e)
}
})
The problem is that return wrong number of boatCount. And it seems like it returns the number of the boats which are inside the db. Any help how to count correctly the boats which have "fuel" : "petrol"?
Is there anything wrong in my code?

https://mongoplayground.net/p/D0FEhTMJEJ1
Hope this is what you need.
The sample data you have provided is missing a ] & }.
So I added 1 additional boat with 2 about.
db.collection.aggregate([
{
$match: {
"boats.about.characteristics.fuel": "petrol"
}
},
{
$unwind: "$boats"
},
{
$unwind: "$boats.about"
},
{
$match: {
"boats.about.characteristics.fuel": "petrol"
}
},
{
$group: {
_id: null,
count: {
$sum: 1
}
}
}
])

Related

Get _id to collect another collection detail in expressjs mongodb

collection 'bookborrow'
{
"_id" : ObjectId("62f66aa9b744696c2d08cade"),
"userId" : "62f492ace559e59ee288b73a",
"data" : {
"bookname" : "62e53c8af5d4c45fb7853d9f",
"todayDate" : "12/08/2022",
"dateofreturn" : "27/08/2022"
}
}
collection 'bookdetails'
{
"_id" : ObjectId("62e53c8af5d4c45fb7853d9f"),
"bookname" : "Steve Jobs",
"bookauthor" : "Walter Isaacson ",
"counterbooks" : "5",
"bookPublisher" : "Simon & Schuster "
}
i need bookborrow collection data.bookname in bookdetails collection (bookborrow data.bookname(62e53c8af5d4c45fb7853d9f) === _id (62e53c8af5d4c45fb7853d9f) bookdetails
i used aggregate method
const getHistoryData=await db.get().collection('bookborrow').aggregate([
{
$lookup: {
from: 'bookdetails',
let: {
bookid:
{$toObjectId: "$data.bookname"}
},
pipeline: [
{
$match: {
$expr: {
$eq: [
'$_id',
'$$bookid'
]
}
}
},
{
$project: {
bookname: 1
}
}
],
as: 'getbookdetails'
}
},
{
$unwind: '$bookid'
},
{
$project: {
_id: 1,
bookname: '$getbookdetails.bookname',
returndate: 1
}
}
])
output looklike :=
"bookname" : "Steve Jobs"
"todayDate" : "12/08/2022"
"dateofreturn" : "27/08/2022"

Mongoose aggregation not returning latest document

I have an aggregation that returns a document, but I need it to return a document that is the latest one in the collection messageTrackingId. The one that has the most recent value in the creationDate field I have a sort to accomplish this, but I keep getting returned documents that aren't the latest and I'm not sure why. The purpose is to show the user the time stamp of the last message sent between that user and another user regardless of who sent it. appreciate any help.
app.get("/api/messages", (req, res, next) => {
query = {};
inbox = false;
messageId = false;
console.log(req.query.recipientId);
if (req.query.recipientId) {
query = { recipientId: req.query.recipientId };
inbox = true;
Messages.aggregate([
{
$match: {
$or: [
{ recipientId: req.query.recipientId },
{ creator: mongoose.Types.ObjectId(req.query.recipientId) }
]
}
},
{
$addFields: {
conversant: {
$cond: [
{ $ne: ["$recipientId", req.query.recipientId] },
"$recipientId",
"$creator"
]
}
}
},
{
$sort: { creationDate: 1 }
},
{
$group: {
_id: "$conversant",
message: { $last: "$message" },
recipientId: { $last: "$recipientId" },
creator: { $last: "$creator" },
messageTrackingId: { $last: "$messageTrackingId" },
recipient: { $last: "$recipient" },
creationDate: { $last: "$creationDate" }
}
},
{
$lookup: {
from: "users",
let: { creator: "$creator" },
pipeline: [
{ $match: { $expr: { $eq: ["$_id", "$$creator"] } } },
{ $project: { instagramName: 1 } }
],
as: "creatorName"
}
}
]).then(documents => {
if (inbox === true) {
res.status(200).json({
message: "User's Inbox Retrieved!",
posts: documents
});
}
});
I cleared out the message table and sent 3 messsages back and forth to demonstrate. When I reload the page, I only receive 2 of the docs in the JSON response instead of the latest one. Of the 2 received, none of them seem to be the latest either. I'm not sure why it's not returning one. Please see output on page load below.
Messages :
/* 1 */
{
"_id" : "5e16463be5fcba1d2c56ebb7",
"creator" : "5e0a1d41f86e1e4234fc1a77",
"recipient" : "andy",
"recipientId" : "5df0014e25ee451beccf588a",
"message" : "First message",
"messageTrackingId" : "17c4fc99-d92f-46ef-9bae-1408f415287a",
"creatorName" : "buyer1",
"creationDate" : ISODate("2020-01-08T21:14:35.104Z"),
"__v" : 0.0
}
/* 2 */
{
"_id" : "5e16464de5fcba1d2c56ebb9",
"creator" : "5df0014e25ee451beccf588a",
"recipient" : "buyer1",
"recipientId" : "5e0a1d41f86e1e4234fc1a77",
"message" : "Second Message",
"messageTrackingId" : "17c4fc99-d92f-46ef-9bae-1408f415287a",
"creatorName" : "andy",
"creationDate" : ISODate("2020-01-08T21:14:53.618Z"),
"__v" : 0.0
}
/* 3 */
{
"_id" : "5e16465ce5fcba1d2c56ebbb",
"creator" : "5e0a1d41f86e1e4234fc1a77",
"recipient" : "andy",
"recipientId" : "5df0014e25ee451beccf588a",
"message" : "Third Message",
"messageTrackingId" : "17c4fc99-d92f-46ef-9bae-1408f415287a",
"creatorName" : "buyer1",
"creationDate" : ISODate("2020-01-08T21:15:08.500Z"),
"__v" : 0.0
}
users :
/* 1 */
{
"_id" : "5df00d08c713f722909c99c1",
"email" : "joe#gmail.com",
"password" : "$2b$10$5eWhwL4RNT8TyQRCC191J.myYBwFcJ8hCARqGfofYWUmRaq6jJFQm",
"instagramName" : "joe",
"over21" : "Yes",
"role" : "Artist",
"fullName" : "testdsfdfsfs",
"address1" : "adfsdf",
"address2" : "dssdf",
"city" : "sdfsdfsdf",
"state" : "dsffsdsd",
"zip" : "dsdfsdf",
"passwordCreated" : ISODate("2020-01-07T23:46:57.861Z"),
"__v" : 0
}
/* 2 */
{
"_id" : "5df0014e25ee451beccf588a",
"email" : "andy#gmail.com",
"password" : "$2b$10$fNHmIoDb4mX6x.YeMLXHbu2yIaeW6HVQvNpZR8Nt/a4xVFkhxM1Ey",
"instagramName" : "andy",
"over21" : "Yes",
"role" : "Artist",
"fullName" : "dsfdsfdfdsf",
"address1" : "111",
"address2" : "111",
"city" : "atlanta",
"state" : "ga",
"zip" : "33222",
"passwordCreated" : ISODate("2019-12-19T15:29:46.528Z"),
"__v" : 0
}
Required output (Return last message from each user) :
/* 1 */
{
"_id" : {
"conversant" : "5df0014e25ee451beccf588a",
"recipient" : "andy"
},
"message" : "Third Message",
"recipientId" : "5df0014e25ee451beccf588a",
"creator" : "5e0a1d41f86e1e4234fc1a77",
"messageTrackingId" : "17c4fc99-d92f-46ef-9bae-1408f415287a",
"recipient" : "andy",
"creationDate" : ISODate("2020-01-08T21:15:08.500Z"),
"creatorName" : [
{
"_id" : "5df0014e25ee451beccf588a",
"instagramName" : "andy"
}
]
}
/* 2 */
{
"_id" : {
"conversant" : "5df0014e25ee451beccf588a",
"recipient" : "buyer1"
},
"message" : "Second Message",
"recipientId" : "5e0a1d41f86e1e4234fc1a77",
"creator" : "5df0014e25ee451beccf588a",
"messageTrackingId" : "17c4fc99-d92f-46ef-9bae-1408f415287a",
"recipient" : "buyer1",
"creationDate" : ISODate("2020-01-08T21:14:53.618Z"),
"creatorName" : [
{
"_id" : "5df0014e25ee451beccf588a",
"instagramName" : "andy"
}
]
}
Changes has to be
1) If I understand correctly at this stage you wanted to group by recipientId on a field(since you can't group on two fields for a single value) So you're creating a new field called conversant, So it has to be $eq or you need to swap recipientId & creator & also it has to be $toString:
{
$addFields: {
conversant: {
$cond: [
{ $eq: ["$recipientId", req.query.recipientId] },
"$recipientId",
{$toString : "$creator"}
]
}
}
}
2) You don't need this $sort stage, as all latest additions to collection will be added at the end (Expecting creationDate is once created but never updated) :
// Not useful
{
$sort: { creationDate: 1 }
}
Also you need to group by _id: { conversant: "$conversant", recipient: '$recipient' } for group on last messages based on user. In $lookup change to this let: { conversant: "$_id.conversant" }.
Try this :
Messages.aggregate([
{
$match: {
$or: [
{ recipientId: req.query.recipientId },
{ creator: mongoose.Types.ObjectId(req.query.recipientId) }
]
}
},
{
$addFields: {
conversant: {
$cond: [
{ $eq: ["$recipientId", req.query.recipientId] },
"$recipientId",
{ $toString: "$creator" }
]
}
}
},
{
$group: {
_id: { conversant: "$conversant", recipient: '$recipient' },
message: { $last: "$message" },
recipientId: { $last: "$recipientId" },
creator: { $last: "$creator" },
messageTrackingId: { $last: "$messageTrackingId" },
recipient: { $last: "$recipient" },
creationDate: { $last: "$creationDate" }
}
},
{
$lookup: {
from: "users",
let: { conversant: "$_id.conversant" },
pipeline: [
{ $match: { $expr: { $eq: ["$_id", "$$conversant"] } } },
{ $project: { instagramName: 1 } }
],
as: "creatorName"
}
},
// this is optional only if you wanted to return latest document irrespective of user.
{ $sort: { creationDate: -1 } }
])

How to get group with groupby get the result with dynamic status in mongoDB

I have the collection like below
{
"_id" : ObjectId("5b6538704ba0292b6c197770"),
"Name":"Name1",
"Status":"Good",
},
{
"_id" : ObjectId("5b6538704ba0292b6c197773"),
"Name":"Name2",
"Status":"Bad"
},
{
"_id" : ObjectId("5b6538704ba0292b6c197774"),
"Name":"Name3",
"Status":"Bad"
},{
"_id" : ObjectId("5b6538704ba0292b6c197774"),
"Name":"Name1",
"Status":"Bad"
},
{
"_id" : ObjectId("5b6538704ba0292b6c197775"),
"Name":"Name1",
"Status":"Good"
}
I have used the query to get the status wise count like below
db.Students.aggregate( [
{ $group: { _id: {
"Name": "$Name","Status":"$Status"}, StatusCount: { $sum: 1 } } }
, { "$project": { _id: 0, Name: "$_id.Name",Status : "$_id.Status", StatusCount:1 } }
] );
The result was
{
"Name":"Name1",
"StatusCount" : 2,
"Status" : "Good"
},
{
"Name":"Name2",
"StatusCount" : 1,
"Status" : "Bad"
}, {
"Name":"Name2",
"StatusCount" : 1,
"Status" : "Bad"
},
{
"Name":"Name1",
"StatusCount" : 1,
"Status" : "Bad"
}
The result what I am approaching is like
{
"Name":"Name1",
"Good" : 2,
"Bad" :1
},
{
"Name":"Name2",
"Good" : 0,
"Bad" :1
}
The result I am expecting to have the status of field names and count as its values. I have tried to do this but I could not make it happen. The status, for now, is only two like Good or Bad but may increase in real dataset.
By using the $arrayToObject operator and a final $replaceRoot pipeline step which has a $mergeObjects operator you will get your desired result.
You would need to run the following aggregate pipeline on MongoDB Server 3.4.4 or newer:
const pipeline = [
{ '$group': {
'_id': {
'Name': '$Name',
'Status': '$Status'
},
'StatusCount': { '$sum': 1 }
} },
{ '$group': {
'_id': '$_id.Name',
'counts': {
'$push': {
'k': '$_id.Status',
'v': '$StatusCount'
}
}
} },
{ '$replaceRoot': {
'newRoot': { '$mergeObjects': [
{ '$arrayToObject': '$counts' },
{ 'Name': '$_id' }
] }
} }
];
db.Students.aggregate(pipeline);

Get array elements across all documents in the collection that match a specific array element content

I have the following document structure in my MongoDB and I am trying to return an array of objects containing all prices for itemID "5a59c587fa9b4a212b0a1312" across all documents using the following query but unfortunately it is always returning an empty array. Can someone please advice what I might be doing wrong here? and how I can get such a result?
Note: I am using promised-mongo in a Node.js app to access my MongoDB
Query I tried:
{ transDetails: { $elemMatch: { itemID: "5a59c587fa9b4a212b0a1312" } } }
DB sample:
{
"_id" : ObjectId("5a688e7ea52deb6d4a6b6663"),
"transactionID" : "1",
"transDetails" : [
{
"itemID" : "5a59c587fa9b4a212b0a1312",
"price" : "22"
},
{
"itemID" : "5a59c95b081c6c612bd17058",
"price" : "24"
}
] }
{
"_id" : ObjectId("5a6aa99a52deb6d4a67714"),
"transactionID" : "2",
"transDetails" : [
{
"itemID" : "5a59c587fa9b4a212b0a1312",
"price" : "35"
},
{
"itemID" : "5a59c95b081c6c612bd17058",
"price" : "24"
}
] }
Find with projection to have only matched items in the transDetails:
.find({"transDetails.itemID": "5a59c587fa9b4a212b0a1312"}, {_id:0, "transDetails.$": 1})
Will return
{
"transDetails" : [
{
"itemID" : "5a59c587fa9b4a212b0a1312",
"price" : "22"
}
]
},
{
"transDetails" : [
{
"itemID" : "5a59c587fa9b4a212b0a1312",
"price" : "35"
}
]
},
....
Wish you re-shape the documents, you can use aggregation:
.aggregate([
{ $match: { "transDetails.itemID": "5a59c587fa9b4a212b0a1312" } },
{ $project: {
_id: 0,
transDetails: {
$filter: {
input: "$transDetails",
as: "item",
cond: { $eq: [ "$$item.itemID", "5a59c587fa9b4a212b0a1312" ] }
}
}
} },
{ $unwind: "$transDetails"},
{ $project: {price: "$transDetails.price"}}
])
Which will give you
{
"price" : "22"
},
{
"price" : "35"
},
...

MongoDB aggregate on Nested data

I have nested data as below,
{
"_id" : ObjectId("5a30ee450889c5f0ebc21116"),
"academicyear" : "2017-18",
"fid" : "be02",
"fname" : "ABC",
"fdept" : "Comp",
"degree" : "BE",
"class" : "1",
"sem" : "8",
"dept" : "Comp",
"section" : "Theory",
"subname" : "BDA",
"fbValueList" : [
{
"_id" : ObjectId("5a30eecd3e3457056c93f7af"),
"score" : 20,
"rating" : "Fair"
},
{
"_id" : ObjectId("5a30eefd3e3457056c93f7b0"),
"score" : 10,
"rating" : "Fair"
},
{
"_id" : ObjectId("5a337e53341bf419040865c4"),
"score" : 88,
"rating" : "Excellent"
},
{
"_id" : ObjectId("5a337ee2341bf419040865c7"),
"score" : 75,
"rating" : "Very Good"
},
{
"_id" : ObjectId("5a3380b583dde50ddcea350e"),
"score" : 72,
"rating" : "Very Good"
}
]
},
{
"_id" : ObjectId("5a3764f1bc19b77dd9fd9a57"),
"academicyear" : "2017-18",
"fid" : "be02",
"fname" : "ABC",
"fdept" : "Comp",
"degree" : "BE",
"class" : "1",
"sem" : "5",
"dept" : "Comp",
"section" : "Theory",
"subname" : "BDA",
"fbValueList" : [
{
"_id" : ObjectId("5a3764f1bc19b77dd9fd9a59"),
"score" : 88,
"rating" : "Excellent"
},
{
"_id" : ObjectId("5a37667aee64bce1b14747d2"),
"score" : 74,
"rating" : "Good"
},
{
"_id" : ObjectId("5a3766b3ee64bce1b14747dc"),
"score" : 74,
"rating" : "Good"
}
]
}
We are trying to perform aggregation using this,
db.fbresults.aggregate([{$match:{academicyear:"2017-18",fdept:'Comp'}},{$group:{_id: {fname: "$fname", rating:"$fbValueList.rating"},count: {"$sum":1}}}])
and we get result like,
{ "_id" : { "fname" : "ABC", "rating" : [ "Fair","Fair","Excellent","Very Good", "Very Good", "Excellent", "Good", "Good" ] }, "count" : 2 }
but we are expecting result like,
{ "_id" : { "fname" : "ABC", "rating_group" : [
{
rating: "Excellent"
count: 2
},
{
rating: "Very Good"
count: 2
},
{
rating: "Good"
count: 2
},
{
rating: "Fair"
count: 2
},
] }, "count" : 2 }
We want to get individual faculty group by their name and inside that group by their rating response and count of rating.
We have already tried this one but we did not the result.
Mongodb Aggregate Nested Group
This should get you going:
db.collection.aggregate([{
$match: {
academicyear: "2017-18",
fdept:'Comp'
}
}, {
$unwind: "$fbValueList" // flatten the fbValueList array into multiple documents
}, {
$group: {
_id: {
fname: "$fname",
rating:"$fbValueList.rating"
},
count: {
"$sum": 1 // this will give us the count per combination of fname and fbValueList.rating
}
}
}, {
$group: {
_id: "$_id.fname", // we only want one bucket per fname
rating_group: {
$push: { // we push the exact structure you were asking for
rating: "$_id.rating",
count: "$count"
}
},
count: {
$avg: "$count" // this will be the average across all entries in the fname bucket
}
}
}])
This is a long aggregation pipeline, there may be some aggregations that are un-necessary, so please check and discard whichever are irrelevant.
NOTE: This will only work with Mongo 3.4+.
You need to use $unwind and then $group and $push ratings with their counts.
matchAcademicYear = {
$match: {
academicyear:"2017-18", fdept:'Comp'
}
}
groupByNameAndRating = {
$group: {
_id: {
fname: "$fname", rating:"$fbValueList.rating"
},
count: {
"$sum":1
}
}
}
unwindRating = {
$unwind: "$_id.rating"
}
addFullRating = {
$addFields: {
"_id.full_rating": "$count"
}
}
replaceIdRoot = {
$replaceRoot: {
newRoot: "$_id"
}
}
groupByRatingAndFname = {
$group: {
_id: {
"rating": "$rating",
"fname": "$fname"
},
count: {"$sum": 1},
full_rating: {"$first": "$full_rating"}
}
}
addFullRatingAndCount = {
$addFields: {
"_id.count": "$count",
"_id.full_rating": "$full_count"
}
}
groupByFname = {
$group: {
_id: "$fname",
rating_group: { $push: {rating: "$rating", count: "$count"}},
count: { $first: "$full_rating"}
}
}
db.fbresults.aggregate([
matchAcademicYear,
groupByNameAndRating,
unwindRating,
addFullRating,
unwindRating,
replaceIdRoot,
groupByRatingAndFname,
addFullRatingAndCount,
replaceIdRoot,
groupByFname
])

Resources