Home > front end >  How to search mongodb
How to search mongodb

Time:05-19

I have two JSONs in a collection in mongodb and would like to write a bson.M filter to fetch the first JSON below.

I tried with the filter below to get the first JSON but got no result. When the first JSON is in the collection, I get result when i use the filter but when i have both JSONs, I do not get a result. Need help.

filter := bson.M{"type": "FPF", "status": "REGISTERED","fpfInfo.fpfInfoList.ssai.st": 1, "fpfInfo.fpfInfoList.infoList.dn": "sim"}

{

    "_id" : "47f6ad68-d431-4b69-9899-f33d828f8f9c",
    "type" : "FPF",
    "status" : "REGISTERED",
    "fpfInfo" : {
        "fpfInfoList" : [
            {
                "ssai" : {
                    "st" : 1
                },
                "infoList" : [
                    {
                        "dn" : "sim"
                    }
                ]
            }
        ]
    }
},

{

    "_id" : "347c8ed2-d9d1-4f1a-9672-7e8a232d2bf8",
    "type" : "FPF",
    "status" : "REGISTERED",
    "fpfInfo" : {
        "fpfInfoList" : [
            {
                "ssai" : {
                    "st" : 1,
                    "ds" : "000004"
                },
                "infoList" : [
                    {
                        "dn" : "sim"
                    }
                ]
            }
        ]
    }
}

CodePudding user response:

db.collection.aggregate([
  {
    "$unwind": "$fpfInfo.fpfInfoList"
  },
  {
    "$match": {
      "fpfInfo.fpfInfoList.ssai.ds": {
        "$exists": false
      },
      "fpfInfo.fpfInfoList.infoList.dn": "sim",
      "fpfInfo.fpfInfoList.ssai.st": 1
    }
  }
])

Playground

  • Related