Hướng dẫn mongodb query time range - phạm vi thời gian truy vấn mongodb

Tài liệu về nhà → MongoDB AtlasMongoDB Atlas

Hướng dẫn này mô tả cách tạo một chỉ mục với ánh xạ động trên bộ sưu tập sample_mflix.movies. Nó cho thấy cách chạy các truy vấn ghép đối với trường released bằng cách sử dụng phạm vi và các toán tử gần. Nó đưa bạn qua các bước sau:

  1. Thiết lập một chỉ mục tìm kiếm ATLAS với ánh xạ động cho bộ sưu tập sample_mflix.movies.

  2. Chạy các truy vấn tổng hợp tìm kiếm Atlas đối với trường released trong bộ sưu tập sample_mflix.movies.

Trước khi bạn bắt đầu, hãy đảm bảo rằng cụm Atlas của bạn đáp ứng các yêu cầu được mô tả trong các điều kiện tiên quyết.

Trong phần này, chúng tôi tạo một chỉ mục tìm kiếm ATLAS sử dụng ánh xạ động để lập chỉ mục các trường trong bộ sưu tập sample_mflix.movies.

  1. Nếu nó chưa được hiển thị, hãy chọn tổ chức chứa dự án mong muốn của bạn từ menu tổ chức trong thanh điều hướng.Organizations menu in the navigation bar.

  2. Nếu nó chưa được hiển thị, hãy chọn dự án mong muốn của bạn từ menu Dự án trong thanh điều hướng.Projects menu in the navigation bar.

  3. Nhấp vào tên cụm của bạn.

  4. Nhấp vào tab Tìm kiếm.Search tab.

  • Để có trải nghiệm có hướng dẫn, chọn Trình chỉnh sửa trực quan.Visual Editor.

  • Để chỉnh sửa định nghĩa chỉ số thô, chọn Trình chỉnh sửa JSON.JSON Editor.

  1. Trong trường Tên chỉ mục, nhập

    db.movies.aggregate([
    {
    "$search": {
    "compound": {
    "must": [{
    "range": {
    "path": "released",
    "gt": ISODate("2015-01-01T00:00:00.000Z"),
    "lt": ISODate("2015-12-31T00:00:00.000Z")
    }
    }],
    "should": [{
    "near": {
    "path": "released",
    "origin": ISODate("2015-07-01T00:00:00.000+00:00"),
    "pivot": 2629800000
    }
    }]
    }
    }
    },
    {
    "$limit": 6
    },
    {
    "$project": {
    "_id": 0,
    "title": 1,
    "released": 1,
    "genres": 1,
    "score": { "$meta": "searchScore" }
    }
    }
    ])
    1.Index Name field, enter
    db.movies.aggregate([
    {
    "$search": {
    "compound": {
    "must": [{
    "range": {
    "path": "released",
    "gt": ISODate("2015-01-01T00:00:00.000Z"),
    "lt": ISODate("2015-12-31T00:00:00.000Z")
    }
    }],
    "should": [{
    "near": {
    "path": "released",
    "origin": ISODate("2015-07-01T00:00:00.000+00:00"),
    "pivot": 2629800000
    }
    }]
    }
    }
    },
    {
    "$limit": 6
    },
    {
    "$project": {
    "_id": 0,
    "title": 1,
    "released": 1,
    "genres": 1,
    "score": { "$meta": "searchScore" }
    }
    }
    ])
    1.

    Ghi chú

    Nếu bạn đặt tên cho chỉ mục của mình

    db.movies.aggregate([
    {
    "$search": {
    "compound": {
    "must": [{
    "range": {
    "path": "released",
    "gt": ISODate("2015-01-01T00:00:00.000Z"),
    "lt": ISODate("2015-12-31T00:00:00.000Z")
    }
    }],
    "should": [{
    "near": {
    "path": "released",
    "origin": ISODate("2015-07-01T00:00:00.000+00:00"),
    "pivot": 2629800000
    }
    }]
    }
    }
    },
    {
    "$limit": 6
    },
    {
    "$project": {
    "_id": 0,
    "title": 1,
    "released": 1,
    "genres": 1,
    "score": { "$meta": "searchScore" }
    }
    }
    ])
    1, bạn không cần chỉ định tham số
    db.movies.aggregate([
    {
    "$search": {
    "compound": {
    "must": [{
    "range": {
    "path": "released",
    "gt": ISODate("2015-01-01T00:00:00.000Z"),
    "lt": ISODate("2015-12-31T00:00:00.000Z")
    }
    }],
    "should": [{
    "near": {
    "path": "released",
    "origin": ISODate("2015-07-01T00:00:00.000+00:00"),
    "pivot": 2629800000
    }
    }]
    }
    }
    },
    {
    "$limit": 6
    },
    {
    "$project": {
    "_id": 0,
    "title": 1,
    "released": 1,
    "genres": 1,
    "score": { "$meta": "searchScore" }
    }
    }
    ])
    3 khi sử dụng giai đoạn đường ống tìm kiếm $. Mặt khác, bạn phải chỉ định tên chỉ mục bằng tham số
    db.movies.aggregate([
    {
    "$search": {
    "compound": {
    "must": [{
    "range": {
    "path": "released",
    "gt": ISODate("2015-01-01T00:00:00.000Z"),
    "lt": ISODate("2015-12-31T00:00:00.000Z")
    }
    }],
    "should": [{
    "near": {
    "path": "released",
    "origin": ISODate("2015-07-01T00:00:00.000+00:00"),
    "pivot": 2629800000
    }
    }]
    }
    }
    },
    {
    "$limit": 6
    },
    {
    "$project": {
    "_id": 0,
    "title": 1,
    "released": 1,
    "genres": 1,
    "score": { "$meta": "searchScore" }
    }
    }
    ])
    3.

  2. Trong phần cơ sở dữ liệu và bộ sưu tập, tìm cơ sở dữ liệu

    db.movies.aggregate([
    {
    "$search": {
    "compound": {
    "must": [{
    "range": {
    "path": "released",
    "gt": ISODate("2015-01-01T00:00:00.000Z"),
    "lt": ISODate("2015-12-31T00:00:00.000Z")
    }
    }],
    "should": [{
    "near": {
    "path": "released",
    "origin": ISODate("2015-07-01T00:00:00.000+00:00"),
    "pivot": 2629800000
    }
    }]
    }
    }
    },
    {
    "$limit": 6
    },
    {
    "$project": {
    "_id": 0,
    "title": 1,
    "released": 1,
    "genres": 1,
    "score": { "$meta": "searchScore" }
    }
    }
    ])
    5 và chọn bộ sưu tập
    db.movies.aggregate([
    {
    "$search": {
    "compound": {
    "must": [{
    "range": {
    "path": "released",
    "gt": ISODate("2015-01-01T00:00:00.000Z"),
    "lt": ISODate("2015-12-31T00:00:00.000Z")
    }
    }],
    "should": [{
    "near": {
    "path": "released",
    "origin": ISODate("2015-07-01T00:00:00.000+00:00"),
    "pivot": 2629800000
    }
    }]
    }
    }
    },
    {
    "$limit": 6
    },
    {
    "$project": {
    "_id": 0,
    "title": 1,
    "released": 1,
    "genres": 1,
    "score": { "$meta": "searchScore" }
    }
    }
    ])
    6.Database and Collection section, find the
    db.movies.aggregate([
    {
    "$search": {
    "compound": {
    "must": [{
    "range": {
    "path": "released",
    "gt": ISODate("2015-01-01T00:00:00.000Z"),
    "lt": ISODate("2015-12-31T00:00:00.000Z")
    }
    }],
    "should": [{
    "near": {
    "path": "released",
    "origin": ISODate("2015-07-01T00:00:00.000+00:00"),
    "pivot": 2629800000
    }
    }]
    }
    }
    },
    {
    "$limit": 6
    },
    {
    "$project": {
    "_id": 0,
    "title": 1,
    "released": 1,
    "genres": 1,
    "score": { "$meta": "searchScore" }
    }
    }
    ])
    5 database, and select the
    db.movies.aggregate([
    {
    "$search": {
    "compound": {
    "must": [{
    "range": {
    "path": "released",
    "gt": ISODate("2015-01-01T00:00:00.000Z"),
    "lt": ISODate("2015-12-31T00:00:00.000Z")
    }
    }],
    "should": [{
    "near": {
    "path": "released",
    "origin": ISODate("2015-07-01T00:00:00.000+00:00"),
    "pivot": 2629800000
    }
    }]
    }
    }
    },
    {
    "$limit": 6
    },
    {
    "$project": {
    "_id": 0,
    "title": 1,
    "released": 1,
    "genres": 1,
    "score": { "$meta": "searchScore" }
    }
    }
    ])
    6 collection.

Bạn có thể tạo một chỉ mục tìm kiếm ATLAS sử dụng ánh xạ động thông qua trình chỉnh sửa trực quan hoặc trình chỉnh sửa JSON trong giao diện người dùng Atlas. Định nghĩa chỉ số sau đây chỉ số các trường của các loại được hỗ trợ trong bộ sưu tập

db.movies.aggregate([
{
"$search": {
"compound": {
"must": [{
"range": {
"path": "released",
"gt": ISODate("2015-01-01T00:00:00.000Z"),
"lt": ISODate("2015-12-31T00:00:00.000Z")
}
}],
"should": [{
"near": {
"path": "released",
"origin": ISODate("2015-07-01T00:00:00.000+00:00"),
"pivot": 2629800000
}
}]
}
}
},
{
"$limit": 6
},
{
"$project": {
"_id": 0,
"title": 1,
"released": 1,
"genres": 1,
"score": { "$meta": "searchScore" }
}
}
])
6.Visual Editor or JSON Editor in the Atlas User Interface. The following index definition dynamically indexes the fields of supported types in the
db.movies.aggregate([
{
"$search": {
"compound": {
"must": [{
"range": {
"path": "released",
"gt": ISODate("2015-01-01T00:00:00.000Z"),
"lt": ISODate("2015-12-31T00:00:00.000Z")
}
}],
"should": [{
"near": {
"path": "released",
"origin": ISODate("2015-07-01T00:00:00.000+00:00"),
"pivot": 2629800000
}
}]
}
}
},
{
"$limit": 6
},
{
"$project": {
"_id": 0,
"title": 1,
"released": 1,
"genres": 1,
"score": { "$meta": "searchScore" }
}
}
])
6 collection.

  1. Nhấn tiếp.Next.

  2. Xem lại định nghĩa chỉ số

    db.movies.aggregate([
    {
    "$search": {
    "compound": {
    "must": [{
    "range": {
    "path": "released",
    "gt": ISODate("2015-01-01T00:00:00.000Z"),
    "lt": ISODate("2015-12-31T00:00:00.000Z")
    }
    }],
    "should": [{
    "near": {
    "path": "released",
    "origin": ISODate("2015-07-01T00:00:00.000+00:00"),
    "pivot": 2629800000
    }
    }]
    }
    }
    },
    {
    "$limit": 6
    },
    {
    "$project": {
    "_id": 0,
    "title": 1,
    "released": 1,
    "genres": 1,
    "score": { "$meta": "searchScore" }
    }
    }
    ])
    8 cho bộ sưu tập
    db.movies.aggregate([
    {
    "$search": {
    "compound": {
    "must": [{
    "range": {
    "path": "released",
    "gt": ISODate("2015-01-01T00:00:00.000Z"),
    "lt": ISODate("2015-12-31T00:00:00.000Z")
    }
    }],
    "should": [{
    "near": {
    "path": "released",
    "origin": ISODate("2015-07-01T00:00:00.000+00:00"),
    "pivot": 2629800000
    }
    }]
    }
    }
    },
    {
    "$limit": 6
    },
    {
    "$project": {
    "_id": 0,
    "title": 1,
    "released": 1,
    "genres": 1,
    "score": { "$meta": "searchScore" }
    }
    }
    ])
    6.

  1. Nhấn tiếp.Next.

  2. Xem lại định nghĩa chỉ số

    db.movies.aggregate([
    {
    "$search": {
    "compound": {
    "must": [{
    "range": {
    "path": "released",
    "gt": ISODate("2015-01-01T00:00:00.000Z"),
    "lt": ISODate("2015-12-31T00:00:00.000Z")
    }
    }],
    "should": [{
    "near": {
    "path": "released",
    "origin": ISODate("2015-07-01T00:00:00.000+00:00"),
    "pivot": 2629800000
    }
    }]
    }
    }
    },
    {
    "$limit": 6
    },
    {
    "$project": {
    "_id": 0,
    "title": 1,
    "released": 1,
    "genres": 1,
    "score": { "$meta": "searchScore" }
    }
    }
    ])
    8 cho bộ sưu tập
    db.movies.aggregate([
    {
    "$search": {
    "compound": {
    "must": [{
    "range": {
    "path": "released",
    "gt": ISODate("2015-01-01T00:00:00.000Z"),
    "lt": ISODate("2015-12-31T00:00:00.000Z")
    }
    }],
    "should": [{
    "near": {
    "path": "released",
    "origin": ISODate("2015-07-01T00:00:00.000+00:00"),
    "pivot": 2629800000
    }
    }]
    }
    }
    },
    {
    "$limit": 6
    },
    {
    "$project": {
    "_id": 0,
    "title": 1,
    "released": 1,
    "genres": 1,
    "score": { "$meta": "searchScore" }
    }
    }
    ])
    6.

    Xem lại định nghĩa chỉ số.

    {
    "mappings": {
    "dynamic": true
    }
    }

    Định nghĩa chỉ số của bạn sẽ trông tương tự như sau:

  3. Nhấn tiếp.Next.

Xem lại định nghĩa chỉ số

db.movies.aggregate([
{
"$search": {
"compound": {
"must": [{
"range": {
"path": "released",
"gt": ISODate("2015-01-01T00:00:00.000Z"),
"lt": ISODate("2015-12-31T00:00:00.000Z")
}
}],
"should": [{
"near": {
"path": "released",
"origin": ISODate("2015-07-01T00:00:00.000+00:00"),
"pivot": 2629800000
}
}]
}
}
},
{
"$limit": 6
},
{
"$project": {
"_id": 0,
"title": 1,
"released": 1,
"genres": 1,
"score": { "$meta": "searchScore" }
}
}
])
8 cho bộ sưu tập
db.movies.aggregate([
{
"$search": {
"compound": {
"must": [{
"range": {
"path": "released",
"gt": ISODate("2015-01-01T00:00:00.000Z"),
"lt": ISODate("2015-12-31T00:00:00.000Z")
}
}],
"should": [{
"near": {
"path": "released",
"origin": ISODate("2015-07-01T00:00:00.000+00:00"),
"pivot": 2629800000
}
}]
}
}
},
{
"$limit": 6
},
{
"$project": {
"_id": 0,
"title": 1,
"released": 1,
"genres": 1,
"score": { "$meta": "searchScore" }
}
}
])
6.Close button.

Xem lại định nghĩa chỉ số.Status column reads

[
{
"genres": [ "Action", "Adventure", "Sci-Fi" ],
"title": "Terminator Genisys",
"released": ISODate("2015-07-01T00:00:00.000Z"),
"score": 2
},
{
"genres": [ "Comedy", "Drama", "Music" ],
"title": "Magic Mike XXL",
"released": ISODate("2015-07-01T00:00:00.000Z"),
"score": 2
},
{
"genres": [ "Documentary", "Biography", "Drama" ],
"title": "Mala Mala",
"released": ISODate("2015-07-01T00:00:00.000Z"),
"score": 2
},
{
"genres": [ "Comedy", "Drama" ],
"title": "Home Care",
"released": ISODate("2015-07-01T00:00:00.000Z"),
"score": 2
},
{
"genres": [ "Documentary", "News" ],
"title": "Bitcoin: The End of Money as We Know It",
"released": ISODate("2015-07-01T00:00:00.000Z"),
"score": 2
},
{
"genres": [ "Drama", "Mystery", "Sci-Fi" ],
"title": "Pig",
"released": ISODate("2015-07-02T00:00:00.000Z"),
"score": 1.9681909084320068
}
]
1. When it is finished building, the Status column reads
[
{
"genres": [ "Action", "Adventure", "Sci-Fi" ],
"title": "Terminator Genisys",
"released": ISODate("2015-07-01T00:00:00.000Z"),
"score": 2
},
{
"genres": [ "Comedy", "Drama", "Music" ],
"title": "Magic Mike XXL",
"released": ISODate("2015-07-01T00:00:00.000Z"),
"score": 2
},
{
"genres": [ "Documentary", "Biography", "Drama" ],
"title": "Mala Mala",
"released": ISODate("2015-07-01T00:00:00.000Z"),
"score": 2
},
{
"genres": [ "Comedy", "Drama" ],
"title": "Home Care",
"released": ISODate("2015-07-01T00:00:00.000Z"),
"score": 2
},
{
"genres": [ "Documentary", "News" ],
"title": "Bitcoin: The End of Money as We Know It",
"released": ISODate("2015-07-01T00:00:00.000Z"),
"score": 2
},
{
"genres": [ "Drama", "Mystery", "Sci-Fi" ],
"title": "Pig",
"released": ISODate("2015-07-02T00:00:00.000Z"),
"score": 1.9681909084320068
}
]
2.

Định nghĩa chỉ số của bạn sẽ trông tương tự như sau:

Định nghĩa chỉ số trên chỉ số các trường của các loại được hỗ trợ trong mỗi tài liệu trong bộ sưu tập

db.movies.aggregate([
{
"$search": {
"compound": {
"must": [{
"range": {
"path": "released",
"gt": ISODate("2015-01-01T00:00:00.000Z"),
"lt": ISODate("2015-12-31T00:00:00.000Z")
}
}],
"should": [{
"near": {
"path": "released",
"origin": ISODate("2015-07-01T00:00:00.000+00:00"),
"pivot": 2629800000
}
}]
}
}
},
{
"$limit": 6
},
{
"$project": {
"_id": 0,
"title": 1,
"released": 1,
"genres": 1,
"score": { "$meta": "searchScore" }
}
}
])
6.
[
{
"genres": [ "Action", "Adventure", "Sci-Fi" ],
"title": "Terminator Genisys",
"released": ISODate("2015-07-01T00:00:00.000Z"),
"score": 2
},
{
"genres": [ "Comedy", "Drama", "Music" ],
"title": "Magic Mike XXL",
"released": ISODate("2015-07-01T00:00:00.000Z"),
"score": 2
},
{
"genres": [ "Documentary", "Biography", "Drama" ],
"title": "Mala Mala",
"released": ISODate("2015-07-01T00:00:00.000Z"),
"score": 2
},
{
"genres": [ "Comedy", "Drama" ],
"title": "Home Care",
"released": ISODate("2015-07-01T00:00:00.000Z"),
"score": 2
},
{
"genres": [ "Documentary", "News" ],
"title": "Bitcoin: The End of Money as We Know It",
"released": ISODate("2015-07-01T00:00:00.000Z"),
"score": 2
},
{
"genres": [ "Drama", "Mystery", "Sci-Fi" ],
"title": "Pig",
"released": ISODate("2015-07-02T00:00:00.000Z"),
"score": 1.9681909084320068
}
]
5
prompt:

Một cửa sổ phương thức dường như cho bạn biết chỉ mục của bạn đang xây dựng. Nhấp vào nút Đóng.

  • Chỉ số sẽ mất khoảng một phút để xây dựng. Trong khi nó đang xây dựng, cột trạng thái đọc

    [
    {
    "genres": [ "Action", "Adventure", "Sci-Fi" ],
    "title": "Terminator Genisys",
    "released": ISODate("2015-07-01T00:00:00.000Z"),
    "score": 2
    },
    {
    "genres": [ "Comedy", "Drama", "Music" ],
    "title": "Magic Mike XXL",
    "released": ISODate("2015-07-01T00:00:00.000Z"),
    "score": 2
    },
    {
    "genres": [ "Documentary", "Biography", "Drama" ],
    "title": "Mala Mala",
    "released": ISODate("2015-07-01T00:00:00.000Z"),
    "score": 2
    },
    {
    "genres": [ "Comedy", "Drama" ],
    "title": "Home Care",
    "released": ISODate("2015-07-01T00:00:00.000Z"),
    "score": 2
    },
    {
    "genres": [ "Documentary", "News" ],
    "title": "Bitcoin: The End of Money as We Know It",
    "released": ISODate("2015-07-01T00:00:00.000Z"),
    "score": 2
    },
    {
    "genres": [ "Drama", "Mystery", "Sci-Fi" ],
    "title": "Pig",
    "released": ISODate("2015-07-02T00:00:00.000Z"),
    "score": 1.9681909084320068
    }
    ]
    1. Khi nó được xây dựng xong, cột trạng thái đọc
    [
    {
    "genres": [ "Action", "Adventure", "Sci-Fi" ],
    "title": "Terminator Genisys",
    "released": ISODate("2015-07-01T00:00:00.000Z"),
    "score": 2
    },
    {
    "genres": [ "Comedy", "Drama", "Music" ],
    "title": "Magic Mike XXL",
    "released": ISODate("2015-07-01T00:00:00.000Z"),
    "score": 2
    },
    {
    "genres": [ "Documentary", "Biography", "Drama" ],
    "title": "Mala Mala",
    "released": ISODate("2015-07-01T00:00:00.000Z"),
    "score": 2
    },
    {
    "genres": [ "Comedy", "Drama" ],
    "title": "Home Care",
    "released": ISODate("2015-07-01T00:00:00.000Z"),
    "score": 2
    },
    {
    "genres": [ "Documentary", "News" ],
    "title": "Bitcoin: The End of Money as We Know It",
    "released": ISODate("2015-07-01T00:00:00.000Z"),
    "score": 2
    },
    {
    "genres": [ "Drama", "Mystery", "Sci-Fi" ],
    "title": "Pig",
    "released": ISODate("2015-07-02T00:00:00.000Z"),
    "score": 1.9681909084320068
    }
    ]
    2.

  • Bạn có thể sử dụng toán tử ghép để kết hợp hai hoặc nhiều toán tử và mệnh đề thành một truy vấn duy nhất. Hướng dẫn này sử dụng các điều khoản của toán tử hỗn hợp để tìm kiếm phim trong phạm vi ngày được chỉ định. Trong phần này, kết nối với cụm Atlas của bạn và chạy các truy vấn mẫu bằng toán tử ghép vào trường released trong bộ sưu tập sample_mflix.movies.

Chạy lệnh sau tại lời nhắc

[
{
"genres": [ "Action", "Adventure", "Sci-Fi" ],
"title": "Terminator Genisys",
"released": ISODate("2015-07-01T00:00:00.000Z"),
"score": 2
},
{
"genres": [ "Comedy", "Drama", "Music" ],
"title": "Magic Mike XXL",
"released": ISODate("2015-07-01T00:00:00.000Z"),
"score": 2
},
{
"genres": [ "Documentary", "Biography", "Drama" ],
"title": "Mala Mala",
"released": ISODate("2015-07-01T00:00:00.000Z"),
"score": 2
},
{
"genres": [ "Comedy", "Drama" ],
"title": "Home Care",
"released": ISODate("2015-07-01T00:00:00.000Z"),
"score": 2
},
{
"genres": [ "Documentary", "News" ],
"title": "Bitcoin: The End of Money as We Know It",
"released": ISODate("2015-07-01T00:00:00.000Z"),
"score": 2
},
{
"genres": [ "Drama", "Mystery", "Sci-Fi" ],
"title": "Pig",
"released": ISODate("2015-07-02T00:00:00.000Z"),
"score": 1.9681909084320068
}
]
5:
db.movies.aggregate([
{
"$search": {
"compound": {
"must": [{
"range": {
"path": "released",
"gt": ISODate("2015-01-01T00:00:00.000Z"),
"lt": ISODate("2015-12-31T00:00:00.000Z")
}
}],
"should": [{
"near": {
"path": "released",
"origin": ISODate("2015-07-01T00:00:00.000+00:00"),
"pivot": 2629800000
}
}],
"mustNot": [{
"text": {
"query": "documentary",
"path": "genres"
}
}]
}
}
},
{
"$limit": 10
},
{
"$project": {
"_id": 0,
"title": 1,
"released": 1,
"genres": 1,
"score": { "$meta": "searchScore" }
}
}
])
4
stage to limit the output to 6 results and a
db.movies.aggregate([
{
"$search": {
"compound": {
"must": [{
"range": {
"path": "released",
"gt": ISODate("2015-01-01T00:00:00.000Z"),
"lt": ISODate("2015-12-31T00:00:00.000Z")
}
}],
"should": [{
"near": {
"path": "released",
"origin": ISODate("2015-07-01T00:00:00.000+00:00"),
"pivot": 2629800000
}
}],
"mustNot": [{
"text": {
"query": "documentary",
"path": "genres"
}
}]
}
}
},
{
"$limit": 10
},
{
"$project": {
"_id": 0,
"title": 1,
"released": 1,
"genres": 1,
"score": { "$meta": "searchScore" }
}
}
])
5
stage to:

  • Các ví dụ sau đây sử dụng toán tử

    [
    {
    "genres": [ "Action", "Adventure", "Sci-Fi" ],
    "title": "Terminator Genisys",
    "released": ISODate("2015-07-01T00:00:00.000Z"),
    "score": 2
    },
    {
    "genres": [ "Comedy", "Drama", "Music" ],
    "title": "Magic Mike XXL",
    "released": ISODate("2015-07-01T00:00:00.000Z"),
    "score": 2
    },
    {
    "genres": [ "Documentary", "Biography", "Drama" ],
    "title": "Mala Mala",
    "released": ISODate("2015-07-01T00:00:00.000Z"),
    "score": 2
    },
    {
    "genres": [ "Comedy", "Drama" ],
    "title": "Home Care",
    "released": ISODate("2015-07-01T00:00:00.000Z"),
    "score": 2
    },
    {
    "genres": [ "Documentary", "News" ],
    "title": "Bitcoin: The End of Money as We Know It",
    "released": ISODate("2015-07-01T00:00:00.000Z"),
    "score": 2
    },
    {
    "genres": [ "Drama", "Mystery", "Sci-Fi" ],
    "title": "Pig",
    "released": ISODate("2015-07-02T00:00:00.000Z"),
    "score": 1.9681909084320068
    }
    ]
    6 với các nhóm phụ để tìm kiếm phim giữa các năm
    [
    {
    "genres": [ "Action", "Adventure", "Sci-Fi" ],
    "title": "Terminator Genisys",
    "released": ISODate("2015-07-01T00:00:00.000Z"),
    "score": 2
    },
    {
    "genres": [ "Comedy", "Drama", "Music" ],
    "title": "Magic Mike XXL",
    "released": ISODate("2015-07-01T00:00:00.000Z"),
    "score": 2
    },
    {
    "genres": [ "Documentary", "Biography", "Drama" ],
    "title": "Mala Mala",
    "released": ISODate("2015-07-01T00:00:00.000Z"),
    "score": 2
    },
    {
    "genres": [ "Comedy", "Drama" ],
    "title": "Home Care",
    "released": ISODate("2015-07-01T00:00:00.000Z"),
    "score": 2
    },
    {
    "genres": [ "Documentary", "News" ],
    "title": "Bitcoin: The End of Money as We Know It",
    "released": ISODate("2015-07-01T00:00:00.000Z"),
    "score": 2
    },
    {
    "genres": [ "Drama", "Mystery", "Sci-Fi" ],
    "title": "Pig",
    "released": ISODate("2015-07-02T00:00:00.000Z"),
    "score": 1.9681909084320068
    }
    ]
    7 đến
    [
    {
    "genres": [ "Action", "Adventure", "Sci-Fi" ],
    "title": "Terminator Genisys",
    "released": ISODate("2015-07-01T00:00:00.000Z"),
    "score": 2
    },
    {
    "genres": [ "Comedy", "Drama", "Music" ],
    "title": "Magic Mike XXL",
    "released": ISODate("2015-07-01T00:00:00.000Z"),
    "score": 2
    },
    {
    "genres": [ "Documentary", "Biography", "Drama" ],
    "title": "Mala Mala",
    "released": ISODate("2015-07-01T00:00:00.000Z"),
    "score": 2
    },
    {
    "genres": [ "Comedy", "Drama" ],
    "title": "Home Care",
    "released": ISODate("2015-07-01T00:00:00.000Z"),
    "score": 2
    },
    {
    "genres": [ "Documentary", "News" ],
    "title": "Bitcoin: The End of Money as We Know It",
    "released": ISODate("2015-07-01T00:00:00.000Z"),
    "score": 2
    },
    {
    "genres": [ "Drama", "Mystery", "Sci-Fi" ],
    "title": "Pig",
    "released": ISODate("2015-07-02T00:00:00.000Z"),
    "score": 1.9681909084320068
    }
    ]
    8. Truy vấn sử dụng các điều khoản sau:

  • Điều khoản

    [
    {
    "genres": [ "Action", "Adventure", "Sci-Fi" ],
    "title": "Terminator Genisys",
    "released": ISODate("2015-07-01T00:00:00.000Z"),
    "score": 2
    },
    {
    "genres": [ "Comedy", "Drama", "Music" ],
    "title": "Magic Mike XXL",
    "released": ISODate("2015-07-01T00:00:00.000Z"),
    "score": 2
    },
    {
    "genres": [ "Documentary", "Biography", "Drama" ],
    "title": "Mala Mala",
    "released": ISODate("2015-07-01T00:00:00.000Z"),
    "score": 2
    },
    {
    "genres": [ "Comedy", "Drama" ],
    "title": "Home Care",
    "released": ISODate("2015-07-01T00:00:00.000Z"),
    "score": 2
    },
    {
    "genres": [ "Documentary", "News" ],
    "title": "Bitcoin: The End of Money as We Know It",
    "released": ISODate("2015-07-01T00:00:00.000Z"),
    "score": 2
    },
    {
    "genres": [ "Drama", "Mystery", "Sci-Fi" ],
    "title": "Pig",
    "released": ISODate("2015-07-02T00:00:00.000Z"),
    "score": 1.9681909084320068
    }
    ]
    9 để tìm kiếm phim được phát hành giữa
    db.movies.aggregate([
    {
    "$search": {
    "compound": {
    "must": [{
    "range": {
    "path": "released",
    "gt": ISODate("2015-01-01T00:00:00.000Z"),
    "lt": ISODate("2015-12-31T00:00:00.000Z")
    }
    }],
    "should": [{
    "near": {
    "path": "released",
    "origin": ISODate("2015-07-01T00:00:00.000+00:00"),
    "pivot": 2629800000
    }
    }],
    "mustNot": [{
    "text": {
    "query": "documentary",
    "path": "genres"
    }
    }]
    }
    }
    },
    {
    "$limit": 10
    },
    {
    "$project": {
    "_id": 0,
    "title": 1,
    "released": 1,
    "genres": 1,
    "score": { "$meta": "searchScore" }
    }
    }
    ])
    0 và
    db.movies.aggregate([
    {
    "$search": {
    "compound": {
    "must": [{
    "range": {
    "path": "released",
    "gt": ISODate("2015-01-01T00:00:00.000Z"),
    "lt": ISODate("2015-12-31T00:00:00.000Z")
    }
    }],
    "should": [{
    "near": {
    "path": "released",
    "origin": ISODate("2015-07-01T00:00:00.000+00:00"),
    "pivot": 2629800000
    }
    }],
    "mustNot": [{
    "text": {
    "query": "documentary",
    "path": "genres"
    }
    }]
    }
    }
    },
    {
    "$limit": 10
    },
    {
    "$project": {
    "_id": 0,
    "title": 1,
    "released": 1,
    "genres": 1,
    "score": { "$meta": "searchScore" }
    }
    }
    ])
    1

db.movies.aggregate([
{
"$search": {
"compound": {
"must": [{
"range": {
"path": "released",
"gt": ISODate("2015-01-01T00:00:00.000Z"),
"lt": ISODate("2015-12-31T00:00:00.000Z")
}
}],
"should": [{
"near": {
"path": "released",
"origin": ISODate("2015-07-01T00:00:00.000+00:00"),
"pivot": 2629800000
}
}]
}
}
},
{
"$limit": 6
},
{
"$project": {
"_id": 0,
"title": 1,
"released": 1,
"genres": 1,
"score": { "$meta": "searchScore" }
}
}
])

Điều khoản

db.movies.aggregate([
{
"$search": {
"compound": {
"must": [{
"range": {
"path": "released",
"gt": ISODate("2015-01-01T00:00:00.000Z"),
"lt": ISODate("2015-12-31T00:00:00.000Z")
}
}],
"should": [{
"near": {
"path": "released",
"origin": ISODate("2015-07-01T00:00:00.000+00:00"),
"pivot": 2629800000
}
}],
"mustNot": [{
"text": {
"query": "documentary",
"path": "genres"
}
}]
}
}
},
{
"$limit": 10
},
{
"$project": {
"_id": 0,
"title": 1,
"released": 1,
"genres": 1,
"score": { "$meta": "searchScore" }
}
}
])
2 để chỉ định ưu tiên cho các bộ phim được phát hành gần
db.movies.aggregate([
{
"$search": {
"compound": {
"must": [{
"range": {
"path": "released",
"gt": ISODate("2015-01-01T00:00:00.000Z"),
"lt": ISODate("2015-12-31T00:00:00.000Z")
}
}],
"should": [{
"near": {
"path": "released",
"origin": ISODate("2015-07-01T00:00:00.000+00:00"),
"pivot": 2629800000
}
}],
"mustNot": [{
"text": {
"query": "documentary",
"path": "genres"
}
}]
}
}
},
{
"$limit": 10
},
{
"$project": {
"_id": 0,
"title": 1,
"released": 1,
"genres": 1,
"score": { "$meta": "searchScore" }
}
}
])
3, với khoảng cách xoay là 1 tháng

[
{
"genres": [ "Action", "Adventure", "Sci-Fi" ],
"title": "Terminator Genisys",
"released": ISODate("2015-07-01T00:00:00.000Z"),
"score": 2
},
{
"genres": [ "Comedy", "Drama", "Music" ],
"title": "Magic Mike XXL",
"released": ISODate("2015-07-01T00:00:00.000Z"),
"score": 2
},
{
"genres": [ "Documentary", "Biography", "Drama" ],
"title": "Mala Mala",
"released": ISODate("2015-07-01T00:00:00.000Z"),
"score": 2
},
{
"genres": [ "Comedy", "Drama" ],
"title": "Home Care",
"released": ISODate("2015-07-01T00:00:00.000Z"),
"score": 2
},
{
"genres": [ "Documentary", "News" ],
"title": "Bitcoin: The End of Money as We Know It",
"released": ISODate("2015-07-01T00:00:00.000Z"),
"score": 2
},
{
"genres": [ "Drama", "Mystery", "Sci-Fi" ],
"title": "Pig",
"released": ISODate("2015-07-02T00:00:00.000Z"),
"score": 1.9681909084320068
}
]

Các truy vấn ví dụ bao gồm giai đoạn

db.movies.aggregate([
{
"$search": {
"compound": {
"must": [{
"range": {
"path": "released",
"gt": ISODate("2015-01-01T00:00:00.000Z"),
"lt": ISODate("2015-12-31T00:00:00.000Z")
}
}],
"should": [{
"near": {
"path": "released",
"origin": ISODate("2015-07-01T00:00:00.000+00:00"),
"pivot": 2629800000
}
}],
"mustNot": [{
"text": {
"query": "documentary",
"path": "genres"
}
}]
}
}
},
{
"$limit": 10
},
{
"$project": {
"_id": 0,
"title": 1,
"released": 1,
"genres": 1,
"score": { "$meta": "searchScore" }
}
}
])
4 để giới hạn đầu ra ở 6 ​​kết quả và giai đoạn
db.movies.aggregate([
{
"$search": {
"compound": {
"must": [{
"range": {
"path": "released",
"gt": ISODate("2015-01-01T00:00:00.000Z"),
"lt": ISODate("2015-12-31T00:00:00.000Z")
}
}],
"should": [{
"near": {
"path": "released",
"origin": ISODate("2015-07-01T00:00:00.000+00:00"),
"pivot": 2629800000
}
}],
"mustNot": [{
"text": {
"query": "documentary",
"path": "genres"
}
}]
}
}
},
{
"$limit": 10
},
{
"$project": {
"_id": 0,
"title": 1,
"released": 1,
"genres": 1,
"score": { "$meta": "searchScore" }
}
}
])
5 thành:

Loại trừ tất cả các trường ngoại trừ các trường

db.movies.aggregate([
{
"$search": {
"compound": {
"must": [{
"range": {
"path": "released",
"gt": ISODate("2015-01-01T00:00:00.000Z"),
"lt": ISODate("2015-12-31T00:00:00.000Z")
}
}],
"should": [{
"near": {
"path": "released",
"origin": ISODate("2015-07-01T00:00:00.000+00:00"),
"pivot": 2629800000
}
}],
"mustNot": [{
"text": {
"query": "documentary",
"path": "genres"
}
}]
}
}
},
{
"$limit": 10
},
{
"$project": {
"_id": 0,
"title": 1,
"released": 1,
"genres": 1,
"score": { "$meta": "searchScore" }
}
}
])
6, released
db.movies.aggregate([
{
"$search": {
"compound": {
"must": [{
"range": {
"path": "released",
"gt": ISODate("2015-01-01T00:00:00.000Z"),
"lt": ISODate("2015-12-31T00:00:00.000Z")
}
}],
"should": [{
"near": {
"path": "released",
"origin": ISODate("2015-07-01T00:00:00.000+00:00"),
"pivot": 2629800000
}
}],
"mustNot": [{
"text": {
"query": "documentary",
"path": "genres"
}
}]
}
}
},
{
"$limit": 10
},
{
"$project": {
"_id": 0,
"title": 1,
"released": 1,
"genres": 1,
"score": { "$meta": "searchScore" }
}
}
])
8
db.movies.aggregate([
{
"$search": {
"compound": {
"must": [{
"range": {
"path": "released",
"gt": ISODate("2015-01-01T00:00:00.000Z"),
"lt": ISODate("2015-12-31T00:00:00.000Z")
}
}],
"should": [{
"near": {
"path": "released",
"origin": ISODate("2015-07-01T00:00:00.000+00:00"),
"pivot": 2629800000
}
}],
"mustNot": [{
"text": {
"query": "documentary",
"path": "genres"
}
}]
}
}
},
{
"$limit": 10
},
{
"$project": {
"_id": 0,
"title": 1,
"released": 1,
"genres": 1,
"score": { "$meta": "searchScore" }
}
}
])
4
stage, and the
db.movies.aggregate([
{
"$search": {
"compound": {
"must": [{
"range": {
"path": "released",
"gt": ISODate("2015-01-01T00:00:00.000Z"),
"lt": ISODate("2015-12-31T00:00:00.000Z")
}
}],
"should": [{
"near": {
"path": "released",
"origin": ISODate("2015-07-01T00:00:00.000+00:00"),
"pivot": 2629800000
}
}],
"mustNot": [{
"text": {
"query": "documentary",
"path": "genres"
}
}]
}
}
},
{
"$limit": 10
},
{
"$project": {
"_id": 0,
"title": 1,
"released": 1,
"genres": 1,
"score": { "$meta": "searchScore" }
}
}
])
5
stages to perform the same search as the previous query. In addition to the
[
{
"genres": [ "Action", "Adventure", "Sci-Fi" ],
"title": "Terminator Genisys",
"released": ISODate("2015-07-01T00:00:00.000Z"),
"score": 2
},
{
"genres": [ "Comedy", "Drama", "Music" ],
"title": "Magic Mike XXL",
"released": ISODate("2015-07-01T00:00:00.000Z"),
"score": 2
},
{
"genres": [ "Documentary", "Biography", "Drama" ],
"title": "Mala Mala",
"released": ISODate("2015-07-01T00:00:00.000Z"),
"score": 2
},
{
"genres": [ "Comedy", "Drama" ],
"title": "Home Care",
"released": ISODate("2015-07-01T00:00:00.000Z"),
"score": 2
},
{
"genres": [ "Documentary", "News" ],
"title": "Bitcoin: The End of Money as We Know It",
"released": ISODate("2015-07-01T00:00:00.000Z"),
"score": 2
},
{
"genres": [ "Drama", "Mystery", "Sci-Fi" ],
"title": "Pig",
"released": ISODate("2015-07-02T00:00:00.000Z"),
"score": 1.9681909084320068
}
]
9 and
db.movies.aggregate([
{
"$search": {
"compound": {
"must": [{
"range": {
"path": "released",
"gt": ISODate("2015-01-01T00:00:00.000Z"),
"lt": ISODate("2015-12-31T00:00:00.000Z")
}
}],
"should": [{
"near": {
"path": "released",
"origin": ISODate("2015-07-01T00:00:00.000+00:00"),
"pivot": 2629800000
}
}],
"mustNot": [{
"text": {
"query": "documentary",
"path": "genres"
}
}]
}
}
},
{
"$limit": 10
},
{
"$project": {
"_id": 0,
"title": 1,
"released": 1,
"genres": 1,
"score": { "$meta": "searchScore" }
}
}
])
2 clauses, the query also includes a
[
{
"genres": [ "Action", "Adventure", "Sci-Fi" ],
"title": "Terminator Genisys",
"released": ISODate("2015-07-01T00:00:00.000Z"),
"score": 2
},
{
"genres": [ "Comedy", "Drama", "Music" ],
"title": "Magic Mike XXL",
"released": ISODate("2015-07-01T00:00:00.000Z"),
"score": 2
},
{
"genres": [ "Comedy", "Drama" ],
"title": "Home Care",
"released": ISODate("2015-07-01T00:00:00.000Z"),
"score": 2
},
{
"genres": [ "Drama", "Mystery", "Sci-Fi" ],
"title": "Pig",
"released": ISODate("2015-07-02T00:00:00.000Z"),
"score": 1.9681909084320068
},
{
"genres": [ "Drama", "History", "Romance" ],
"title": "Gold Coast",
"released": ISODate("2015-07-02T00:00:00.000Z"),
"score": 1.9681909084320068
},
{
"genres": [ "Animation", "Family" ],
"title": "Zarafa",
"released": ISODate("2015-07-03T00:00:00.000Z"),
"score": 1.9383430480957031
}
]
6 clause to specify that movies in the
[
{
"genres": [ "Action", "Adventure", "Sci-Fi" ],
"title": "Terminator Genisys",
"released": ISODate("2015-07-01T00:00:00.000Z"),
"score": 2
},
{
"genres": [ "Comedy", "Drama", "Music" ],
"title": "Magic Mike XXL",
"released": ISODate("2015-07-01T00:00:00.000Z"),
"score": 2
},
{
"genres": [ "Comedy", "Drama" ],
"title": "Home Care",
"released": ISODate("2015-07-01T00:00:00.000Z"),
"score": 2
},
{
"genres": [ "Drama", "Mystery", "Sci-Fi" ],
"title": "Pig",
"released": ISODate("2015-07-02T00:00:00.000Z"),
"score": 1.9681909084320068
},
{
"genres": [ "Drama", "History", "Romance" ],
"title": "Gold Coast",
"released": ISODate("2015-07-02T00:00:00.000Z"),
"score": 1.9681909084320068
},
{
"genres": [ "Animation", "Family" ],
"title": "Zarafa",
"released": ISODate("2015-07-03T00:00:00.000Z"),
"score": 1.9383430480957031
}
]
7 genre must not be included in the results.

db.movies.aggregate([
{
"$search": {
"compound": {
"must": [{
"range": {
"path": "released",
"gt": ISODate("2015-01-01T00:00:00.000Z"),
"lt": ISODate("2015-12-31T00:00:00.000Z")
}
}],
"should": [{
"near": {
"path": "released",
"origin": ISODate("2015-07-01T00:00:00.000+00:00"),
"pivot": 2629800000
}
}],
"mustNot": [{
"text": {
"query": "documentary",
"path": "genres"
}
}]
}
}
},
{
"$limit": 10
},
{
"$project": {
"_id": 0,
"title": 1,
"released": 1,
"genres": 1,
"score": { "$meta": "searchScore" }
}
}
])

Điều khoản

db.movies.aggregate([
{
"$search": {
"compound": {
"must": [{
"range": {
"path": "released",
"gt": ISODate("2015-01-01T00:00:00.000Z"),
"lt": ISODate("2015-12-31T00:00:00.000Z")
}
}],
"should": [{
"near": {
"path": "released",
"origin": ISODate("2015-07-01T00:00:00.000+00:00"),
"pivot": 2629800000
}
}],
"mustNot": [{
"text": {
"query": "documentary",
"path": "genres"
}
}]
}
}
},
{
"$limit": 10
},
{
"$project": {
"_id": 0,
"title": 1,
"released": 1,
"genres": 1,
"score": { "$meta": "searchScore" }
}
}
])
2 để chỉ định ưu tiên cho các bộ phim được phát hành gần
db.movies.aggregate([
{
"$search": {
"compound": {
"must": [{
"range": {
"path": "released",
"gt": ISODate("2015-01-01T00:00:00.000Z"),
"lt": ISODate("2015-12-31T00:00:00.000Z")
}
}],
"should": [{
"near": {
"path": "released",
"origin": ISODate("2015-07-01T00:00:00.000+00:00"),
"pivot": 2629800000
}
}],
"mustNot": [{
"text": {
"query": "documentary",
"path": "genres"
}
}]
}
}
},
{
"$limit": 10
},
{
"$project": {
"_id": 0,
"title": 1,
"released": 1,
"genres": 1,
"score": { "$meta": "searchScore" }
}
}
])
3, với khoảng cách xoay là 1 tháng

[
{
"genres": [ "Action", "Adventure", "Sci-Fi" ],
"title": "Terminator Genisys",
"released": ISODate("2015-07-01T00:00:00.000Z"),
"score": 2
},
{
"genres": [ "Comedy", "Drama", "Music" ],
"title": "Magic Mike XXL",
"released": ISODate("2015-07-01T00:00:00.000Z"),
"score": 2
},
{
"genres": [ "Comedy", "Drama" ],
"title": "Home Care",
"released": ISODate("2015-07-01T00:00:00.000Z"),
"score": 2
},
{
"genres": [ "Drama", "Mystery", "Sci-Fi" ],
"title": "Pig",
"released": ISODate("2015-07-02T00:00:00.000Z"),
"score": 1.9681909084320068
},
{
"genres": [ "Drama", "History", "Romance" ],
"title": "Gold Coast",
"released": ISODate("2015-07-02T00:00:00.000Z"),
"score": 1.9681909084320068
},
{
"genres": [ "Animation", "Family" ],
"title": "Zarafa",
"released": ISODate("2015-07-03T00:00:00.000Z"),
"score": 1.9383430480957031
}
]

Các truy vấn ví dụ bao gồm giai đoạn

db.movies.aggregate([
{
"$search": {
"compound": {
"must": [{
"range": {
"path": "released",
"gt": ISODate("2015-01-01T00:00:00.000Z"),
"lt": ISODate("2015-12-31T00:00:00.000Z")
}
}],
"should": [{
"near": {
"path": "released",
"origin": ISODate("2015-07-01T00:00:00.000+00:00"),
"pivot": 2629800000
}
}],
"mustNot": [{
"text": {
"query": "documentary",
"path": "genres"
}
}]
}
}
},
{
"$limit": 10
},
{
"$project": {
"_id": 0,
"title": 1,
"released": 1,
"genres": 1,
"score": { "$meta": "searchScore" }
}
}
])
4 để giới hạn đầu ra ở 6 ​​kết quả và giai đoạn
db.movies.aggregate([
{
"$search": {
"compound": {
"must": [{
"range": {
"path": "released",
"gt": ISODate("2015-01-01T00:00:00.000Z"),
"lt": ISODate("2015-12-31T00:00:00.000Z")
}
}],
"should": [{
"near": {
"path": "released",
"origin": ISODate("2015-07-01T00:00:00.000+00:00"),
"pivot": 2629800000
}
}],
"mustNot": [{
"text": {
"query": "documentary",
"path": "genres"
}
}]
}
}
},
{
"$limit": 10
},
{
"$project": {
"_id": 0,
"title": 1,
"released": 1,
"genres": 1,
"score": { "$meta": "searchScore" }
}
}
])
5 thành: