Hướng dẫn how do i run a distinct query in mongodb compass? - làm cách nào để chạy một truy vấn riêng biệt trong la bàn mongodb?

Tôi đang sử dụng la bàn MongoDB và không có vỏ Mongo. Tôi cần xây dựng một truy vấn bằng công cụ la bàn MongoDB để chọn các giá trị riêng biệt của trường "thể loại" từ bộ sưu tập của tôi.

Hướng dẫn how do i run a distinct query in mongodb compass? - làm cách nào để chạy một truy vấn riêng biệt trong la bàn mongodb?

Đầu vào mẫu:

{"_id":{"$oid":"58c59c6a99d4ee0af9e0c34e"},"title":"Bateau-mouche sur la Seine","year":{"$numberInt":"1896"},"imdbId":"tt0000042","genre":["Documentary”,”Short”],"viewerRating":{"$numberDouble":"3.8"},"viewerVotes":{"$numberInt":"17"},"director":"Georges Mlis"}
{"_id":{"$oid":"58c59c6a99d4ee0af9e0c340"},"title":"Watering the Flowers","year":{"$numberInt":"1896"},"imdbId":"tt0000035","genre":["Short”],"viewerRating":{"$numberDouble":"5.3"},"viewerVotes":{"$numberInt":"33"},"director":"Georges M�li�s"}
{"_id":{"$oid":"58c59c6a99d4ee0af9e0c34a"},"title":"The Boxing Kangaroo","year":{"$numberInt":"1896"},"imdbId":"tt0000048","genre":["Short”],"viewerRating":{"$numberDouble":"5.2"},"viewerVotes":{"$numberInt":"48"},"director":"Birt Acres"}

Đầu ra mong đợi: phim tài liệu, ngắn: Documentary, Short

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

db.runCommand(
{
distinct: "<collection>",
key: "<field>",
query: <query>,
readConcern: <read concern document>,
collation: <collation document>,
comment: <any>
}
)
9Find các giá trị riêng biệt cho một trường được chỉ định trên một bộ sưu tập duy nhất.
db.runCommand(
{
distinct: "<collection>",
key: "<field>",
query: <query>,
readConcern: <read concern document>,
collation: <collation document>,
comment: <any>
}
)
9 Trả về một tài liệu chứa một mảng các giá trị riêng biệt. Tài liệu trả lại cũng chứa một tài liệu nhúng với số liệu thống kê truy vấn và kế hoạch truy vấn.

Finds the distinct values for a specified field across a single collection.

db.runCommand(
{
distinct: "<collection>",
key: "<field>",
query: <query>,
readConcern: <read concern document>,
collation: <collation document>,
comment: <any>
}
)
9 returns a document that contains an array of the distinct values. The return document also contains an embedded document with query statistics and the query plan.

Mẹo

Trong

collation: {
locale: <string>,
caseLevel: <boolean>,
caseFirst: <string>,
strength: <int>,
numericOrdering: <boolean>,
alternate: <string>,
maxVariable: <string>,
backwards: <boolean>
}
1, lệnh này cũng có thể được chạy thông qua phương thức trợ giúp
collation: {
locale: <string>,
caseLevel: <boolean>,
caseFirst: <string>,
strength: <int>,
numericOrdering: <boolean>,
alternate: <string>,
maxVariable: <string>,
backwards: <boolean>
}
2 ..
collation: {
locale: <string>,
caseLevel: <boolean>,
caseFirst: <string>,
strength: <int>,
numericOrdering: <boolean>,
alternate: <string>,
maxVariable: <string>,
backwards: <boolean>
}
1
, this command can also be run through the
collation: {
locale: <string>,
caseLevel: <boolean>,
caseFirst: <string>,
strength: <int>,
numericOrdering: <boolean>,
alternate: <string>,
maxVariable: <string>,
backwards: <boolean>
}
2 helper method..

Các phương thức trợ giúp thuận tiện cho người dùng

collation: {
locale: <string>,
caseLevel: <boolean>,
caseFirst: <string>,
strength: <int>,
numericOrdering: <boolean>,
alternate: <string>,
maxVariable: <string>,
backwards: <boolean>
}
1, nhưng chúng có thể không trả lại mức thông tin tương tự như các lệnh cơ sở dữ liệu. Trong trường hợp không cần thiết sự thuận tiện hoặc các trường trả về bổ sung được yêu cầu, hãy sử dụng lệnh cơ sở dữ liệu.
collation: {
locale: <string>,
caseLevel: <boolean>,
caseFirst: <string>,
strength: <int>,
numericOrdering: <boolean>,
alternate: <string>,
maxVariable: <string>,
backwards: <boolean>
}
1
users, but they may not return the same level of information as database commands. In cases where the convenience is not needed or the additional return fields are required, use the database command.

Lệnh có cú pháp sau:

db.runCommand(
{
distinct: "<collection>",
key: "<field>",
query: <query>,
readConcern: <read concern document>,
collation: <collation document>,
comment: <any>
}
)

Lệnh lấy các trường sau:

Đồng ruộng

Loại hình

Sự mô tả

db.runCommand(
{
distinct: "<collection>",
key: "<field>",
query: <query>,
readConcern: <read concern document>,
collation: <collation document>,
comment: <any>
}
)
9

sợi dây

Tên của bộ sưu tập để truy vấn cho các giá trị riêng biệt.

collation: {
locale: <string>,
caseLevel: <boolean>,
caseFirst: <string>,
strength: <int>,
numericOrdering: <boolean>,
alternate: <string>,
maxVariable: <string>,
backwards: <boolean>
}
5

sợi dây

Tên của bộ sưu tập để truy vấn cho các giá trị riêng biệt.

collation: {
locale: <string>,
caseLevel: <boolean>,
caseFirst: <string>,
strength: <int>,
numericOrdering: <boolean>,
alternate: <string>,
maxVariable: <string>,
backwards: <boolean>
}
6

Trường để trả về các giá trị riêng biệt.

tài liệu

collation: {
locale: <string>,
caseLevel: <boolean>,
caseFirst: <string>,
strength: <int>,
numericOrdering: <boolean>,
alternate: <string>,
maxVariable: <string>,
backwards: <boolean>
}
7

Trường để trả về các giá trị riêng biệt.

tài liệu

Không bắt buộc. Một truy vấn chỉ định các tài liệu để lấy các giá trị riêng biệt.

Không bắt buộc. Chỉ định mối quan tâm đọc.

  • Bắt đầu từ MongoDB 3.6, tùy chọn ReadConcern có cú pháp sau:

    collation: {
    locale: <string>,
    caseLevel: <boolean>,
    caseFirst: <string>,
    strength: <int>,
    numericOrdering: <boolean>,
    alternate: <string>,
    maxVariable: <string>,
    backwards: <boolean>
    }
    8

  • Có thể đọc các cấp độ quan tâm là:

  • collation: {
    locale: <string>,
    caseLevel: <boolean>,
    caseFirst: <string>,
    strength: <int>,
    numericOrdering: <boolean>,
    alternate: <string>,
    maxVariable: <string>,
    backwards: <boolean>
    }
    9. Đây là mức độ quan tâm đọc mặc định cho các hoạt động đọc so với chính và phụ.

  • { "_id": 1, "dept": "A", "item": { "sku": "111", "color": "red" }, "sizes": [ "S", "M" ] }
    { "_id": 2, "dept": "A", "item": { "sku": "111", "color": "blue" }, "sizes": [ "M", "L" ] }
    { "_id": 3, "dept": "B", "item": { "sku": "222", "color": "blue" }, "sizes": "S" }
    { "_id": 4, "dept": "A", "item": { "sku": "333", "color": "black" }, "sizes": [ "S" ] }
    0. Có sẵn cho các hoạt động đọc chống lại các chính và thứ hai.
    { "_id": 1, "dept": "A", "item": { "sku": "111", "color": "red" }, "sizes": [ "S", "M" ] }
    { "_id": 2, "dept": "A", "item": { "sku": "111", "color": "blue" }, "sizes": [ "M", "L" ] }
    { "_id": 3, "dept": "B", "item": { "sku": "222", "color": "blue" }, "sizes": "S" }
    { "_id": 4, "dept": "A", "item": { "sku": "333", "color": "black" }, "sizes": [ "S" ] }
    0 hoạt động giống như
    collation: {
    locale: <string>,
    caseLevel: <boolean>,
    caseFirst: <string>,
    strength: <int>,
    numericOrdering: <boolean>,
    alternate: <string>,
    maxVariable: <string>,
    backwards: <boolean>
    }
    9 so với các thứ hai chính và không có vỏ. Truy vấn trả về dữ liệu gần đây nhất của phiên bản.

{ "_id": 1, "dept": "A", "item": { "sku": "111", "color": "red" }, "sizes": [ "S", "M" ] }
{ "_id": 2, "dept": "A", "item": { "sku": "111", "color": "blue" }, "sizes": [ "M", "L" ] }
{ "_id": 3, "dept": "B", "item": { "sku": "222", "color": "blue" }, "sizes": "S" }
{ "_id": 4, "dept": "A", "item": { "sku": "333", "color": "black" }, "sizes": [ "S" ] }
3. Có sẵn cho các bộ bản sao sử dụng động cơ lưu trữ Wiredtiger.

{ "_id": 1, "dept": "A", "item": { "sku": "111", "color": "red" }, "sizes": [ "S", "M" ] }
{ "_id": 2, "dept": "A", "item": { "sku": "111", "color": "blue" }, "sizes": [ "M", "L" ] }
{ "_id": 3, "dept": "B", "item": { "sku": "222", "color": "blue" }, "sizes": "S" }
{ "_id": 4, "dept": "A", "item": { "sku": "333", "color": "black" }, "sizes": [ "S" ] }
6

Trường để trả về các giá trị riêng biệt.

Optional.

tài liệu

Không bắt buộc. Một truy vấn chỉ định các tài liệu để lấy các giá trị riêng biệt.

Không bắt buộc. Chỉ định mối quan tâm đọc.

collation: {
locale: <string>,
caseLevel: <boolean>,
caseFirst: <string>,
strength: <int>,
numericOrdering: <boolean>,
alternate: <string>,
maxVariable: <string>,
backwards: <boolean>
}

Bắt đầu từ MongoDB 3.6, tùy chọn ReadConcern có cú pháp sau:

collation: {
locale: <string>,
caseLevel: <boolean>,
caseFirst: <string>,
strength: <int>,
numericOrdering: <boolean>,
alternate: <string>,
maxVariable: <string>,
backwards: <boolean>
}
8

Có thể đọc các cấp độ quan tâm là:

collation: {
locale: <string>,
caseLevel: <boolean>,
caseFirst: <string>,
strength: <int>,
numericOrdering: <boolean>,
alternate: <string>,
maxVariable: <string>,
backwards: <boolean>
}
9. Đây là mức độ quan tâm đọc mặc định cho các hoạt động đọc so với chính và phụ.

{ "_id": 1, "dept": "A", "item": { "sku": "111", "color": "red" }, "sizes": [ "S", "M" ] }
{ "_id": 2, "dept": "A", "item": { "sku": "111", "color": "blue" }, "sizes": [ "M", "L" ] }
{ "_id": 3, "dept": "B", "item": { "sku": "222", "color": "blue" }, "sizes": "S" }
{ "_id": 4, "dept": "A", "item": { "sku": "333", "color": "black" }, "sizes": [ "S" ] }
0. Có sẵn cho các hoạt động đọc chống lại các chính và thứ hai.
{ "_id": 1, "dept": "A", "item": { "sku": "111", "color": "red" }, "sizes": [ "S", "M" ] }
{ "_id": 2, "dept": "A", "item": { "sku": "111", "color": "blue" }, "sizes": [ "M", "L" ] }
{ "_id": 3, "dept": "B", "item": { "sku": "222", "color": "blue" }, "sizes": "S" }
{ "_id": 4, "dept": "A", "item": { "sku": "333", "color": "black" }, "sizes": [ "S" ] }
0 hoạt động giống như
collation: {
locale: <string>,
caseLevel: <boolean>,
caseFirst: <string>,
strength: <int>,
numericOrdering: <boolean>,
alternate: <string>,
maxVariable: <string>,
backwards: <boolean>
}
9 so với các thứ hai chính và không có vỏ. Truy vấn trả về dữ liệu gần đây nhất của phiên bản.

{ "_id": 1, "dept": "A", "item": { "sku": "111", "color": "red" }, "sizes": [ "S", "M" ] }
{ "_id": 2, "dept": "A", "item": { "sku": "111", "color": "blue" }, "sizes": [ "M", "L" ] }
{ "_id": 3, "dept": "B", "item": { "sku": "222", "color": "blue" }, "sizes": "S" }
{ "_id": 4, "dept": "A", "item": { "sku": "333", "color": "black" }, "sizes": [ "S" ] }
9

không tí nào

Optional.

Một nhận xét do người dùng cung cấp để đính kèm vào lệnh này. Sau khi được đặt, nhận xét này xuất hiện cùng với các bản ghi của lệnh này ở các vị trí sau:

  • Tin nhắn nhật ký Mongod, trong trường

    db.runCommand ( { distinct: "inventory", key: "dept" } )

    0.

  • Đầu ra hồ sơ cơ sở dữ liệu, trong trường

    db.runCommand ( { distinct: "inventory", key: "dept" } )

    1.

  • Đầu ra

    db.runCommand ( { distinct: "inventory", key: "dept" } )

    2, trong trường

    db.runCommand ( { distinct: "inventory", key: "dept" } )

    1.

Một nhận xét có thể là bất kỳ loại BSON hợp lệ (chuỗi, số nguyên, đối tượng, mảng, v.v.).

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

Ghi chú

MongoDB cũng cung cấp phương thức trình bao bọc shell

collation: {
locale: <string>,
caseLevel: <boolean>,
caseFirst: <string>,
strength: <int>,
numericOrdering: <boolean>,
alternate: <string>,
maxVariable: <string>,
backwards: <boolean>
}
2 cho lệnh
db.runCommand(
{
distinct: "<collection>",
key: "<field>",
query: <query>,
readConcern: <read concern document>,
collation: <collation document>,
comment: <any>
}
)
9. Ngoài ra, nhiều trình điều khiển MongoDB cung cấp một phương thức trình bao bọc. Tham khảo tài liệu trình điều khiển cụ thể.
db.runCommand(
{
distinct: "<collection>",
key: "<field>",
query: <query>,
readConcern: <read concern document>,
collation: <collation document>,
comment: <any>
}
)
9
command. Additionally, many MongoDB drivers provide a wrapper method. Refer to the specific driver documentation.

Trong một cụm bị che khuất, lệnh

db.runCommand(
{
distinct: "<collection>",
key: "<field>",
query: <query>,
readConcern: <read concern document>,
collation: <collation document>,
comment: <any>
}
)
9 có thể trả lại các tài liệu mồ côi.
db.runCommand(
{
distinct: "<collection>",
key: "<field>",
query: <query>,
readConcern: <read concern document>,
collation: <collation document>,
comment: <any>
}
)
9
command may return orphaned documents.

Nếu giá trị của

db.runCommand ( { distinct: "inventory", key: "dept" } )

7 được chỉ định là một mảng,
db.runCommand(
{
distinct: "<collection>",
key: "<field>",
query: <query>,
readConcern: <read concern document>,
collation: <collation document>,
comment: <any>
}
)
9 coi mỗi phần tử của mảng là một giá trị riêng biệt.
db.runCommand(
{
distinct: "<collection>",
key: "<field>",
query: <query>,
readConcern: <read concern document>,
collation: <collation document>,
comment: <any>
}
)
9
considers each element of the array as a separate value.

Chẳng hạn, nếu một trường có giá trị của nó

db.runCommand ( { distinct: "inventory", key: "dept" } )

9, thì
db.runCommand(
{
distinct: "<collection>",
key: "<field>",
query: <query>,
readConcern: <read concern document>,
collation: <collation document>,
comment: <any>
}
)
9 coi
{
"values" : [ "A", "B" ],
"ok" : 1
}
1,
{
"values" : [ "A", "B" ],
"ok" : 1
}
2 và
{
"values" : [ "A", "B" ],
"ok" : 1
}
1 là các giá trị riêng biệt.
db.runCommand(
{
distinct: "<collection>",
key: "<field>",
query: <query>,
readConcern: <read concern document>,
collation: <collation document>,
comment: <any>
}
)
9
considers
{
"values" : [ "A", "B" ],
"ok" : 1
}
1,
{
"values" : [ "A", "B" ],
"ok" : 1
}
2, and
{
"values" : [ "A", "B" ],
"ok" : 1
}
1 as separate values.

Bắt đầu từ MongoDB 6.0, lệnh

db.runCommand(
{
distinct: "<collection>",
key: "<field>",
query: <query>,
readConcern: <read concern document>,
collation: <collation document>,
comment: <any>
}
)
9 trả về kết quả tương tự cho các bộ sưu tập và chế độ xem khi sử dụng mảng.
db.runCommand(
{
distinct: "<collection>",
key: "<field>",
query: <query>,
readConcern: <read concern document>,
collation: <collation document>,
comment: <any>
}
)
9
command returns the same results for collections and views when using arrays.

Để biết ví dụ, xem:

  • Trả về các giá trị riêng biệt cho một trường mảng

  • Mảng trong bộ sưu tập và chế độ xem

Khi có thể, các hoạt động

db.runCommand(
{
distinct: "<collection>",
key: "<field>",
query: <query>,
readConcern: <read concern document>,
collation: <collation document>,
comment: <any>
}
)
9 có thể sử dụng các chỉ mục.
db.runCommand(
{
distinct: "<collection>",
key: "<field>",
query: <query>,
readConcern: <read concern document>,
collation: <collation document>,
comment: <any>
}
)
9
operations can use indexes.

Các chỉ mục cũng có thể bao gồm các hoạt động

db.runCommand(
{
distinct: "<collection>",
key: "<field>",
query: <query>,
readConcern: <read concern document>,
collation: <collation document>,
comment: <any>
}
)
9. Xem truy vấn được bảo hiểm để biết thêm thông tin về các truy vấn được bao gồm bởi các chỉ mục.
db.runCommand(
{
distinct: "<collection>",
key: "<field>",
query: <query>,
readConcern: <read concern document>,
collation: <collation document>,
comment: <any>
}
)
9
operations. See Covered Query for more information on queries covered by indexes.

Để thực hiện một hoạt động khác biệt trong một giao dịch:

  • Đối với các bộ sưu tập không có vỏ, bạn có thể sử dụng phương thức

    collation: {
    locale: <string>,
    caseLevel: <boolean>,
    caseFirst: <string>,
    strength: <int>,
    numericOrdering: <boolean>,
    alternate: <string>,
    maxVariable: <string>,
    backwards: <boolean>
    }
    2/lệnh
    db.runCommand(
    {
    distinct: "<collection>",
    key: "<field>",
    query: <query>,
    readConcern: <read concern document>,
    collation: <collation document>,
    comment: <any>
    }
    )
    9 cũng như đường ống tổng hợp với giai đoạn
    {
    "values" : [ "A", "B" ],
    "ok" : 1
    }
    9.
    db.runCommand(
    {
    distinct: "<collection>",
    key: "<field>",
    query: <query>,
    readConcern: <read concern document>,
    collation: <collation document>,
    comment: <any>
    }
    )
    9
    command as well as the aggregation pipeline with the
    {
    "values" : [ "A", "B" ],
    "ok" : 1
    }
    9 stage.

  • Đối với các bộ sưu tập Sharded, bạn không thể sử dụng phương thức

    collation: {
    locale: <string>,
    caseLevel: <boolean>,
    caseFirst: <string>,
    strength: <int>,
    numericOrdering: <boolean>,
    alternate: <string>,
    maxVariable: <string>,
    backwards: <boolean>
    }
    2 hoặc lệnh
    db.runCommand(
    {
    distinct: "<collection>",
    key: "<field>",
    query: <query>,
    readConcern: <read concern document>,
    collation: <collation document>,
    comment: <any>
    }
    )
    9.
    db.runCommand(
    {
    distinct: "<collection>",
    key: "<field>",
    query: <query>,
    readConcern: <read concern document>,
    collation: <collation document>,
    comment: <any>
    }
    )
    9
    command.

    Để tìm các giá trị riêng biệt cho một bộ sưu tập Sharded, hãy sử dụng đường ống tổng hợp với giai đoạn

    {
    "values" : [ "A", "B" ],
    "ok" : 1
    }
    9 thay thế. Xem hoạt động khác biệt để biết chi tiết.

Quan trọng

Trong hầu hết các trường hợp, giao dịch đa tài liệu phát sinh chi phí hiệu suất lớn hơn so với ghi tài liệu đơn lẻ và sự sẵn có của các giao dịch đa tài liệu không nên là một sự thay thế cho thiết kế lược đồ hiệu quả. Đối với nhiều kịch bản, mô hình dữ liệu được chuẩn hóa (tài liệu và mảng nhúng) sẽ tiếp tục tối ưu cho các trường hợp dữ liệu và sử dụng của bạn. Đó là, đối với nhiều kịch bản, mô hình hóa dữ liệu của bạn một cách thích hợp sẽ giảm thiểu nhu cầu cho các giao dịch đa tài liệu.

Để biết các cân nhắc sử dụng giao dịch bổ sung (như giới hạn thời gian chạy và giới hạn kích thước oplog), xem thêm các cân nhắc sản xuất.

Bắt đầu từ MongoDB 4.2, nếu khách hàng phát hành

db.runCommand(
{
distinct: "<collection>",
key: "<field>",
query: <query>,
readConcern: <read concern document>,
collation: <collation document>,
comment: <any>
}
)
9 ngắt kết nối trước khi hoạt động hoàn thành, MongoDB đánh dấu
db.runCommand(
{
distinct: "<collection>",
key: "<field>",
query: <query>,
readConcern: <read concern document>,
collation: <collation document>,
comment: <any>
}
)
9 để chấm dứt bằng cách sử dụng

db.runCommand ( { distinct: "inventory", key: "item.sku" } )

5
db.runCommand(
{
distinct: "<collection>",
key: "<field>",
query: <query>,
readConcern: <read concern document>,
collation: <collation document>,
comment: <any>
}
)
9
disconnects before the operation completes, MongoDB marks
db.runCommand(
{
distinct: "<collection>",
key: "<field>",
query: <query>,
readConcern: <read concern document>,
collation: <collation document>,
comment: <any>
}
)
9
for termination using

db.runCommand ( { distinct: "inventory", key: "item.sku" } )

5

Bắt đầu từ MongoDB 4.4, để chạy trên một thành viên tập hợp, các hoạt động

db.runCommand(
{
distinct: "<collection>",
key: "<field>",
query: <query>,
readConcern: <read concern document>,
collation: <collation document>,
comment: <any>
}
)
9 yêu cầu thành viên phải ở trạng thái

db.runCommand ( { distinct: "inventory", key: "item.sku" } )

7 hoặc

db.runCommand ( { distinct: "inventory", key: "item.sku" } )

8. Nếu thành viên ở một trạng thái khác, chẳng hạn như

db.runCommand ( { distinct: "inventory", key: "item.sku" } )

9, các lỗi hoạt động.
db.runCommand(
{
distinct: "<collection>",
key: "<field>",
query: <query>,
readConcern: <read concern document>,
collation: <collation document>,
comment: <any>
}
)
9
operations require the member to be in

db.runCommand ( { distinct: "inventory", key: "item.sku" } )

7 or

db.runCommand ( { distinct: "inventory", key: "item.sku" } )

8 state. If the member is in another state, such as

db.runCommand ( { distinct: "inventory", key: "item.sku" } )

9, the operation errors.

Trong các phiên bản trước, các hoạt động cũng chạy khi thành viên ở

db.runCommand ( { distinct: "inventory", key: "item.sku" } )

9. Các hoạt động đợi cho đến khi thành viên chuyển sang
{
"values" : [ "111", "222", "333" ],
"ok" : 1
}
1

Bắt đầu từ MongoDB 6.0, bộ lọc chỉ mục sử dụng bộ đối chiếu được đặt trước đó bằng lệnh

{
"values" : [ "111", "222", "333" ],
"ok" : 1
}
2.

Các ví dụ sử dụng bộ sưu tập

{
"values" : [ "111", "222", "333" ],
"ok" : 1
}
3 có chứa các tài liệu sau:

{ "_id": 1, "dept": "A", "item": { "sku": "111", "color": "red" }, "sizes": [ "S", "M" ] }
{ "_id": 2, "dept": "A", "item": { "sku": "111", "color": "blue" }, "sizes": [ "M", "L" ] }
{ "_id": 3, "dept": "B", "item": { "sku": "222", "color": "blue" }, "sizes": "S" }
{ "_id": 4, "dept": "A", "item": { "sku": "333", "color": "black" }, "sizes": [ "S" ] }

Ví dụ sau trả về các giá trị riêng biệt cho trường

{
"values" : [ "111", "222", "333" ],
"ok" : 1
}
4 từ tất cả các tài liệu trong bộ sưu tập
{
"values" : [ "111", "222", "333" ],
"ok" : 1
}
3:

db.runCommand ( { distinct: "inventory", key: "dept" } )

Lệnh trả về một tài liệu với một trường có tên

{
"values" : [ "111", "222", "333" ],
"ok" : 1
}
6 chứa các giá trị
{
"values" : [ "111", "222", "333" ],
"ok" : 1
}
4 riêng biệt:

{
"values" : [ "A", "B" ],
"ok" : 1
}

Ví dụ sau trả về các giá trị riêng biệt cho trường

{
"values" : [ "111", "222", "333" ],
"ok" : 1
}
8, được nhúng trong trường
{
"values" : [ "111", "222", "333" ],
"ok" : 1
}
9, từ tất cả các tài liệu trong bộ sưu tập
{
"values" : [ "111", "222", "333" ],
"ok" : 1
}
3:

db.runCommand ( { distinct: "inventory", key: "item.sku" } )

Lệnh trả về một tài liệu với một trường có tên

{
"values" : [ "111", "222", "333" ],
"ok" : 1
}
6 chứa các giá trị
{
"values" : [ "111", "222", "333" ],
"ok" : 1
}
8 riêng biệt:

{
"values" : [ "111", "222", "333" ],
"ok" : 1
}

Mẹo

Xem thêm:

Ký hiệu chấm cho thông tin về truy cập các trường trong các tài liệu nhúng

Ví dụ sau trả về các giá trị riêng biệt cho trường

db.runCommand ( { distinct: "inventory", key: "sizes" } )

3 từ tất cả các tài liệu trong bộ sưu tập
{
"values" : [ "111", "222", "333" ],
"ok" : 1
}
3:

db.runCommand ( { distinct: "inventory", key: "sizes" } )

Lệnh trả về một tài liệu với một trường có tên

{
"values" : [ "111", "222", "333" ],
"ok" : 1
}
6 chứa các giá trị

db.runCommand ( { distinct: "inventory", key: "sizes" } )

3 riêng biệt:

{
"values" : [ "M", "S", "L" ],
"ok" : 1
}

Để biết thông tin về các trường

db.runCommand(
{
distinct: "<collection>",
key: "<field>",
query: <query>,
readConcern: <read concern document>,
collation: <collation document>,
comment: <any>
}
)
9 và mảng, hãy xem phần Hành vi.
db.runCommand(
{
distinct: "<collection>",
key: "<field>",
query: <query>,
readConcern: <read concern document>,
collation: <collation document>,
comment: <any>
}
)
9
and array fields, see the Behavior section.

Bắt đầu từ MongoDB 6.0, lệnh

db.runCommand(
{
distinct: "<collection>",
key: "<field>",
query: <query>,
readConcern: <read concern document>,
collation: <collation document>,
comment: <any>
}
)
9 trả về kết quả tương tự cho các bộ sưu tập và chế độ xem khi sử dụng mảng.
db.runCommand(
{
distinct: "<collection>",
key: "<field>",
query: <query>,
readConcern: <read concern document>,
collation: <collation document>,
comment: <any>
}
)
9
command returns the same results for collections and views when using arrays.

Để biết ví dụ, xem:

db.runCommand(
{
distinct: "<collection>",
key: "<field>",
query: <query>,
readConcern: <read concern document>,
collation: <collation document>,
comment: <any>
}
)
0

Trả về các giá trị riêng biệt cho một trường mảng

db.runCommand(
{
distinct: "<collection>",
key: "<field>",
query: <query>,
readConcern: <read concern document>,
collation: <collation document>,
comment: <any>
}
)
1

Mảng trong bộ sưu tập và chế độ xem

db.runCommand(
{
distinct: "<collection>",
key: "<field>",
query: <query>,
readConcern: <read concern document>,
collation: <collation document>,
comment: <any>
}
)
9 to return the unique values from the
{
"values" : [ "M", "S", "L" ],
"ok" : 1
}
3 array in the

db.runCommand ( { distinct: "inventory", key: "sizes" } )

9 collection:

db.runCommand(
{
distinct: "<collection>",
key: "<field>",
query: <query>,
readConcern: <read concern document>,
collation: <collation document>,
comment: <any>
}
)
2

Khi có thể, các hoạt động

db.runCommand(
{
distinct: "<collection>",
key: "<field>",
query: <query>,
readConcern: <read concern document>,
collation: <collation document>,
comment: <any>
}
)
9 có thể sử dụng các chỉ mục.

Ví dụ đầu ra:

Ví dụ cho

{
"values" : [ "M", "S", "L" ],
"ok" : 1
}
0:

db.runCommand(
{
distinct: "<collection>",
key: "<field>",
query: <query>,
readConcern: <read concern document>,
collation: <collation document>,
comment: <any>
}
)
3

Ví dụ đầu ra:

  • {
    "values" : [ "M", "S", "L" ],
    "ok" : 1
    }
    9 bắt đầu từ MongoDB 6.0 (giống hệt kết quả được trả về từ bộ sưu tập

    db.runCommand ( { distinct: "inventory", key: "sizes" } )

    9).

  • db.runCommand(
    {
    distinct: "<collection>",
    key: "<field>",
    query: <query>,
    readConcern: <read concern document>,
    collation: <collation document>,
    comment: <any>
    }
    )
    01 trong phiên bản MongoDB sớm hơn 6.0.

Ví dụ sau trả về các giá trị riêng biệt cho trường

{
"values" : [ "111", "222", "333" ],
"ok" : 1
}
8, được nhúng trong trường
{
"values" : [ "111", "222", "333" ],
"ok" : 1
}
9, từ các tài liệu có
{
"values" : [ "111", "222", "333" ],
"ok" : 1
}
4 bằng
db.runCommand(
{
distinct: "<collection>",
key: "<field>",
query: <query>,
readConcern: <read concern document>,
collation: <collation document>,
comment: <any>
}
)
05:

db.runCommand(
{
distinct: "<collection>",
key: "<field>",
query: <query>,
readConcern: <read concern document>,
collation: <collation document>,
comment: <any>
}
)
4

Lệnh trả về một tài liệu với một trường có tên

{
"values" : [ "111", "222", "333" ],
"ok" : 1
}
6 chứa các giá trị
{
"values" : [ "111", "222", "333" ],
"ok" : 1
}
8 riêng biệt:

db.runCommand(
{
distinct: "<collection>",
key: "<field>",
query: <query>,
readConcern: <read concern document>,
collation: <collation document>,
comment: <any>
}
)
5

Đối chiếu cho phép người dùng chỉ định các quy tắc cụ thể về ngôn ngữ để so sánh chuỗi, chẳng hạn như các quy tắc cho Lettercase và Dấu nhấn.

Bộ sưu tập

db.runCommand(
{
distinct: "<collection>",
key: "<field>",
query: <query>,
readConcern: <read concern document>,
collation: <collation document>,
comment: <any>
}
)
08 có các tài liệu sau:

db.runCommand(
{
distinct: "<collection>",
key: "<field>",
query: <query>,
readConcern: <read concern document>,
collation: <collation document>,
comment: <any>
}
)
6

Hoạt động tổng hợp sau đây bao gồm tùy chọn đối chiếu:

db.runCommand(
{
distinct: "<collection>",
key: "<field>",
query: <query>,
readConcern: <read concern document>,
collation: <collation document>,
comment: <any>
}
)
7

Để biết mô tả về các trường đối chiếu, xem tài liệu đối chiếu.

Để ghi đè lên mức độ liên quan đọc mặc định của

collation: {
locale: <string>,
caseLevel: <boolean>,
caseFirst: <string>,
strength: <int>,
numericOrdering: <boolean>,
alternate: <string>,
maxVariable: <string>,
backwards: <boolean>
}
9, hãy sử dụng tùy chọn
collation: {
locale: <string>,
caseLevel: <boolean>,
caseFirst: <string>,
strength: <int>,
numericOrdering: <boolean>,
alternate: <string>,
maxVariable: <string>,
backwards: <boolean>
}
7.

Hoạt động sau đây trên một bộ bản sao chỉ định mối quan tâm đọc của

{ "_id": 1, "dept": "A", "item": { "sku": "111", "color": "red" }, "sizes": [ "S", "M" ] }
{ "_id": 2, "dept": "A", "item": { "sku": "111", "color": "blue" }, "sizes": [ "M", "L" ] }
{ "_id": 3, "dept": "B", "item": { "sku": "222", "color": "blue" }, "sizes": "S" }
{ "_id": 4, "dept": "A", "item": { "sku": "333", "color": "black" }, "sizes": [ "S" ] }
3 để đọc bản sao gần đây nhất của dữ liệu được xác nhận là đã được viết cho phần lớn các nút.

Ghi chú

Bất kể mức độ quan tâm đọc, dữ liệu gần đây nhất trên một nút có thể không phản ánh phiên bản gần đây nhất của dữ liệu trong hệ thống.

db.runCommand(
{
distinct: "<collection>",
key: "<field>",
query: <query>,
readConcern: <read concern document>,
collation: <collation document>,
comment: <any>
}
)
8

Để đảm bảo rằng một luồng duy nhất có thể đọc các chữ viết của riêng mình, hãy sử dụng

{ "_id": 1, "dept": "A", "item": { "sku": "111", "color": "red" }, "sizes": [ "S", "M" ] }
{ "_id": 2, "dept": "A", "item": { "sku": "111", "color": "blue" }, "sizes": [ "M", "L" ] }
{ "_id": 3, "dept": "B", "item": { "sku": "222", "color": "blue" }, "sizes": "S" }
{ "_id": 4, "dept": "A", "item": { "sku": "333", "color": "black" }, "sizes": [ "S" ] }
3 Đọc mối quan tâm và
{ "_id": 1, "dept": "A", "item": { "sku": "111", "color": "red" }, "sizes": [ "S", "M" ] }
{ "_id": 2, "dept": "A", "item": { "sku": "111", "color": "blue" }, "sizes": [ "M", "L" ] }
{ "_id": 3, "dept": "B", "item": { "sku": "222", "color": "blue" }, "sizes": "S" }
{ "_id": 4, "dept": "A", "item": { "sku": "333", "color": "black" }, "sizes": [ "S" ] }
3 Viết mối quan tâm chống lại bản sao của bộ bản sao.

Làm thế nào có được dữ liệu khác biệt từ MongoDB?

Phương thức MongoDB - Disport () trong MongoDB, phương thức riêng biệt () tìm thấy các giá trị riêng biệt cho một trường đã cho trên một bộ sưu tập duy nhất và trả về kết quả trong một mảng. Phải mất ba tham số đầu tiên là trường để trả về các giá trị riêng biệt và các tham số khác là tùy chọn.Distinct() Method In MongoDB, the distinct() method finds the distinct values for a given field across a single collection and returns the results in an array. It takes three parameters first one is the field for which to return distinct values and the others are optional.

Tôi có thể viết truy vấn bằng la bàn MongoDB không?

Có, Mongo La bàn chỉ cung cấp tùy chọn bộ lọc (thanh truy vấn) để thực hiện các truy vấn trên bộ sưu tập cụ thể..

Chúng ta có thể sử dụng tìm và khác biệt với nhau trong MongoDB không?

Cả hai hoạt động tìm thấy và khác biệt đều rất hữu ích khi lấy dữ liệu từ MongoDB.Hoạt động tìm trả về tất cả dữ liệu cùng một lúc hoặc theo truy vấn và trình chiếu.Hoạt động riêng biệt có chức năng đặc biệt là truy xuất các giá trị duy nhất của một trường được chỉ định.. The find operation returns the data all at once or according to the query and projection. The distinct operation has a special functionality of retrieving unique values of a specified field.

Sự khác biệt giữa la bàn MongoDB và MongoDB là gì?

MongoDB Atlas thuộc danh mục "MongoDB Hosting" của Stack Tech, trong khi Compass MongoDB có thể được phân loại chủ yếu theo "công cụ cơ sở dữ liệu".Một số tính năng được cung cấp bởi MongoDB Atlas là: các cụm toàn cầu cho các ứng dụng đẳng cấp thế giới.Hỗ trợ cho hơn 60 vùng đám mây trên khắp AWS, Azure, & GCP.. Some of the features offered by MongoDB Atlas are: Global clusters for world-class applications. Support for 60+ cloud regions across AWS, Azure, & GCP.