You can only do it with aggregation framework, not with regular find.
db.coll.aggregate({$project:{newField:{$concat:["$field1","$field2"]}}},
{$match:{newField:"value"}}
);
Note that this will not be able to use any indexes, since there is no support for indexes on computed values in MongoDB (yet).
If you have an index on field1
and you know how many characters you expect field1 to contribute to value
you can improve performance of this aggregation like this:
db.coll.aggregate({$match:{field1:/^val/}},
{$project:{newField:{$concat:["$field1","$field2"]}}},
{$match:{newField:"value"}}
);
where val
is first part of "value" string (you must not compare more characters than the shortest possible value of field1
though.
EDIT as of version 3.6 you can do this in find using the $expr
expression:
db.coll.find({$expr:{$eq:["value", {$concat:["$field1", "$field2"]}]}})