0

I'm a beginner at MongoDB.

I want to find documents in MongoDB by favorite.foods.

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

I tried to

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

but couldn't find id 1...

Please help me.

Yong Shun
  • 35,286
  • 4
  • 24
  • 46
bob
  • 3
  • 4

1 Answers1

1

With dot notation.

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

Sample Mongo Playground

Yong Shun
  • 35,286
  • 4
  • 24
  • 46
  • 1
    do I have to use dot? – bob Nov 08 '21 at 04:12
  • 1
    @bob This answer works because it tries to match on 2 different fields inside the `favorite.foods` object. Your initial attempt is close, but you are trying to match the whole `favorite.food` object so you cannot fetch the expected document. Thanks YongShun for this clean and concise answer. – ray Nov 08 '21 at 04:12
  • And thanks to @ray for the explanation in detail. =) – Yong Shun Nov 08 '21 at 04:13
  • Hi @bob, you may have a look at the link I shared for the sample query for the embedded documents. – Yong Shun Nov 08 '21 at 04:15
  • @YongShun @ray thanks for your answers. but, it doesn't work.. I tried to ```db.collectionName.find({"favorite.foods.fruits":"banana","favorite.foods.fastfood":{"$all":["burger", "sandwich"]}})``` – bob Nov 08 '21 at 04:25
  • 1
    oh, sorry! I was wrong spell. thank :) – bob Nov 08 '21 at 04:42
  • @YongShun Can I ask one more question? I want to get only id 1 document even document have more field. I mean if when id 4 document have ```{"favorite.foods.fruits":"banana", "favorite.foods.fastfood":["burger", "sandwich"], "favorite.foods.soup":"cream soup"}```, I want to get only id 1 document still – bob Nov 09 '21 at 04:15
  • Hi, you may use [`.findOne()`](https://docs.mongodb.com/manual/reference/method/db.collection.findOne/). Or switch to `.aggregate()` with [`$limit`](https://docs.mongodb.com/manual/reference/operator/aggregation/limit/): 1 – Yong Shun Nov 09 '21 at 04:42
  • @YongShun oh, Sorry, it was my fault. I mean, I want to get documents which matching perfectly. if id 5 document has same field with id 1(```"favorite.foods.fastfood"``` array not same order), I want to get id 1 and 5 but not id 4. – bob Nov 09 '21 at 05:04
  • Hmmm, I think you should create a new question and provide the sample documents, requirements to meet your desired data. It's hard to follow up and provide the answer that you need with the comment section. – Yong Shun Nov 09 '21 at 05:21
  • you're right. thanks for your kindness :) – bob Nov 09 '21 at 05:25