Home > Mobile >  Mongoose: Get objects by a search query
Mongoose: Get objects by a search query

Time:05-27

I am trying to pull out data of 2 types from my BSON document using mongoose. The 2 data are;

  1. Intention 1 - I want to get objects from the items array where tagNo == 2
  2. Intention 2 -I want to get objects from the items array where tagNo == 2 and tagID == kLawOURVpz1IIjoQ2fhCvy7NM.

For intention 1, I tried;

 db.find(
  { "fileID": "0pdn3jdndndj3msms, "items.tagNo": 2 },
  {
    "items": {
      "$elemMatch": { "tagNo": 2 }
    }
  }
 );

and I tired the query below for intention 2

db.find(
 { "fileID": "0pdn3jdndndj3msms, "items.tagNo": 2 },
 {
   "items": {
      "$elemMatch": { "tagNo": 2, "tagID": "kLawOURVpz1IIjoQ2fhCvy7NM", }
    }
  }
);

I keep getting the entire object back.

My BSON document is;

{
  "_id": "ID_GOES_HERE",
  "fileID": "0pdn3jdndndj3msms",
  "fileName": "Item List",
  "items": [
    {
      "tagNo": 2,
      "status": 0,
      "tagID": "kLawOURVpz1IIjoQ2fhCvy7NM",
      "tagUnit": 5
    },
    {
      "tagNo": 2,
      "status": 0,
      "tagID": "kLawOURVpz1IIjoQ2fhCvy7NM",
      "tagUnit": 8
    },
    {
      "tagNo": 2,
      "status": 0,
      "tagID": "pOawOURVpz1IIjoQ2fhCvy7w3",
      "tagUnit": 81
    },
    {
      "tagNo": 4,
      "status": 0,
      "tagID": "kLawOURVpz1IIjoQ2fhCvy7NM",
      "tagUnit": 904
    },
    {
      "tagNo": 3,
      "status": 0,
      "tagID": "pOawOURVpz1IIjoQ2fhCvy7w3",
      "tagUnit": 24
    },
    {
      "tagNo": 2,
      "status": 0,
      "tagID": "pOawOURVpz1IIjoQ2fhCvy7w3",
      "tagUnit": 35
    },
    {
      "tagNo": 1,
      "status": 0,
      "tagID": "kLawOURVpz1IIjoQ2fhCvy7NM",
      "tagUnit": 11
    },
    {
      "tagNo": 2,
      "status": 0,
      "tagID": "kLawOURVpz1IIjoQ2fhCvy7NM",
      "tagUnit": 30
    }
  ]
}

CodePudding user response:

You need aggregation framework

  db.collection.aggregate([
  {
    $match: { //Match condition to get the matching arrays
      "fileID": "0pdn3jdndndj3msms",
      "items.tagNo": 2
    }
  },
  {
    $project: {
      "items": {
        $filter: { //Project only matching array elements
          input: "$items",
          as: "item",
          cond: {
            $eq: [
              "$$item.tagNo",
              2
            ]
          }
        }
      }
    }
  }
])

Playground

db.collection.aggregate([
  {
    $match: {
      "items": {
        "$elemMatch": {
          "tagNo": 2,
          "tagID": "kLawOURVpz1IIjoQ2fhCvy7NM",
          
        }
      }
    }
  },
  {
    $project: {
      "items": {
        $filter: {
          input: "$items",
          as: "item",
          cond: {
            "$and": [
              {
                $eq: [
                  "$$item.tagNo",
                  2
                ]
              },
              {
                $eq: [
                  "$$item.tagID",
                  "kLawOURVpz1IIjoQ2fhCvy7NM"
                ]
              }
            ]
          }
        }
      }
    }
  }
])

Playground

  • Related