2016-05-21 7 views
1

$またはを使用して、指定された引数のいずれかを含むエントリを検索したいとします。データベースのエントリは次のようになりますネストされた構造体の論理演算子でクエリを検索しますか?

"resources" : { 
    "compute" : "compute4", 
    "storage" : "storage3", 
    "network" : "network2" 
}, 

リソース内のいずれかのフィールドを満たすエントリを検索したいと思います。

bkCollection.Find(bson.M{"resources": bson.M{ 
    "compute": filter.Resources.Compute, "$or", 
    "storage": filter.Resources.Storage, "$or", 
    "network": filter.Resources.Network}}).All(&result) 

答えて

2

あなたが$or演算子を使用して、このモンゴシェルクエリと同等のものを構築する必要があります。

bkCollection.Find(bson.M{ "$or": []bson.M{ 
    bson.M{ "resources.compute": filter.Resources.Compute }, 
    bson.M{ "resources.storage": filter.Resources.Storage }, 
    bson.M{ "resources.network": filter.Resources.Network } 
}}).All(&result) 
+1

が脳を持っていた:としてgoで、これは構造化されるだろう

db.collection.find({ "$or": [ { "resources.compute" : "compute5" }, { "resources.storage" : "storage3" }, { "resources.network" : "network1" } ] }) 

おなら。しかし、良いキャッチ。 – khrm