Hướng dẫn mongodb combine two queries - mongodb kết hợp hai truy vấn

Tôi muốn phân chia các truy vấn dài của mình bằng MongoDB thay vào đó sử dụng db.users.find (q) trong mã sau, tôi muốn sử dụng phiên bản concatenate như db.users.find (q0+q1) hoặc bất kỳ cách nào khác mà Tôi có thể chia q thành hai truy vấn

Nội dung chính ShowShow

  • Trang này mô tả giai đoạn $merge, đưa ra kết quả đường ống tổng hợp cho một bộ sưu tập. Đối với toán tử $mergeObjects, hợp nhất các tài liệu vào một tài liệu, xem $mergeObjects
  • Làm cách nào để tham gia hai truy vấn trong MongoDB?
  • Người vận hành nào được sử dụng Kết hợp 2 biểu thức trong MongoDB?
  • Truy vấn tổng hợp trong MongoDB là gì?
  • Làm cách nào để truy vấn nhiều giá trị trong MongoDB?

q= {},{name:1,"education.school.name":1,"education.type":1} 
db.users.find(q)  
q0={}
q1={name:1,"education.school.name":1,"education.type":1}   
q=q0+","+q1 
db.users.find(q)

Làm thế nào để tôi làm một cái gì đó như vậy?

Hỏi ngày 13 tháng 12 năm 2012 lúc 16:04Dec 13, 2012 at 16:04Dec 13, 2012 at 16:04

Hướng dẫn mongodb combine two queries - mongodb kết hợp hai truy vấn

2

Các điều khoản của bạn có phần mơ hồ ở đó (cả

{ $merge: {
into: -or- { db: , coll: },
on: -or- [ , ...], // Optional
let: , // Optional
whenMatched: , // Optional
whenNotMatched: // Optional
} }
3 và
{ $merge: {
into: -or- { db: , coll: },
on: -or- [ , ...], // Optional
let: , // Optional
whenMatched: , // Optional
whenNotMatched: // Optional
} }
4 của bạn đều là các đối tượng trống), nhưng bạn có thể muốn người vận hành $ hoặc toán tử

db.users.find(
  $or: [
    {name:1,"education.school.name":1,"education.type":1},
    {age:1,"education.school.name":2,"education.type":2} 
  ]
)

Điều này sẽ trả lại sự kết hợp của hai truy vấn đó. Điều đó nói rằng, tùy thuộc vào những gì bạn thực sự muốn làm, bạn có thể sử dụng

{ $merge: {
into: -or- { db: , coll: },
on: -or- [ , ...], // Optional
let: , // Optional
whenMatched: , // Optional
whenNotMatched: // Optional
} }
5 với một loạt các giá trị để tìm kiếm cho mỗi trường, nếu bạn chỉ muốn tìm kiếm trên nhiều giá trị.

Đã trả lời ngày 13 tháng 12 năm 2012 lúc 16:09Dec 13, 2012 at 16:09Dec 13, 2012 at 16:09

Chris Healdchris HealdChris HealdChris Heald

59,9k10 Huy hiệu vàng119 Huy hiệu bạc133 Huy hiệu đồng10 gold badges119 silver badges133 bronze badges10 gold badges119 silver badges133 bronze badges

4

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

Ghi chú

Trang này mô tả giai đoạn

{ $merge: {
into: -or- { db: , coll: },
on: -or- [ , ...], // Optional
let: , // Optional
whenMatched: , // Optional
whenNotMatched: // Optional
} }
6, đưa ra kết quả đường ống tổng hợp cho một bộ sưu tập. Đối với toán tử
{ $merge: {
into: -or- { db: , coll: },
on: -or- [ , ...], // Optional
let: , // Optional
whenMatched: , // Optional
whenNotMatched: // Optional
} }
7, hợp nhất các tài liệu vào một tài liệu, xem
{ $merge: {
into: -or- { db: , coll: },
on: -or- [ , ...], // Optional
let: , // Optional
whenMatched: , // Optional
whenNotMatched: // Optional
} }
7
{ $merge: {
into: -or- { db: , coll: },
on: -or- [ , ...], // Optional
let: , // Optional
whenMatched: , // Optional
whenNotMatched: // Optional
} }
6 stage, which outputs the aggregation pipeline results to a collection. For the
{ $merge: {
into: -or- { db: , coll: },
on: -or- [ , ...], // Optional
let: , // Optional
whenMatched: , // Optional
whenNotMatched: // Optional
} }
7 operator, which merges documents into a single document, see
{ $merge: {
into: -or- { db: , coll: },
on: -or- [ , ...], // Optional
let: , // Optional
whenMatched: , // Optional
whenNotMatched: // Optional
} }
7
{ $merge: {
into: -or- { db: , coll: },
on: -or- [ , ...], // Optional
let: , // Optional
whenMatched: , // Optional
whenNotMatched: // Optional
} }
6
stage, which outputs the aggregation pipeline results to a collection. For the
{ $merge: {
into: -or- { db: , coll: },
on: -or- [ , ...], // Optional
let: , // Optional
whenMatched: , // Optional
whenNotMatched: // Optional
} }
7 operator, which merges documents into a single document, see
{ $merge: {
into: -or- { db: , coll: },
on: -or- [ , ...], // Optional
let: , // Optional
whenMatched: , // Optional
whenNotMatched: // Optional
} }
7

{ $merge: {
into: -or- { db: , coll: },
on: -or- [ , ...], // Optional
let: , // Optional
whenMatched: , // Optional
whenNotMatched: // Optional
} }
6 viết kết quả của đường ống tổng hợp cho một bộ sưu tập được chỉ định. Nhà điều hành
{ $merge: {
into: -or- { db: , coll: },
on: -or- [ , ...], // Optional
let: , // Optional
whenMatched: , // Optional
whenNotMatched: // Optional
} }
6 phải là giai đoạn cuối cùng trong đường ống.

Writes the results of the aggregation pipeline to a specified collection. The

{ $merge: {
into: -or- { db: , coll: },
on: -or- [ , ...], // Optional
let: , // Optional
whenMatched: , // Optional
whenNotMatched: // Optional
} }
6 operator must be the last stage in the pipeline.
{ $merge: {
into: -or- { db: , coll: },
on: -or- [ , ...], // Optional
let: , // Optional
whenMatched: , // Optional
whenNotMatched: // Optional
} }
6
operator must be the last stage in the pipeline.

Giai đoạn

{ $merge: {
into: -or- { db: , coll: },
on: -or- [ , ...], // Optional
let: , // Optional
whenMatched: , // Optional
whenNotMatched: // Optional
} }
6:
{ $merge: {
into: -or- { db: , coll: },
on: -or- [ , ...], // Optional
let: , // Optional
whenMatched: , // Optional
whenNotMatched: // Optional
} }
6 stage:
{ $merge: {
into: -or- { db: , coll: },
on: -or- [ , ...], // Optional
let: , // Optional
whenMatched: , // Optional
whenNotMatched: // Optional
} }
6
stage:

  • Có thể xuất vào một bộ sưu tập trong cùng một cơ sở dữ liệu hoặc khác nhau.

  • Bắt đầu từ MongoDB 4.4:

    • { $merge: {
      into: -or- { db: , coll: },
      on: -or- [ , ...], // Optional
      let: , // Optional
      whenMatched: , // Optional
      whenNotMatched: // Optional
      } }
      6 có thể đầu ra cho cùng một bộ sưu tập đang được tổng hợp. Để biết thêm thông tin, hãy xem đầu ra cho cùng một bộ sưu tập đang được tổng hợp. can output to the same collection that is being aggregated. For more information, see Output to the Same Collection that is Being Aggregated. can output to the same collection that is being aggregated. For more information, see Output to the Same Collection that is Being Aggregated.

    • Các đường ống với giai đoạn

      { $merge: {
      into: -or- { db: , coll: },
      on: -or- [ , ...], // Optional
      let: , // Optional
      whenMatched: , // Optional
      whenNotMatched: // Optional
      } }
      6 có thể chạy trên các nút thứ cấp đặt bản sao nếu tất cả các nút trong cụm có tính năng tương tự featureCompitionVersion được đặt thành

      db.users.find(
        $or: [
          {name:1,"education.school.name":1,"education.type":1},
          {age:1,"education.school.name":2,"education.type":2} 
        ]
      )
      
      6 hoặc cao hơn và sở thích đọc cho phép đọc thứ cấp.
      { $merge: {
      into: -or- { db: , coll: },
      on: -or- [ , ...], // Optional
      let: , // Optional
      whenMatched: , // Optional
      whenNotMatched: // Optional
      } }
      6 stage can run on replica set secondary nodes if all the nodes in cluster have featureCompatibilityVersion set to
      db.users.find(
        $or: [
          {name:1,"education.school.name":1,"education.type":1},
          {age:1,"education.school.name":2,"education.type":2} 
        ]
      )
      
      6 or higher and the Read Preference allows secondary reads.
      { $merge: {
      into: -or- { db: , coll: },
      on: -or- [ , ...], // Optional
      let: , // Optional
      whenMatched: , // Optional
      whenNotMatched: // Optional
      } }
      6
      stage can run on replica set secondary nodes if all the nodes in cluster have featureCompatibilityVersion set to
      db.users.find(
        $or: [
          {name:1,"education.school.name":1,"education.type":1},
          {age:1,"education.school.name":2,"education.type":2} 
        ]
      )
      
      6 or higher and the Read Preference allows secondary reads.
      • Đọc các hoạt động của câu lệnh

        { $merge: {
        into: -or- { db: , coll: },
        on: -or- [ , ...], // Optional
        let: , // Optional
        whenMatched: , // Optional
        whenNotMatched: // Optional
        } }
        6 được gửi đến các nút thứ cấp, trong khi các hoạt động ghi chỉ xảy ra trên nút chính.
        { $merge: {
        into: -or- { db: , coll: },
        on: -or- [ , ...], // Optional
        let: , // Optional
        whenMatched: , // Optional
        whenNotMatched: // Optional
        } }
        6 statement are sent to secondary nodes, while the write operations occur only on the primary node.
        { $merge: {
        into: -or- { db: , coll: },
        on: -or- [ , ...], // Optional
        let: , // Optional
        whenMatched: , // Optional
        whenNotMatched: // Optional
        } }
        6
        statement are sent to secondary nodes, while the write operations occur only on the primary node.

      • Không phải tất cả các phiên bản trình điều khiển đều hỗ trợ nhắm mục tiêu các hoạt động

        { $merge: {
        into: -or- { db: , coll: },
        on: -or- [ , ...], // Optional
        let: , // Optional
        whenMatched: , // Optional
        whenNotMatched: // Optional
        } }
        6 để bản sao đặt các nút thứ cấp. Kiểm tra tài liệu trình điều khiển của bạn để xem khi trình điều khiển của bạn thêm hỗ trợ cho các hoạt động đọc
        { $merge: {
        into: -or- { db: , coll: },
        on: -or- [ , ...], // Optional
        let: , // Optional
        whenMatched: , // Optional
        whenNotMatched: // Optional
        } }
        6 chạy trên các nút thứ cấp.
        { $merge: {
        into: -or- { db: , coll: },
        on: -or- [ , ...], // Optional
        let: , // Optional
        whenMatched: , // Optional
        whenNotMatched: // Optional
        } }
        6 operations to replica set secondary nodes. Check your driver documentation to see when your driver added support for
        { $merge: {
        into: -or- { db: , coll: },
        on: -or- [ , ...], // Optional
        let: , // Optional
        whenMatched: , // Optional
        whenNotMatched: // Optional
        } }
        6 read operations running on secondary nodes.
        { $merge: {
        into: -or- { db: , coll: },
        on: -or- [ , ...], // Optional
        let: , // Optional
        whenMatched: , // Optional
        whenNotMatched: // Optional
        } }
        6
        operations to replica set secondary nodes. Check your driver documentation to see when your driver added support for
        { $merge: {
        into: -or- { db: , coll: },
        on: -or- [ , ...], // Optional
        let: , // Optional
        whenMatched: , // Optional
        whenNotMatched: // Optional
        } }
        6
        read operations running on secondary nodes.

  • Tạo một bộ sưu tập mới nếu bộ sưu tập đầu ra chưa tồn tại.

  • Có thể kết hợp kết quả (chèn các tài liệu mới, hợp nhất tài liệu, thay thế tài liệu, giữ các tài liệu hiện có, không thành công hoạt động, xử lý tài liệu bằng đường ống cập nhật tùy chỉnh) vào một bộ sưu tập hiện có.

  • Có thể đầu ra vào một bộ sưu tập Sharded. Bộ sưu tập đầu vào cũng có thể được chia cắt.

Để so sánh với giai đoạn

{ $merge: {
into: -or- { db: , coll: },
on: -or- [ , ...], // Optional
let: , // Optional
whenMatched: , // Optional
whenNotMatched: // Optional
} }
0 cũng đưa ra kết quả tổng hợp vào một bộ sưu tập, xem so sánh
{ $merge: {
into: -or- { db: , coll: },
on: -or- [ , ...], // Optional
let: , // Optional
whenMatched: , // Optional
whenNotMatched: // Optional
} }
6 và
{ $merge: {
into: -or- { db: , coll: },
on: -or- [ , ...], // Optional
let: , // Optional
whenMatched: , // Optional
whenNotMatched: // Optional
} }
0.
{ $merge: {
into: -or- { db: , coll: },
on: -or- [ , ...], // Optional
let: , // Optional
whenMatched: , // Optional
whenNotMatched: // Optional
} }
6 and
{ $merge: {
into: -or- { db: , coll: },
on: -or- [ , ...], // Optional
let: , // Optional
whenMatched: , // Optional
whenNotMatched: // Optional
} }
0 Comparison.
{ $merge: {
into: -or- { db: , coll: },
on: -or- [ , ...], // Optional
let: , // Optional
whenMatched: , // Optional
whenNotMatched: // Optional
} }
6 and
{ $merge: {
into: -or- { db: , coll: },
on: -or- [ , ...], // Optional
let: , // Optional
whenMatched: , // Optional
whenNotMatched: // Optional
} }
0 Comparison.

Ghi chú

Trang này mô tả giai đoạn $merge, đưa ra kết quả đường ống tổng hợp cho một bộ sưu tập. Đối với toán tử $mergeObjects, hợp nhất các tài liệu vào một tài liệu, xem $mergeObjects

Trang này mô tả giai đoạn

{ $merge: {
into: -or- { db: , coll: },
on: -or- [ , ...], // Optional
let: , // Optional
whenMatched: , // Optional
whenNotMatched: // Optional
} }
6, đưa ra kết quả đường ống tổng hợp cho một bộ sưu tập. Đối với toán tử
{ $merge: {
into: -or- { db: , coll: },
on: -or- [ , ...], // Optional
let: , // Optional
whenMatched: , // Optional
whenNotMatched: // Optional
} }
7, hợp nhất các tài liệu vào một tài liệu, xem
{ $merge: {
into: -or- { db: , coll: },
on: -or- [ , ...], // Optional
let: , // Optional
whenMatched: , // Optional
whenNotMatched: // Optional
} }
7
{ $merge: {
into: -or- { db: , coll: },
on: -or- [ , ...], // Optional
let: , // Optional
whenMatched: , // Optional
whenNotMatched: // Optional
} }
6 stage, which outputs the aggregation pipeline results to a collection. For the
{ $merge: {
into: -or- { db: , coll: },
on: -or- [ , ...], // Optional
let: , // Optional
whenMatched: , // Optional
whenNotMatched: // Optional
} }
7 operator, which merges documents into a single document, see
{ $merge: {
into: -or- { db: , coll: },
on: -or- [ , ...], // Optional
let: , // Optional
whenMatched: , // Optional
whenNotMatched: // Optional
} }
7 can incorporate the pipeline results into an existing output collection rather than perform a full replacement of the collection. This functionality allows users to create on-demand materialized views, where the content of the output collection is incrementally updated when the pipeline is run.

Giai đoạn

{ $merge: {
into: -or- { db: , coll: },
on: -or- [ , ...], // Optional
let: , // Optional
whenMatched: , // Optional
whenNotMatched: // Optional
} }
6:

Có thể xuất vào một bộ sưu tập trong cùng một cơ sở dữ liệu hoặc khác nhau.

Bắt đầu từ MongoDB 4.4: has the following syntax: has the following syntax:

{ $merge: {
into: -or- { db: , coll: },
on: -or- [ , ...], // Optional
let: , // Optional
whenMatched: , // Optional
whenNotMatched: // Optional
} }

{ $merge: {
into: -or- { db: , coll: },
on: -or- [ , ...], // Optional
let: , // Optional
whenMatched: , // Optional
whenNotMatched: // Optional
} }
6 có thể đầu ra cho cùng một bộ sưu tập đang được tổng hợp. Để biết thêm thông tin, hãy xem đầu ra cho cùng một bộ sưu tập đang được tổng hợp.

{ $merge: { into: "myOutput", on: "_id", whenMatched: "replace", whenNotMatched: "insert" } }

Các đường ống với giai đoạn

{ $merge: {
into: -or- { db: , coll: },
on: -or- [ , ...], // Optional
let: , // Optional
whenMatched: , // Optional
whenNotMatched: // Optional
} }
6 có thể chạy trên các nút thứ cấp đặt bản sao nếu tất cả các nút trong cụm có tính năng tương tự featureCompitionVersion được đặt thành

db.users.find(
  $or: [
    {name:1,"education.school.name":1,"education.type":1},
    {age:1,"education.school.name":2,"education.type":2} 
  ]
)
6 hoặc cao hơn và sở thích đọc cho phép đọc thứ cấp.
{ $merge: {
into: -or- { db: , coll: },
on: -or- [ , ...], // Optional
let: , // Optional
whenMatched: , // Optional
whenNotMatched: // Optional
} }
6, including writing to a collection in the same database, you can use the simplified form:
db.users.find(
  $or: [
    {name:1,"education.school.name":1,"education.type":1},
    {age:1,"education.school.name":2,"education.type":2} 
  ]
)
0
{ $merge: {
into: -or- { db: , coll: },
on: -or- [ , ...], // Optional
let: , // Optional
whenMatched: , // Optional
whenNotMatched: // Optional
} }
6
, including writing to a collection in the same database, you can use the simplified form:
db.users.find(
  $or: [
    {name:1,"education.school.name":1,"education.type":1},
    {age:1,"education.school.name":2,"education.type":2} 
  ]
)
0

{ $merge: {
into: -or- { db: , coll: },
on: -or- [ , ...], // Optional
let: , // Optional
whenMatched: , // Optional
whenNotMatched: // Optional
} }
6 lấy một tài liệu với các trường sau:
{ $merge: {
into: -or- { db: , coll: },
on: -or- [ , ...], // Optional
let: , // Optional
whenMatched: , // Optional
whenNotMatched: // Optional
} }
6 takes a document with the following fields:
{ $merge: {
into: -or- { db: , coll: },
on: -or- [ , ...], // Optional
let: , // Optional
whenMatched: , // Optional
whenNotMatched: // Optional
} }
6
takes a document with the following fields:

Đồng ruộng

Sự mô tả

vào trong

Bộ sưu tập đầu ra. Chỉ định một trong hai:

  • Tên bộ sưu tập dưới dạng chuỗi để xuất vào một bộ sưu tập trong cùng một cơ sở dữ liệu nơi chạy tập hợp. Ví dụ:

    { $merge: {
    into: -or- { db: , coll: },
    on: -or- [ , ...], // Optional
    let: , // Optional
    whenMatched: , // Optional
    whenNotMatched: // Optional
    } }
    7
  • Cơ sở dữ liệu và tên thu thập trong một tài liệu để xuất vào một bộ sưu tập trong cơ sở dữ liệu được chỉ định. Ví dụ:

    { $merge: {
    into: -or- { db: , coll: },
    on: -or- [ , ...], // Optional
    let: , // Optional
    whenMatched: , // Optional
    whenNotMatched: // Optional
    } }
    8

Ghi chú

  • Nếu bộ sưu tập đầu ra không tồn tại,

    { $merge: {
    into: -or- { db: , coll: },
    on: -or- [ , ...], // Optional
    let: , // Optional
    whenMatched: , // Optional
    whenNotMatched: // Optional
    } }
    6 sẽ tạo ra bộ sưu tập:
    { $merge: {
    into: -or- { db: , coll: },
    on: -or- [ , ...], // Optional
    let: , // Optional
    whenMatched: , // Optional
    whenNotMatched: // Optional
    } }
    6 creates the collection:
    { $merge: {
    into: -or- { db: , coll: },
    on: -or- [ , ...], // Optional
    let: , // Optional
    whenMatched: , // Optional
    whenNotMatched: // Optional
    } }
    6
    creates the collection:

    • Đối với một bộ bản sao hoặc độc lập, nếu cơ sở dữ liệu đầu ra không tồn tại,

      { $merge: {
      into: -or- { db: , coll: },
      on: -or- [ , ...], // Optional
      let: , // Optional
      whenMatched: , // Optional
      whenNotMatched: // Optional
      } }
      6 cũng tạo cơ sở dữ liệu.
      { $merge: {
      into: -or- { db: , coll: },
      on: -or- [ , ...], // Optional
      let: , // Optional
      whenMatched: , // Optional
      whenNotMatched: // Optional
      } }
      6 also creates the database.
      { $merge: {
      into: -or- { db: , coll: },
      on: -or- [ , ...], // Optional
      let: , // Optional
      whenMatched: , // Optional
      whenNotMatched: // Optional
      } }
      6
      also creates the database.

    • Đối với một cụm mảnh vỡ, cơ sở dữ liệu đầu ra được chỉ định phải tồn tại.

  • Bộ sưu tập đầu ra có thể là một bộ sưu tập Sharded.

trên

Không bắt buộc. Trường hoặc các trường hoạt động như một định danh duy nhất cho một tài liệu. Mã định danh xác định xem tài liệu kết quả có khớp với một tài liệu hiện có trong bộ sưu tập đầu ra hay không. Chỉ định một trong hai:matches an existing document in the output collection. Specify either:matches an existing document in the output collection. Specify either:

  • Một tên trường duy nhất dưới dạng chuỗi. Ví dụ:

    { $merge: { into: "myOutput", on: "_id", whenMatched: "replace", whenNotMatched: "insert" } }

    1
  • Một sự kết hợp của các trường trong một mảng. Ví dụ:

    { $merge: { into: "myOutput", on: "_id", whenMatched: "replace", whenNotMatched: "insert" } }

    2

    Thứ tự của các trường trong mảng không quan trọng và bạn không thể chỉ định cùng một trường nhiều lần.

Đối với trường hoặc trường được chỉ định:

  • Các tài liệu kết quả tổng hợp phải chứa (các) trường được chỉ định trong

    { $merge: { into: "myOutput", on: "_id", whenMatched: "replace", whenNotMatched: "insert" } }

    3, trừ khi trường

    { $merge: { into: "myOutput", on: "_id", whenMatched: "replace", whenNotMatched: "insert" } }

    3 là trường

    { $merge: { into: "myOutput", on: "_id", whenMatched: "replace", whenNotMatched: "insert" } }

    5. Nếu trường

    { $merge: { into: "myOutput", on: "_id", whenMatched: "replace", whenNotMatched: "insert" } }

    5 bị thiếu trong tài liệu kết quả, MongoDB sẽ tự động thêm nó.
  • Trường hoặc trường được chỉ định không thể chứa giá trị null hoặc mảng.

{ $merge: {
into: -or- { db: , coll: },
on: -or- [ , ...], // Optional
let: , // Optional
whenMatched: , // Optional
whenNotMatched: // Optional
} }
6 yêu cầu một chỉ mục duy nhất với các khóa tương ứng với các trường Định danh trên. Mặc dù thứ tự của đặc tả khóa chỉ mục không quan trọng, nhưng chỉ mục duy nhất chỉ phải chứa các trường

{ $merge: { into: "myOutput", on: "_id", whenMatched: "replace", whenNotMatched: "insert" } }

3 làm khóa của nó. requires a unique, index with keys that correspond to the on identifier fields. Although the order of the index key specification does not matter, the unique index must only contain the on identifier fields. Although the order of the index key specification does not matter, the unique index must only contain the

{ $merge: { into: "myOutput", on: "_id", whenMatched: "replace", whenNotMatched: "insert" } }

3 fields as its keys.
  • Chỉ số cũng phải có sự đối chiếu giống như đối chiếu của tổng hợp.

  • Chỉ số duy nhất có thể là một chỉ số thưa thớt.

  • Chỉ số duy nhất không thể là một chỉ số một phần.

  • Đối với các bộ sưu tập đầu ra đã tồn tại, chỉ mục tương ứng phải tồn tại.

Giá trị mặc định cho trên phụ thuộc vào bộ sưu tập đầu ra:on depends on the output collection:on depends on the output collection:

  • Nếu bộ sưu tập đầu ra không tồn tại, định danh BẬT phải và mặc định cho trường

    { $merge: { into: "myOutput", on: "_id", whenMatched: "replace", whenNotMatched: "insert" } }

    5. Chỉ số

    { $merge: { into: "myOutput", on: "_id", whenMatched: "replace", whenNotMatched: "insert" } }

    5 duy nhất tương ứng được tự động tạo.on identifier must be and defaults to the

    { $merge: { into: "myOutput", on: "_id", whenMatched: "replace", whenNotMatched: "insert" } }

    5 field. The corresponding unique

    { $merge: { into: "myOutput", on: "_id", whenMatched: "replace", whenNotMatched: "insert" } }

    5 index is automatically created.on identifier must be and defaults to the

    { $merge: { into: "myOutput", on: "_id", whenMatched: "replace", whenNotMatched: "insert" } }

    5 field. The corresponding unique

    { $merge: { into: "myOutput", on: "_id", whenMatched: "replace", whenNotMatched: "insert" } }

    5 index is automatically created.

    Mẹo

Để sử dụng (các) trường định danh khác cho một bộ sưu tập không tồn tại, bạn có thể tạo bộ sưu tập trước bằng cách tạo một chỉ mục duy nhất trên (các) trường mong muốn. Xem phần về bộ sưu tập đầu ra không tồn tại để biết ví dụ.on identifier field(s) for a collection that does not exist, you can create the collection first by creating a unique index on the desired field(s). See the section on non-existent output collection for an example.on identifier field(s) for a collection that does not exist, you can create the collection first by creating a unique index on the desired field(s). See the section on non-existent output collection for an example.

  • Nếu bộ sưu tập đầu ra hiện có không được đặt, định danh

    { $merge: { into: "myOutput", on: "_id", whenMatched: "replace", whenNotMatched: "insert" } }

    3 mặc định cho trường

    { $merge: { into: "myOutput", on: "_id", whenMatched: "replace", whenNotMatched: "insert" } }

    5.
  • Nếu bộ sưu tập đầu ra hiện có là một bộ sưu tập Sharded, định danh trên mặc định cho tất cả các trường khóa Shard và trường

    { $merge: { into: "myOutput", on: "_id", whenMatched: "replace", whenNotMatched: "insert" } }

    5. Nếu chỉ định một định danh

    { $merge: { into: "myOutput", on: "_id", whenMatched: "replace", whenNotMatched: "insert" } }

    3 khác nhau,

    { $merge: { into: "myOutput", on: "_id", whenMatched: "replace", whenNotMatched: "insert" } }

    3 phải chứa tất cả các trường khóa Shard.on identifier defaults to all the shard key fields and the

    { $merge: { into: "myOutput", on: "_id", whenMatched: "replace", whenNotMatched: "insert" } }

    5 field. If specifying a different

    { $merge: { into: "myOutput", on: "_id", whenMatched: "replace", whenNotMatched: "insert" } }

    3 identifier, the

    { $merge: { into: "myOutput", on: "_id", whenMatched: "replace", whenNotMatched: "insert" } }

    3 must contain all the shard key fields.on identifier defaults to all the shard key fields and the

    { $merge: { into: "myOutput", on: "_id", whenMatched: "replace", whenNotMatched: "insert" } }

    5 field. If specifying a different

    { $merge: { into: "myOutput", on: "_id", whenMatched: "replace", whenNotMatched: "insert" } }

    3 identifier, the

    { $merge: { into: "myOutput", on: "_id", whenMatched: "replace", whenNotMatched: "insert" } }

    3 must contain all the shard key fields.
  • khi nào

    Không bắt buộc. Hành vi của

    { $merge: {
    into: -or- { db: , coll: },
    on: -or- [ , ...], // Optional
    let: , // Optional
    whenMatched: , // Optional
    whenNotMatched: // Optional
    } }
    6 nếu một tài liệu kết quả và một tài liệu hiện có trong bộ sưu tập có cùng giá trị cho (các) trường được chỉ định.
    { $merge: {
    into: -or- { db: , coll: },
    on: -or- [ , ...], // Optional
    let: , // Optional
    whenMatched: , // Optional
    whenNotMatched: // Optional
    } }
    6 if a result document and an existing document in the collection have the same value for the specified on field(s).
    { $merge: {
    into: -or- { db: , coll: },
    on: -or- [ , ...], // Optional
    let: , // Optional
    whenMatched: , // Optional
    whenNotMatched: // Optional
    } }
    6
    if a result document and an existing document in the collection have the same value for the specified on field(s).

    Bạn có thể chỉ định:

    • Một trong những chuỗi hành động được xác định trước:

      Hoạt động

      Sự mô tả

      "replace"

      Thay thế tài liệu hiện có trong bộ sưu tập đầu ra bằng tài liệu kết quả phù hợp.output collection with the matching results document.output collection with the matching results document.

      Khi thực hiện thay thế, tài liệu thay thế không thể dẫn đến việc sửa đổi giá trị

      { $merge: { into: "myOutput", on: "_id", whenMatched: "replace", whenNotMatched: "insert" } }

      5 hoặc, nếu bộ sưu tập đầu ra bị chia cắt, giá trị khóa của Shard. Nếu không, thao tác tạo ra một lỗi.

      Mẹo

      Để tránh lỗi này, nếu trường trên không bao gồm trường

      { $merge: { into: "myOutput", on: "_id", whenMatched: "replace", whenNotMatched: "insert" } }

      5, hãy xóa trường

      { $merge: { into: "myOutput", on: "_id", whenMatched: "replace", whenNotMatched: "insert" } }

      5 trong kết quả tổng hợp để tránh lỗi, chẳng hạn như với giai đoạn
      { $merge: {
      into: -or- { db: , coll: },
      on: -or- [ , ...], // Optional
      let: , // Optional
      whenMatched: , // Optional
      whenNotMatched: // Optional
      } }
      30 trước đó, v.v.on field does not include the

      { $merge: { into: "myOutput", on: "_id", whenMatched: "replace", whenNotMatched: "insert" } }

      5 field, remove the

      { $merge: { into: "myOutput", on: "_id", whenMatched: "replace", whenNotMatched: "insert" } }

      5 field in the aggregation results to avoid the error, such as with a preceding
      { $merge: {
      into: -or- { db: , coll: },
      on: -or- [ , ...], // Optional
      let: , // Optional
      whenMatched: , // Optional
      whenNotMatched: // Optional
      } }
      30 stage, and so on. on field does not include the

      { $merge: { into: "myOutput", on: "_id", whenMatched: "replace", whenNotMatched: "insert" } }

      5 field, remove the

      { $merge: { into: "myOutput", on: "_id", whenMatched: "replace", whenNotMatched: "insert" } }

      5 field in the aggregation results to avoid the error, such as with a preceding
      { $merge: {
      into: -or- { db: , coll: },
      on: -or- [ , ...], // Optional
      let: , // Optional
      whenMatched: , // Optional
      whenNotMatched: // Optional
      } }
      30 stage, and so on.

    "keepExisting"

    Giữ tài liệu hiện có trong bộ sưu tập đầu ra.output collection.output collection.

    "Hợp nhất" (mặc định) (Default) (Default)

    Hợp nhất các tài liệu phù hợp (tương tự như toán tử

    { $merge: {
    into: -or- { db: , coll: },
    on: -or- [ , ...], // Optional
    let: , // Optional
    whenMatched: , // Optional
    whenNotMatched: // Optional
    } }
    7).

    • Nếu tài liệu kết quả chứa các trường không có trong tài liệu hiện có, hãy thêm các trường mới này vào tài liệu hiện có.

    • Nếu tài liệu kết quả chứa các trường trong tài liệu hiện có, hãy thay thế các giá trị trường hiện có bằng các giá trị từ tài liệu kết quả.

    Ví dụ: nếu bộ sưu tập đầu ra có tài liệu:

    { $merge: {
    into: -or- { db: , coll: },
    on: -or- [ , ...], // Optional
    let: , // Optional
    whenMatched: , // Optional
    whenNotMatched: // Optional
    } }
    32

    Và kết quả tổng hợp có tài liệu:

    { $merge: {
    into: -or- { db: , coll: },
    on: -or- [ , ...], // Optional
    let: , // Optional
    whenMatched: , // Optional
    whenNotMatched: // Optional
    } }
    33

    Sau đó, tài liệu được hợp nhất là:

    { $merge: {
    into: -or- { db: , coll: },
    on: -or- [ , ...], // Optional
    let: , // Optional
    whenMatched: , // Optional
    whenNotMatched: // Optional
    } }
    34

    Khi thực hiện hợp nhất, tài liệu được hợp nhất không thể dẫn đến việc sửa đổi giá trị

    { $merge: { into: "myOutput", on: "_id", whenMatched: "replace", whenNotMatched: "insert" } }

    5 hoặc, nếu bộ sưu tập đầu ra bị chia cắt, giá trị khóa của Shard. Nếu không, thao tác tạo ra một lỗi.

    Mẹo

    Để tránh lỗi này, nếu trường trên không bao gồm trường

    { $merge: { into: "myOutput", on: "_id", whenMatched: "replace", whenNotMatched: "insert" } }

    5, hãy xóa trường

    { $merge: { into: "myOutput", on: "_id", whenMatched: "replace", whenNotMatched: "insert" } }

    5 trong kết quả tổng hợp để tránh lỗi, chẳng hạn như với giai đoạn
    { $merge: {
    into: -or- { db: , coll: },
    on: -or- [ , ...], // Optional
    let: , // Optional
    whenMatched: , // Optional
    whenNotMatched: // Optional
    } }
    30 trước đó, v.v.on field does not include the

    { $merge: { into: "myOutput", on: "_id", whenMatched: "replace", whenNotMatched: "insert" } }

    5 field, remove the

    { $merge: { into: "myOutput", on: "_id", whenMatched: "replace", whenNotMatched: "insert" } }

    5 field in the aggregation results to avoid the error, such as with a preceding
    { $merge: {
    into: -or- { db: , coll: },
    on: -or- [ , ...], // Optional
    let: , // Optional
    whenMatched: , // Optional
    whenNotMatched: // Optional
    } }
    30 stage, and so on. on field does not include the

    { $merge: { into: "myOutput", on: "_id", whenMatched: "replace", whenNotMatched: "insert" } }

    5 field, remove the

    { $merge: { into: "myOutput", on: "_id", whenMatched: "replace", whenNotMatched: "insert" } }

    5 field in the aggregation results to avoid the error, such as with a preceding
    { $merge: {
    into: -or- { db: , coll: },
    on: -or- [ , ...], // Optional
    let: , // Optional
    whenMatched: , // Optional
    whenNotMatched: // Optional
    } }
    30 stage, and so on.

    Giữ tài liệu hiện có trong bộ sưu tập đầu ra.output collection.

    "Hợp nhất" (mặc định) (Default)you can perform a left outer join by using the $lookup stage. The $lookup stage lets you specify which collection you want to join with the current collection, and which fields that should match.

    Hợp nhất các tài liệu phù hợp (tương tự như toán tử { $merge: { into: -or- { db: , coll: }, on: -or- [ , ...], // Optional let: , // Optional whenMatched: , // Optional whenNotMatched: // Optional} }7).

    Nếu tài liệu kết quả chứa các trường không có trong tài liệu hiện có, hãy thêm các trường mới này vào tài liệu hiện có.

    Nếu tài liệu kết quả chứa các trường trong tài liệu hiện có, hãy thay thế các giá trị trường hiện có bằng các giá trị từ tài liệu kết quả.

    Ví dụ: nếu bộ sưu tập đầu ra có tài liệu:a way of processing a large number of documents in a collection by means of passing them through different stages. The stages make up what is known as a pipeline. The stages in a pipeline can filter, sort, group, reshape and modify documents that pass through the pipeline.

    Và kết quả tổng hợp có tài liệu:

    Sau đó, tài liệu được hợp nhất là:use the aggregation operations that are provided by MongoDB itself.