How can I find documents which matching field and data perfectly(not same data order)

209 Views Asked by At

I'm begginer at mongoDB

I want to get documents which matching perfectly in mongoDB. id 1 and id 5 document have same fields(but "favorite.foods.fastfood" array not same order).

I mean, I want to get id 1 and 5 but not id 4.

here are documents

[
  {
    "_id": 1,
    "favorite": {
      "color": "red",
      "foods": {
        "fruits": "banana",
        "fastfood": [
          "burger",
          "sandwich"
        ]
      }
    }
  },
  {
    "_id": 2,
    "favorite": {
      "color": "green",
      "foods": {
        "noodles": "ramen",
        "fastfood": [
          "fries",
          "burger",
          "corn dog"
        ]
      }
    }
  },
  {
    "_id": 3,
    "favorite": {
      "color": "red",
      "foods": {
        "soup": "cream soup"
      }
    }
  },
  {
    "_id": 4,
    "favorite": {
      "color": "red",
      "foods": {
        "fruits": "banana",
        "fastfood": [
          "sandwich",
          "burger"
        ],
        "soup": "cream soup"
      }
    }
  },
  {
    "_id": 5,
    "favorite": {
      "color": "red",
      "foods": {
        "fruits": "banana",
        "fastfood": [
          "sandwich",
          "burger"
        ],
      }
    }
  }
]

I tried to this query, but failed.

Please help me.

db.collection.find({
  "favorite.foods.fruits": "banana",
  "favorite.foods.fastfood": {
    "$all": [
      "sandwich",
      "burger"
    ]
  }
})
0

There are 0 best solutions below