FlowerPower1990
FlowerPower1990

Reputation: 67

Find query with logical operator for nested structs?

I want to find entries that contain any of the given arguments using $or. The database entry looks like below

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

I want to find entries that satisfy any of the fields inside resources.

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

Upvotes: 1

Views: 250

Answers (1)

chridam
chridam

Reputation: 103445

You need to construct the equivalent of this mongo shell query using the $or operator:

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

where in go this would be structured as:

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)

Upvotes: 2

Related Questions