簡體   English   中英

嵌入文檔中的更新(嵌套集合)

[英]Update in embedded document(Nested collection)

我有一個類似User的集合,其中User具有列表用戶,而該用戶具有user列表。 就像等級制度。

   {        
     "_id" : ObjectId("55530326bc687d21783fd1ff"),
            "Name" : "User 1",
            "Role" : "Manager",
        number:NumberLong(0),
            "1" : 
        [
        {
        "_id" : ObjectId("55530326bc687d21783fd1fd"),
            "Name" : "User 2",
            "Role" : "Ass Manager",
        number:NumberLong(0),
            "1" : 
        [
        .......
        ]
        }
        {
        "_id" : ObjectId("55530326bc687d21783fd1fq"),
            "Name" : "User 2",
            "Role" : "Ass Manager",
    number:NumberLong(1),
            "1" : 
        [
        .........
        ]
        },
        {
        "_id" : ObjectId("55530326bc687d21783fd1fg"),
            "Name" : "User 3",
            "Role" : "Ass Manager",
    number:NumberLong(2),
            "1" : 
        [
        ........
        ]
        }
        ],
         "2" : 
        [
        {
        "_id" : ObjectId("55530326bc687d21783fd1fw"),
            "Name" : "User 4",
            "Role" : "Specialist",
        number:NumberLong(0),
            "1" : 
        [
        .......
        ]
        }
        {
        "_id" : ObjectId("55530326bc687d21783fd1fe"),
            "Name" : "User 5",
            "Role" : "Specialist",
    number:NumberLong(1),
            "1" : 
        [
        .........
        ]
        },
        {
        "_id" : ObjectId("55530326bc687d21783fd1fr"),
            "Name" : "User 6",
            "Role" : "Specialist",
    number:NumberLong(2),
            "1" : 
        [
        ........
        ]
        }
        ]
        }

上面只是樣本的一個集合,像這樣,我有近10000個文檔。 我需要找到“數字”為0的集合。即使任何一個嵌入式文檔的“數字”都為0,我也想要該文檔。

注意:我無法告訴用戶會有多少孩子。

好吧,我將假設您的每個User文檔可以具有兩個子用戶數組(即“ 1”和“ 2”),並且您具有最大嵌套級別,例如3(這意味着嵌套用戶不能具有超過2位主持人)。 順便說一句, mongodb允許的最大嵌套級別是100

可能這不是您想要的:在這種情況下,您的架構設計有問題,因為

現在,讓我們假裝我的假設對您來說還可以。 嘗試(由於我們通常不使用大寫的集合名稱,因此我打電話給您的集合users ):

db.users.find({$or:["1.number" : 0, "2.number" : 0, "1.1.number":0, "1.2.number":0, ..., "2.2.1.number":0, "2.2.2.number":0]})

我跳過了一些您需要添加的組合。 請注意,您不必擔心數組中的位置,並且只有3個嵌套級別,對於$or運算符來說已經有很多子句,這可能會使您確信遵循鏈接的最佳實踐會更好。 。

面向未來讀者的注意事項:OP已在注釋中澄清說他實際上不需要update ,但需要find查詢。

暫無
暫無

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

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