2016-10-11 53 views
0

我有一個集合中蒙戈在子文檔中多個領域蒙戈查詢

{ 
    "_id": 1, 
    "favorites": { 
    "artist": "Picasso", 
    "food": "pizza" 
    }, 
    "finished": [ 
    17, 
    3 
    ], 
    "badges": [ 
    "blue", 
    "black" 
    ], 
    "points": [ 
    { 
     "points": 85, 
     "bo nus": 20 
    }, 
    { 
     "points": 85, 
     "bonus": 10 
    } 
    ] 
}{ 
    "_id": 2, 
    "favorites": { 
    "artist": "Miro", 
    "food": "meringue" 
    }, 
    "finished": [ 
    11, 
    25 
    ], 
    "badges": [ 
    "green" 
    ], 
    "points": [ 
    { 
     "points": 85, 
     "bonus": 20 
    }, 
    { 
     "points": 64, 
     "bonus": 12 
    } 
    ] 
}{ 
    "_id": 3, 
    "favorites": { 
    "artist": "Cassatt", 
    "food": "cake" 
    }, 
    "finished": [ 
    6 
    ], 
    "badges": [ 
    "blue", 
    "red" 
    ], 
    "points": [ 
    { 
     "points": 85, 
     "bonus": 8 
    }, 
    { 
     "points": 55, 
     "bonus": 20 
    } 
    ] 
}{ 
    "_id": 4, 
    "favorites": { 
    "artist": "Chagall", 
    "food": "chocolate" 
    }, 
    "finished": [ 
    5, 
    11 
    ], 
    "badges": [ 
    "red", 
    "black" 
    ], 
    "points": [ 
    { 
     "points": 53, 
     "bonus": 15 
    }, 
    { 
     "points": 51, 
     "bonus": 15 
    } 
    ] 
}{ 
    "_id": 5, 
    "favorites": { 
    "artist": "Noguchi", 
    "food": "nougat" 
    }, 
    "finished": [ 
    14, 
    6 
    ], 
    "badges": [ 
    "orange" 
    ], 
    "points": [ 
    { 
     "points": 71, 
     "bonus": 20 
    } 
    ] 
}{ 
    "_id": 6, 
    "favorites": { 
    "food": "pizza", 
    "artist": "Picasso" 
    }, 
    "finished": [ 
    18, 
    12 
    ], 
    "badges": [ 
    "black", 
    "blue" 
    ], 
    "points": [ 
    { 
     "points": 78, 
     "b onus": 8 
    }, 
    { 
     "points": 57, 
     "bonus": 7 
    } 
    ] 
} 

我想檢索具有所有元素點= 85和獎金= 20。 查詢將是

db.temp2.find({"points":{"points":85,"bonus":20}}) 

它返回文檔ID爲:1和2

現在,如果我想檢索具有元件(分= 85和獎金= 20)且另一子文檔與{積分= 85和獎金> 10)。基本上,我想用ID檢索元素= 2

如果查詢

db.temp2.find({$and:[{"points":{"points":85,"bonus":20}},{"points":{"points":64,"bonus":{$gte:10}}}]}).pretty() 

它沒有給出結果,而查詢

db.temp2.find({$and:[{"points":{"$elemMatch":{"points":85,"bonus":20}}},{"points":{"$elemMatch":{"points":64,"bonus":{$gte:10}}}}]}) 

給我ID = 2。

同樣的事情,我試着用花葯套

[ 
    { 
    "name": "User1", 
    "tags": [ 
     { 
     "k": "group", 
     "v": "test" 
     }, 
     { 
     "k": "color", 
     "v": "blue" 
     } 
    ] 
    }, 
    { 
    "name": "User2", 
    "tags": [ 
     { 
     "k": "group", 
     "v": "dev" 
     }, 
     { 
     "k": "color", 
     "v": "blue" 
     } 
    ] 
    }, 
    { 
    "name": "User3", 
    "tags": [ 
     { 
     "k": "group", 
     "v": "dev" 
     }, 
     { 
     "k": "color", 
     "v": "red" 
     } 
    ] 
    } 
] 

,如果你想找出其元素

"tags": [ 
     { 
     "k": "group", 
     "v": "dev" 
     }, 
     { 
     "k": "color", 
     "v": "blue" 
     } 
    ] 

查詢:

db.temp4.find({$and:[{"tags":{"k":"group","v":"dev"}},{"tags":{"k":"color","v":"blue"}}]}) 

db.temp4.find({$and:[{"tags":{"$elemMatch":{"k":"group","v":"dev"}}},{"tags":{"$elemMatch":{"k":"color","v":"blue"}}}]}) 

在這兩種情況下你都會得到迴應。

請幫我瞭解何時使用 $ elemMatch「$」和「」。

在此先感謝。 對不起語法錯誤。

回答

0

下面的查詢應該可以工作。由於它是嵌入式陣列,因此當使用$gte時,應該像這樣引用「points.bonus」和「points.points」。

db.collection.find({$and:[{"points":{"points":85,"bonus":20}}, {"points.points" : 64, "points.bonus" : {$gte : 10}}]}) 

在第二個例子中,沒有$gte。所以,你得到這兩個查詢的迴應。