定義
$whereUse the
$whereoperator to pass either a string containing a JavaScript expression or a full JavaScript function to the query system. The$whereprovides greater flexibility, but requires that the database processes the JavaScript expression or function for each document in the collection. Reference the document in the JavaScript expression or function using eitherthisorobj.
互換性
次の環境でホストされる配置には $where を使用できます。
- MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud
MongoDB Enterprise: サブスクリプションベースの自己管理型 MongoDB バージョン
MongoDB Community: ソースが利用可能で、無料で使用できる自己管理型の MongoDB のバージョン
構文
The $where operator has the following form:
{ $where: <string|JavaScript Code> }
注意
$where no longer supports the deprecated BSON type JavaScript code with scope (BSON Type 15). The $where operator only supports BSON type String (BSON Type 2) or BSON type JavaScript (BSON Type 13). The use of BSON type JavaScript with scope for $where has been deprecated since MongoDB 4.2.1.
注意
優先される集計の代替手段
$expr 演算子を使用すると、クエリ言語内で集計式を使用できます。$function と $accumulator を使用すると、提供されているパイプライン演算子がアプリケーションのニーズを満たせない場合に、ユーザーは JavaScript でカスタム集計式を定義できます。
使用可能な集計演算子を指定すると、次のようになります。
The use of
$exprwith aggregation operators that do not use JavaScript (i.e. non-$functionand non-$accumulatoroperators) is faster than$wherebecause it does not execute JavaScript and should be preferred if possible.
動作
利用可能な JavaScript プロパティと関数
map-reduce operations and $where operator expressions cannot access certain global functions or properties, such as db, that are available in mongosh.
The following JavaScript functions and properties are available to map-reduce operations and $where operator expressions:
利用可能なプロパティ | 利用可能な機能 | |
|---|---|---|
|
|
|
elemMatch
Only apply the $where query operator to top-level documents. The $where query operator will not work inside a nested document, for instance, in an $elemMatch query.
Considerations
グローバル変数は使用しないでください。
Using normal non-$where query statements provides the following performance advantages:
JavaScript Enablement
To use $where (or $function, $accumulator, or mapReduce), you must have server-side scripting enabled (default).
ただし、これらの操作を使用しない場合は、サーバー側スクリプトを無効にします。
mongodインスタンスについては、security.javascriptEnabled構成オプションまたは--noscriptingコマンドライン オプションを参照してください。mongosインスタンスについては、security.javascriptEnabled構成オプションまたは--noscriptingコマンドライン オプションを参照してください。
➤ 安全な構成オプションを使用して MongoDB を実行するも参照してください。
サポートされていない配列関数と文字列関数
MongoDB 6.0 upgrades the internal JavaScript engine used for server-side JavaScript, $accumulator, $function, and $where expressions and from MozJS-60 to MozJS-91. Several deprecated, non-standard array and string functions that existed in MozJS-60 are removed in MozJS-91.
例
playersコレクションに含まれる以下の文書を考えます。
db.players.insertMany([ { _id: 12378, name: "Steve", username: "steveisawesome", first_login: "2017-01-01" }, { _id: 2, name: "Anya", username: "anya", first_login: "2001-02-02" } ])
The following example uses $where and the hex_md5() JavaScript function to compare the value of the name field to an MD5 hash and returns any matching document.
db.players.find( { $where: function() { return (hex_md5(this.name) == "9b53e667f30cd329dca1ec9e6a83e994") } } );
この操作では、次の結果を返します。
{ "_id" : 2, "name" : "Anya", "username" : "anya", "first_login" : "2001-02-02" }
あるいは、前の例は$exprと$functionを使用して書き換えることもできます。 集計演算子$functionを使用して、JavaScript でカスタム集計式を定義できます。 $functionの およびその他の集計演算子にアクセスするには、db.collection.find() $exprとともに使用します。
db.players.find( {$expr: { $function: { body: function(name) { return hex_md5(name) == "9b53e667f30cd329dca1ec9e6a83e994"; }, args: [ "$name" ], lang: "js" } } } )
If you must create custom expressions, $function is preferred over $where.