簡體   English   中英

MongoDB - 如何查找文檔字段包含具有給定名稱的子文檔的位置? (包括示例查詢和文檔)

[英]MongoDB - How to find where a document field contains a subdocument with a given name? (Example query and document included)

在這里,我試圖檢索文檔中包含文檔DummyFoo的文檔,而文檔DummyFoo又包含文檔DummyBar?

據我所知,下面的語法是正確的,但沒有返回結果,但我知道它們存在。 我是否錯誤地構建了查詢?

{ "DummyFoo" : { "$elemMatch" : { "DummyBar": { "$exists" : true } }}}

下面是我希望匹配的文檔示例:

{
  "_id" : ObjectId("5423d66ba077e6205cc9ecef"),
  "Title" : "Test 1234587611",
  "Subtitle" : "Test 1 Subtitle",
  "DummyFoo" : {
    "DummyDate" : "2014-09-25T09:46:35.6626303+01:00",
    "DummyBar" : {
      "Name" : "Test",
      "Number" : 1
    }
  }
}

$ elemMatch專門用於查詢數組。在這種情況下,你有一個嵌套的json,mongodb將其識別為嵌入式文檔

您可以使用點表示法來查詢嵌入的文檔。

 { "DummyFoo.DummyBar": { "$exists" : true }}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM