Hướng dẫn create function in mongodb - tạo hàm trong mongodb

Tài liệu về nhà → Hướng dẫn sử dụng MongoDBMongoDB Manual

$function
Hướng dẫn create function in mongodb - tạo hàm trong mongodb

Mới trong phiên bản 4.4.

Xác định chức năng hoặc biểu thức tập hợp tùy chỉnh trong JavaScript.

Bạn có thể sử dụng toán tử $function để xác định các chức năng tùy chỉnh để thực hiện hành vi không được hỗ trợ bởi ngôn ngữ truy vấn MongoDB. Xem thêm $accumulator$function operator to define custom functions to implement behavior not supported by the MongoDB Query Language. See also $accumulator

Quan trọng

Thực hiện JavaScript bên trong một biểu thức tổng hợp có thể làm giảm hiệu suất. Chỉ sử dụng toán tử $function nếu các nhà khai thác đường ống được cung cấp không thể đáp ứng nhu cầu của ứng dụng của bạn.$function operator if the provided pipeline operators cannot fulfill your application's needs.

Toán tử $function có cú pháp sau:$function operator has the following syntax:

{
$function: {
body: ,
args: <array expression>,
lang: "js"
}
}

Đồng ruộng

Loại hình

Sự mô tả

thân hình

Chuỗi hoặc mã

Định nghĩa chức năng. Bạn có thể chỉ định định nghĩa chức năng là mã loại BSON hoặc chuỗi. Xem thêm Lang.lang.

db.players.insertMany([
{ _id: 1, name: "Miss Cheevous", scores: [ 10, 5, 10 ] },
{ _id: 2, name: "Miss Ann Thrope", scores: [ 10, 10, 10 ] },
{ _id: 3, name: "Mrs. Eppie Delta ", scores: [ 9, 8, 8 ] }
])
2

hoặc

db.players.insertMany([
{ _id: 1, name: "Miss Cheevous", scores: [ 10, 5, 10 ] },
{ _id: 2, name: "Miss Ann Thrope", scores: [ 10, 10, 10 ] },
{ _id: 3, name: "Mrs. Eppie Delta ", scores: [ 9, 8, 8 ] }
])
3

Args

Mảng

Đối số truyền cho cơ thể chức năng. Nếu chức năng cơ thể không có đối số, bạn có thể chỉ định một mảng trống

db.players.insertMany([
{ _id: 1, name: "Miss Cheevous", scores: [ 10, 5, 10 ] },
{ _id: 2, name: "Miss Ann Thrope", scores: [ 10, 10, 10 ] },
{ _id: 3, name: "Mrs. Eppie Delta ", scores: [ 9, 8, 8 ] }
])
4.body. If the body function does not take an argument, you can specify an empty array
db.players.insertMany([
{ _id: 1, name: "Miss Cheevous", scores: [ 10, 5, 10 ] },
{ _id: 2, name: "Miss Ann Thrope", scores: [ 10, 10, 10 ] },
{ _id: 3, name: "Mrs. Eppie Delta ", scores: [ 9, 8, 8 ] }
])
4.

Các phần tử mảng có thể là bất kỳ loại BSON nào, bao gồm cả mã. Xem Ví dụ 2: Thay thế cho

db.players.insertMany([
{ _id: 1, name: "Miss Cheevous", scores: [ 10, 5, 10 ] },
{ _id: 2, name: "Miss Ann Thrope", scores: [ 10, 10, 10 ] },
{ _id: 3, name: "Mrs. Eppie Delta ", scores: [ 9, 8, 8 ] }
])
5Example 2: Alternative to
db.players.insertMany([
{ _id: 1, name: "Miss Cheevous", scores: [ 10, 5, 10 ] },
{ _id: 2, name: "Miss Ann Thrope", scores: [ 10, 10, 10 ] },
{ _id: 3, name: "Mrs. Eppie Delta ", scores: [ 9, 8, 8 ] }
])
5

Lang

Sợi dây

Ngôn ngữ được sử dụng trong cơ thể. Bạn phải chỉ định

db.players.insertMany([
{ _id: 1, name: "Miss Cheevous", scores: [ 10, 5, 10 ] },
{ _id: 2, name: "Miss Ann Thrope", scores: [ 10, 10, 10 ] },
{ _id: 3, name: "Mrs. Eppie Delta ", scores: [ 9, 8, 8 ] }
])
6.body. You must specify
db.players.insertMany([
{ _id: 1, name: "Miss Cheevous", scores: [ 10, 5, 10 ] },
{ _id: 2, name: "Miss Ann Thrope", scores: [ 10, 10, 10 ] },
{ _id: 3, name: "Mrs. Eppie Delta ", scores: [ 9, 8, 8 ] }
])
6.

Bạn không thể sử dụng $function như một phần của biểu thức truy vấn xác thực lược đồ.$function as part of schema validation query expression.

Để sử dụng $function, bạn phải bật kịch bản phía máy chủ (mặc định).$function, you must have server-side scripting enabled (default).

Nếu bạn không sử dụng $function (hoặc $accumulator,

db.players.insertMany([
{ _id: 1, name: "Miss Cheevous", scores: [ 10, 5, 10 ] },
{ _id: 2, name: "Miss Ann Thrope", scores: [ 10, 10, 10 ] },
{ _id: 3, name: "Mrs. Eppie Delta ", scores: [ 9, 8, 8 ] }
])
5 hoặc
db.players.aggregate( [
{ $addFields:
{
isFound:
{ $function:
{
body: function(name) {
return hex_md5(name) == "15b0a220baa16331e8d80e15367677ad"
},
args: [ "$name" ],
lang: "js"
}
},
message:
{ $function:
{
body: function(name, scores) {
let total = Array.sum(scores);
return `Hello ${name}. Your total score is ${total}.`
},
args: [ "$name", "$scores"],
lang: "js"
}
}
}
}
] )
2), vô hiệu hóa kịch bản phía máy chủ:$function (or $accumulator,
db.players.insertMany([
{ _id: 1, name: "Miss Cheevous", scores: [ 10, 5, 10 ] },
{ _id: 2, name: "Miss Ann Thrope", scores: [ 10, 10, 10 ] },
{ _id: 3, name: "Mrs. Eppie Delta ", scores: [ 9, 8, 8 ] }
])
5, or
db.players.aggregate( [
{ $addFields:
{
isFound:
{ $function:
{
body: function(name) {
return hex_md5(name) == "15b0a220baa16331e8d80e15367677ad"
},
args: [ "$name" ],
lang: "js"
}
},
message:
{ $function:
{
body: function(name, scores) {
let total = Array.sum(scores);
return `Hello ${name}. Your total score is ${total}.`
},
args: [ "$name", "$scores"],
lang: "js"
}
}
}
}
] )
2), disable server-side scripting:

  • Để biết ví dụ

    db.players.aggregate( [
    { $addFields:
    {
    isFound:
    { $function:
    {
    body: function(name) {
    return hex_md5(name) == "15b0a220baa16331e8d80e15367677ad"
    },
    args: [ "$name" ],
    lang: "js"
    }
    },
    message:
    { $function:
    {
    body: function(name, scores) {
    let total = Array.sum(scores);
    return `Hello ${name}. Your total score is ${total}.`
    },
    args: [ "$name", "$scores"],
    lang: "js"
    }
    }
    }
    }
    ] )
    3, xem tùy chọn cấu hình
    db.players.aggregate( [
    { $addFields:
    {
    isFound:
    { $function:
    {
    body: function(name) {
    return hex_md5(name) == "15b0a220baa16331e8d80e15367677ad"
    },
    args: [ "$name" ],
    lang: "js"
    }
    },
    message:
    { $function:
    {
    body: function(name, scores) {
    let total = Array.sum(scores);
    return `Hello ${name}. Your total score is ${total}.`
    },
    args: [ "$name", "$scores"],
    lang: "js"
    }
    }
    }
    }
    ] )
    4 hoặc tùy chọn dòng lệnh
    db.players.aggregate( [
    { $addFields:
    {
    isFound:
    { $function:
    {
    body: function(name) {
    return hex_md5(name) == "15b0a220baa16331e8d80e15367677ad"
    },
    args: [ "$name" ],
    lang: "js"
    }
    },
    message:
    { $function:
    {
    body: function(name, scores) {
    let total = Array.sum(scores);
    return `Hello ${name}. Your total score is ${total}.`
    },
    args: [ "$name", "$scores"],
    lang: "js"
    }
    }
    }
    }
    ] )
    5.

  • Đối với một ví dụ

    db.players.aggregate( [
    { $addFields:
    {
    isFound:
    { $function:
    {
    body: function(name) {
    return hex_md5(name) == "15b0a220baa16331e8d80e15367677ad"
    },
    args: [ "$name" ],
    lang: "js"
    }
    },
    message:
    { $function:
    {
    body: function(name, scores) {
    let total = Array.sum(scores);
    return `Hello ${name}. Your total score is ${total}.`
    },
    args: [ "$name", "$scores"],
    lang: "js"
    }
    }
    }
    }
    ] )
    6, xem tùy chọn cấu hình
    db.players.aggregate( [
    { $addFields:
    {
    isFound:
    { $function:
    {
    body: function(name) {
    return hex_md5(name) == "15b0a220baa16331e8d80e15367677ad"
    },
    args: [ "$name" ],
    lang: "js"
    }
    },
    message:
    { $function:
    {
    body: function(name, scores) {
    let total = Array.sum(scores);
    return `Hello ${name}. Your total score is ${total}.`
    },
    args: [ "$name", "$scores"],
    lang: "js"
    }
    }
    }
    }
    ] )
    4 hoặc tùy chọn dòng lệnh
    db.players.aggregate( [
    { $addFields:
    {
    isFound:
    { $function:
    {
    body: function(name) {
    return hex_md5(name) == "15b0a220baa16331e8d80e15367677ad"
    },
    args: [ "$name" ],
    lang: "js"
    }
    },
    message:
    { $function:
    {
    body: function(name, scores) {
    let total = Array.sum(scores);
    return `Hello ${name}. Your total score is ${total}.`
    },
    args: [ "$name", "$scores"],
    lang: "js"
    }
    }
    }
    }
    ] )
    5 bắt đầu từ MongoDB 4.4.

    Trong các phiên bản trước, MongoDB không cho phép thực thi JavaScript trên các trường hợp

    db.players.aggregate( [
    { $addFields:
    {
    isFound:
    { $function:
    {
    body: function(name) {
    return hex_md5(name) == "15b0a220baa16331e8d80e15367677ad"
    },
    args: [ "$name" ],
    lang: "js"
    }
    },
    message:
    { $function:
    {
    body: function(name, scores) {
    let total = Array.sum(scores);
    return `Hello ${name}. Your total score is ${total}.`
    },
    args: [ "$name", "$scores"],
    lang: "js"
    }
    }
    }
    }
    ] )
    6.

Xem thêm ➤ Chạy MongoDB với các tùy chọn cấu hình an toàn.

Toán tử truy vấn

db.players.insertMany([
{ _id: 1, name: "Miss Cheevous", scores: [ 10, 5, 10 ] },
{ _id: 2, name: "Miss Ann Thrope", scores: [ 10, 10, 10 ] },
{ _id: 3, name: "Mrs. Eppie Delta ", scores: [ 9, 8, 8 ] }
])
5 cũng có thể được sử dụng để chỉ định biểu thức JavaScript. Tuy nhiên:

  • Toán tử

    { "_id" : 1, "name" : "Miss Cheevous", "scores" : [ 10, 5, 10 ], "isFound" : false, "message" : "Hello Miss Cheevous. Your total score is 25." }
    { "_id" : 2, "name" : "Miss Ann Thrope", "scores" : [ 10, 10, 10 ], "isFound" : true, "message" : "Hello Miss Ann Thrope. Your total score is 30." }
    { "_id" : 3, "name" : "Mrs. Eppie Delta ", "scores" : [ 9, 8, 8 ], "isFound" : false, "message" : "Hello Mrs. Eppie Delta . Your total score is 25." }
    1 cho phép sử dụng các biểu thức tổng hợp trong ngôn ngữ truy vấn.

  • Bắt đầu từ MongoDB 4.4, $function$accumulator cho phép người dùng xác định các biểu thức tập hợp tùy chỉnh trong JavaScript nếu các nhà khai thác đường ống được cung cấp không thể đáp ứng nhu cầu của ứng dụng của bạn.$function and $accumulator allows users to define custom aggregation expressions in JavaScript if the provided pipeline operators cannot fulfill your application's needs.

Cho các nhà khai thác tổng hợp có sẵn:

  • Việc sử dụng

    { "_id" : 1, "name" : "Miss Cheevous", "scores" : [ 10, 5, 10 ], "isFound" : false, "message" : "Hello Miss Cheevous. Your total score is 25." }
    { "_id" : 2, "name" : "Miss Ann Thrope", "scores" : [ 10, 10, 10 ], "isFound" : true, "message" : "Hello Miss Ann Thrope. Your total score is 30." }
    { "_id" : 3, "name" : "Mrs. Eppie Delta ", "scores" : [ 9, 8, 8 ], "isFound" : false, "message" : "Hello Mrs. Eppie Delta . Your total score is 25." }
    1 với các toán tử tổng hợp không sử dụng JavaScript (tức là không phải là người vận hành không phải là không-____ và không.$function and non-$accumulator operators) is faster than
    db.players.insertMany([
    { _id: 1, name: "Miss Cheevous", scores: [ 10, 5, 10 ] },
    { _id: 2, name: "Miss Ann Thrope", scores: [ 10, 10, 10 ] },
    { _id: 3, name: "Mrs. Eppie Delta ", scores: [ 9, 8, 8 ] }
    ])
    5 because it does not execute JavaScript and should be preferred if possible.

  • Tuy nhiên, nếu bạn phải tạo các biểu thức tùy chỉnh, $function được ưu tiên hơn

    db.players.insertMany([
    { _id: 1, name: "Miss Cheevous", scores: [ 10, 5, 10 ] },
    { _id: 2, name: "Miss Ann Thrope", scores: [ 10, 10, 10 ] },
    { _id: 3, name: "Mrs. Eppie Delta ", scores: [ 9, 8, 8 ] }
    ])
    5$function is preferred over
    db.players.insertMany([
    { _id: 1, name: "Miss Cheevous", scores: [ 10, 5, 10 ] },
    { _id: 2, name: "Miss Ann Thrope", scores: [ 10, 10, 10 ] },
    { _id: 3, name: "Mrs. Eppie Delta ", scores: [ 9, 8, 8 ] }
    ])
    5

MongoDB 6.0 nâng cấp Công cụ JavaScript nội bộ được sử dụng cho các biểu thức JavaScript phía máy chủ, $accumulator, $function

db.players.insertMany([
{ _id: 1, name: "Miss Cheevous", scores: [ 10, 5, 10 ] },
{ _id: 2, name: "Miss Ann Thrope", scores: [ 10, 10, 10 ] },
{ _id: 3, name: "Mrs. Eppie Delta ", scores: [ 9, 8, 8 ] }
])
5 và từ Mozjs-60 đến Mozjs-91. Một số hàm mảng và chuỗi không tiêu chuẩn, không có tiêu chuẩn tồn tại trong mozjs-60 được loại bỏ trong mozjs-91.$function, and
db.players.insertMany([
{ _id: 1, name: "Miss Cheevous", scores: [ 10, 5, 10 ] },
{ _id: 2, name: "Miss Ann Thrope", scores: [ 10, 10, 10 ] },
{ _id: 3, name: "Mrs. Eppie Delta ", scores: [ 9, 8, 8 ] }
])
5 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.

Để biết danh sách đầy đủ các chức năng mảng và chuỗi bị xóa, hãy xem Ghi chú tương thích 6.0.

Tạo một bộ sưu tập mẫu có tên

db.players.find( { $where: function() {
return (hex_md5(this.name) == "15b0a220baa16331e8d80e15367677ad")
} } );
3 với các tài liệu sau:

db.players.insertMany([
{ _id: 1, name: "Miss Cheevous", scores: [ 10, 5, 10 ] },
{ _id: 2, name: "Miss Ann Thrope", scores: [ 10, 10, 10 ] },
{ _id: 3, name: "Mrs. Eppie Delta ", scores: [ 9, 8, 8 ] }
])

Hoạt động tổng hợp sau sử dụng

db.players.find( { $where: function() {
return (hex_md5(this.name) == "15b0a220baa16331e8d80e15367677ad")
} } );
4 để thêm các trường mới vào mỗi tài liệu:

  • db.players.find( { $where: function() {
    return (hex_md5(this.name) == "15b0a220baa16331e8d80e15367677ad")
    } } );
    5 có giá trị được xác định bởi biểu thức tùy chỉnh $function kiểm tra xem băm MD5 của tên có bằng một hàm băm được chỉ định hay không.$function expression that checks whether the MD5 hash of the name is equal to a specified hash.

  • db.players.find( { $where: function() {
    return (hex_md5(this.name) == "15b0a220baa16331e8d80e15367677ad")
    } } );
    7 có giá trị được xác định bởi biểu thức $function tùy chỉnh định dạng một thông báo chuỗi bằng cách sử dụng một mẫu.$function expression that format a string message using a template.

db.players.aggregate( [
{ $addFields:
{
isFound:
{ $function:
{
body: function(name) {
return hex_md5(name) == "15b0a220baa16331e8d80e15367677ad"
},
args: [ "$name" ],
lang: "js"
}
},
message:
{ $function:
{
body: function(name, scores) {
let total = Array.sum(scores);
return `Hello ${name}. Your total score is ${total}.`
},
args: [ "$name", "$scores"],
lang: "js"
}
}
}
}
] )

Hoạt động trả về các tài liệu sau:

{ "_id" : 1, "name" : "Miss Cheevous", "scores" : [ 10, 5, 10 ], "isFound" : false, "message" : "Hello Miss Cheevous. Your total score is 25." }
{ "_id" : 2, "name" : "Miss Ann Thrope", "scores" : [ 10, 10, 10 ], "isFound" : true, "message" : "Hello Miss Ann Thrope. Your total score is 30." }
{ "_id" : 3, "name" : "Mrs. Eppie Delta ", "scores" : [ 9, 8, 8 ], "isFound" : false, "message" : "Hello Mrs. Eppie Delta . Your total score is 25." }

Ghi chú

Các lựa chọn thay thế tổng hợp ưa thích hơn $ ở đâu

Toán tử

{ "_id" : 1, "name" : "Miss Cheevous", "scores" : [ 10, 5, 10 ], "isFound" : false, "message" : "Hello Miss Cheevous. Your total score is 25." }
{ "_id" : 2, "name" : "Miss Ann Thrope", "scores" : [ 10, 10, 10 ], "isFound" : true, "message" : "Hello Miss Ann Thrope. Your total score is 30." }
{ "_id" : 3, "name" : "Mrs. Eppie Delta ", "scores" : [ 9, 8, 8 ], "isFound" : false, "message" : "Hello Mrs. Eppie Delta . Your total score is 25." }
1 cho phép sử dụng các biểu thức tổng hợp trong ngôn ngữ truy vấn. Và, bắt đầu trong MongoDB 4.4, $function$accumulator cho phép người dùng xác định các biểu thức tập hợp tùy chỉnh trong JavaScript nếu các nhà khai thác đường ống được cung cấp không thể đáp ứng nhu cầu của ứng dụng của bạn.$function and $accumulator allows users to define custom aggregation expressions in JavaScript if the provided pipeline operators cannot fulfill your application's needs.

Cho các nhà khai thác tổng hợp có sẵn:

  • Việc sử dụng

    { "_id" : 1, "name" : "Miss Cheevous", "scores" : [ 10, 5, 10 ], "isFound" : false, "message" : "Hello Miss Cheevous. Your total score is 25." }
    { "_id" : 2, "name" : "Miss Ann Thrope", "scores" : [ 10, 10, 10 ], "isFound" : true, "message" : "Hello Miss Ann Thrope. Your total score is 30." }
    { "_id" : 3, "name" : "Mrs. Eppie Delta ", "scores" : [ 9, 8, 8 ], "isFound" : false, "message" : "Hello Mrs. Eppie Delta . Your total score is 25." }
    1 với các toán tử tổng hợp không sử dụng JavaScript (tức là không phải là người vận hành không phải là không-____ và không.$function and non-$accumulator operators) is faster than
    db.players.insertMany([
    { _id: 1, name: "Miss Cheevous", scores: [ 10, 5, 10 ] },
    { _id: 2, name: "Miss Ann Thrope", scores: [ 10, 10, 10 ] },
    { _id: 3, name: "Mrs. Eppie Delta ", scores: [ 9, 8, 8 ] }
    ])
    5 because it does not execute JavaScript and should be preferred if possible.

  • Tuy nhiên, nếu bạn phải tạo các biểu thức tùy chỉnh, $function được ưu tiên hơn

    db.players.insertMany([
    { _id: 1, name: "Miss Cheevous", scores: [ 10, 5, 10 ] },
    { _id: 2, name: "Miss Ann Thrope", scores: [ 10, 10, 10 ] },
    { _id: 3, name: "Mrs. Eppie Delta ", scores: [ 9, 8, 8 ] }
    ])
    5$function is preferred over
    db.players.insertMany([
    { _id: 1, name: "Miss Cheevous", scores: [ 10, 5, 10 ] },
    { _id: 2, name: "Miss Ann Thrope", scores: [ 10, 10, 10 ] },
    { _id: 3, name: "Mrs. Eppie Delta ", scores: [ 9, 8, 8 ] }
    ])
    5

MongoDB 6.0 nâng cấp Công cụ JavaScript nội bộ được sử dụng cho các biểu thức JavaScript phía máy chủ, $accumulator, $function

db.players.insertMany([
{ _id: 1, name: "Miss Cheevous", scores: [ 10, 5, 10 ] },
{ _id: 2, name: "Miss Ann Thrope", scores: [ 10, 10, 10 ] },
{ _id: 3, name: "Mrs. Eppie Delta ", scores: [ 9, 8, 8 ] }
])
5 và từ Mozjs-60 đến Mozjs-91. Một số hàm mảng và chuỗi không tiêu chuẩn, không có tiêu chuẩn tồn tại trong mozjs-60 được loại bỏ trong mozjs-91.$function. For example, consider the following
db.players.insertMany([
{ _id: 1, name: "Miss Cheevous", scores: [ 10, 5, 10 ] },
{ _id: 2, name: "Miss Ann Thrope", scores: [ 10, 10, 10 ] },
{ _id: 3, name: "Mrs. Eppie Delta ", scores: [ 9, 8, 8 ] }
])
5 example.

db.players.find( { $where: function() {
return (hex_md5(this.name) == "15b0a220baa16331e8d80e15367677ad")
} } );

Để biết danh sách đầy đủ các chức năng mảng và chuỗi bị xóa, hãy xem Ghi chú tương thích 6.0.

{ "_id" : 2, "name" : "Miss Ann Thrope", "scores" : [ 10, 10, 10 ] }

Tạo một bộ sưu tập mẫu có tên

db.players.find( { $where: function() {
return (hex_md5(this.name) == "15b0a220baa16331e8d80e15367677ad")
} } );
3 với các tài liệu sau:$function

db.players.find( {$expr: { $function: {
body: function(name) { return hex_md5(name) == "15b0a220baa16331e8d80e15367677ad"; },
args: [ "$name" ],
lang: "js"
} } } )