On Fri, 06 Oct 2017 06:59:40 GMT, Andrew Chapman wrote:

Hi everyone, just wondering how I might do a Mongo OR query such as:

db.events.find({$or: [{"usrId" : 29}, {"metadataJson.workspaceId": "02DD6A2A:12345"}, {"metadataJson.workspaceId": "02DD6A2A:12346"}]})

using vibe.d

What I want to do is something like:

auto queryData = [
    "usrId" : Bson(29),
    $or: [
    	"metadataJson.workspaceId" : Bson("02DD6A2A:12345"), "metadataJson.workspaceId" : Bson("02DD6A2A:12346")
    ]
];

auto result = this.mongoCollection.find(query);

Any thoughts?

The literal translation would be:

db.events.find(["$or": [
    ["usrId" : Bson(29)],
    ["metadataJson.workspaceId": Bson("02DD6A2A:12345")],
    ["metadataJson.workspaceId": Bson("02DD6A2A:12346")]
]]);

Or for the second one:

db.events.find([
    "usrId" : Bson(29),
    "$or": serializeToBson([
        ["metadataJson.workspaceId": "02DD6A2A:12345"],
        ["metadataJson.workspaceId": "02DD6A2A:12346"]
    ])
]);

just so that the value type of the AA literals always matches up. A slightly more efficient alternative would be to use the $in operator:

db.events.find([
    "usrId" : Bson(29),
    "metadataJson.workspaceId": serializeToBson(["$in": [
        "02DD6A2A:12345",
        "02DD6A2A:12346"
    ]])
]);

The query can also be defined as a struct to reduce the amount of GC allocations per query:

struct QW {
    @name("metadataJson.workspaceId")
    string id;
}

struct Q {
    int userId;
    @name("$or")
    QW[] workspaces;
}

db.events.find(Q(29, [QW("02DD6A2A:12345"), QW("02DD6A2A:12346")]));