简体   繁体   中英

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

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

Let me know what I am missing

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))
    })

Got the answer we can use ObjectId(id) in search like this

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM