Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Mongoose aggregate query working as a mongodb query but not able to convert to mongoose

Below is my JSON structure

  [{
        "_id" : ObjectId("626204345ae3d8ec53ef41ee"),
        "categoryName" : "Test Cate",
        "__v" : 0,
        "createdAt" : ISODate("2022-04-22T01:26:11.627Z"),
        "items" : [
                {
                        "itemName" : "Cate",
                        "user" : ObjectId("6260729af547915d9d876c23"),
                        "itemDescription" : "slkkndanslk",
                        "itemImage" : "/images/camping-table.jpeg",
                        "_id" : ObjectId("626204339b24b2ead6c05a70"),
                        "updatedAt" : ISODate("2022-04-22T01:26:11.627Z"),
                        "createdAt" : ISODate("2022-04-22T01:26:11.627Z")
                }
        ],
        "updatedAt" : ISODate("2022-04-22T01:26:11.627Z")
},
{
        "_id" : ObjectId("62620e725ae3d8ec53ef4aa8"),
        "categoryName" : "sdsad",
        "__v" : 0,
        "createdAt" : ISODate("2022-04-22T02:09:54.028Z"),
        "items" : [
                {
                        "itemName" : "asdada",
                        "user" : ObjectId("62620e6299145edb95147482"),
                        "itemDescription" : "asdsadad",
                        "itemImage" : "/images/camping-table.jpeg",
                        "_id" : ObjectId("62620e7299145edb95147486"),
                        "updatedAt" : ISODate("2022-04-22T02:09:54.028Z"),
                        "createdAt" : ISODate("2022-04-22T02:09:54.028Z")
                },
                {
                        "itemName" : "dsdsa",
                        "user" : ObjectId("62620e6299145edb95147482"),
                        "itemDescription" : "adasdad",
                        "itemImage" : "/images/camping-table.jpeg",
                        "_id" : ObjectId("62621b9c3662e0b4acabb71f"),
                        "updatedAt" : ISODate("2022-04-22T03:06:04.727Z"),
                        "createdAt" : ISODate("2022-04-22T03:06:04.727Z")
                }
        ],
        "updatedAt" : ISODate("2022-04-22T03:06:04.727Z")
}]

This is just one document and there would be array of documents. Also there may be multiple items within the same category.
I want to fetch all the items in all category with a particular userid. In MongoDB below is my query which is giving correct output on mongo shell

db.trades.aggregate([
  {
    $unwind: "$items"
  },
  {
    $match: {
      "items.user": ObjectId("6260729af547915d9d876c23")
    }
  }
]).pretty()

In mongoose I am doing the following thing but not getting the result

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

tradeModel.aggregate([ { $unwind : "$items" }, { $match : { "items.user" : id } } ])
    .then(res => {
        console.log(JSON.stringify(res))
    })

Let me know what I am missing

>Solution :

Your parameter id is type string but mongodb store type ObjectId

change

tradeModel.aggregate([ { $unwind : "$items" }, { $match : { "items.user" : id } } ])
    .then(res => {
        console.log(JSON.stringify(res))
    })

into

tradeModel.aggregate([ { $unwind : "$items" }, { $match : { "items.user" : {"$oid": id} } } ])
    .then(res => {
        console.log(JSON.stringify(res))
    })

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading