Hướng dẫn can a single model be used to query more than one mongodb collection? - có thể sử dụng một mô hình duy nhất để truy vấn nhiều hơn một bộ sưu tập mongodb không?

Bạn có thể sử dụng

{
    "_id" : ObjectId("5901a5f83541b7d5d3293768"),
    "userId" : ObjectId("5901a4c63541b7d5d3293766"),
    "address" : "Gurgaon",
    "mob" : "9876543211"
}
6 (nhiều) để lấy hồ sơ từ nhiều bộ sưu tập:

Example:

Nếu bạn có nhiều bộ sưu tập hơn (tôi có 3 bộ sưu tập cho bản demo ở đây, bạn có thể có nhiều hơn 3). Và tôi muốn lấy dữ liệu từ 3 bộ sưu tập trong một đối tượng:

Bộ sưu tập như:

db.doc1.find().pretty();

{
    "_id" : ObjectId("5901a4c63541b7d5d3293766"),
    "firstName" : "shubham",
    "lastName" : "verma"
}

db.doc2.find().pretty();

{
    "_id" : ObjectId("5901a5f83541b7d5d3293768"),
    "userId" : ObjectId("5901a4c63541b7d5d3293766"),
    "address" : "Gurgaon",
    "mob" : "9876543211"
}

db.doc3.find().pretty();

{
    "_id" : ObjectId("5901b0f6d318b072ceea44fb"),
    "userId" : ObjectId("5901a4c63541b7d5d3293766"),
    "fbURLs" : "http://www.facebook.com",
    "twitterURLs" : "http://www.twitter.com"
}

Bây giờ truy vấn của bạn sẽ như dưới đây:

db.doc1.aggregate([
    { $match: { _id: ObjectId("5901a4c63541b7d5d3293766") } },
    {
        $lookup:
        {
            from: "doc2",
            localField: "_id",
            foreignField: "userId",
            as: "address"
        }
    },
    {
        $unwind: "$address"
    },
    {
        $project: {
            __v: 0,
            "address.__v": 0,
            "address._id": 0,
            "address.userId": 0,
            "address.mob": 0
        }
    },
    {
        $lookup:
        {
            from: "doc3",
            localField: "_id",
            foreignField: "userId",
            as: "social"
        }
    },
    {
        $unwind: "$social"
    },

  {   
    $project: {      
           __v: 0,      
           "social.__v": 0,      
           "social._id": 0,      
           "social.userId": 0
       }
 }

]).pretty();

Sau đó, kết quả của bạn sẽ là:

{
    "_id" : ObjectId("5901a4c63541b7d5d3293766"),
    "firstName" : "shubham",
    "lastName" : "verma",

    "address" : {
        "address" : "Gurgaon"
    },
    "social" : {
        "fbURLs" : "http://www.facebook.com",
        "twitterURLs" : "http://www.twitter.com"
    }
}

Nếu bạn muốn tất cả các bản ghi từ mỗi bộ sưu tập thì bạn nên xóa dòng bên dưới khỏi truy vấn:

{
            $project: {
                __v: 0,
                "address.__v": 0,
                "address._id": 0,
                "address.userId": 0,
                "address.mob": 0
            }
        }

{   
        $project: {      
               "social.__v": 0,      
               "social._id": 0,      
               "social.userId": 0
           }
     }

Sau khi xóa mã trên, bạn sẽ nhận được tổng số bản ghi là:

{
    "_id" : ObjectId("5901a4c63541b7d5d3293766"),
    "firstName" : "shubham",
    "lastName" : "verma",
    "address" : {
        "_id" : ObjectId("5901a5f83541b7d5d3293768"),
        "userId" : ObjectId("5901a4c63541b7d5d3293766"),
        "address" : "Gurgaon",
        "mob" : "9876543211"
    },
    "social" : {
        "_id" : ObjectId("5901b0f6d318b072ceea44fb"),
        "userId" : ObjectId("5901a4c63541b7d5d3293766"),
        "fbURLs" : "http://www.facebook.com",
        "twitterURLs" : "http://www.twitter.com"
    }
}

Truy vấn trên trả về

exports = function(){
var pipeline = [
{ $match: {purchaseMethod: "Phone"} },
{ $unwind: {path: "$items"}},
{ $group: {
_id: { $dateToString:
{ format: "%Y-%m", date: "$saleDate" } },
sales_quantity: { $sum: "$items.quantity"},
sales_price: { $sum: "$items.price"}
}},
{ $set: { sales_price: { $toDouble: "$sales_price"}}},
{ $merge: { into: "monthlysales", whenMatched: "replace" } }
]
var monthlysales = context.services.get("mongodb-atlas").db("sample_supplies").collection("sales");
return monthlysales.aggregate(pipeline);
};
4, chỉ ra rằng chỉ 4 tháng trong tất cả các tháng trong
{
    "_id" : ObjectId("5901b0f6d318b072ceea44fb"),
    "userId" : ObjectId("5901a4c63541b7d5d3293766"),
    "fbURLs" : "http://www.facebook.com",
    "twitterURLs" : "http://www.twitter.com"
}
1 có tổng doanh thu lớn hơn hoặc bằng 10000 đô la.MongoDB Atlas

Để biết tài liệu đường ống tổng hợp hoàn chỉnh, hãy xem Hướng dẫn sử dụng máy chủ MongoDB.

  • Tôi có thể truy vấn nhiều bộ sưu tập trong MongoDB không?
  • Tập hợp - Như nghĩa đen cho thấy nó liên quan đến việc kết hợp nhiều thứ khác nhau, tương tự trong tập hợp MongoDB là một kỹ thuật để truy vấn dữ liệu từ nhiều bộ sưu tập bằng cách nhóm hoặc tham gia chúng theo sau bằng cách thực hiện nhiều hoạt động khác nhau (được giải thích sau trong tài liệu này) và sau đó trả về kết quả được tính toán .
  • MongoDB có thể mô hình nhiều cho nhiều người không?
  • Một mối quan hệ nhiều người nhiều (N: M) vì không có lệnh nào để thực hiện mối quan hệ nhiều đến nhiều trong cơ sở dữ liệu quan hệ, khó khăn hơn một mối quan hệ một-nhiều. Điều tương tự cũng đúng khi sử dụng MongoDB để thực hiện chúng. Trên thực tế, bạn không thể sử dụng một lệnh để tạo bất kỳ loại mối quan hệ nào trong MongoDB.

Bạn sẽ sử dụng phương pháp nào để tìm nhiều dữ liệu từ MongoDB?on-demand materialized views and Atlas App Services scheduled triggers on the

{
    "_id" : ObjectId("5901a5f83541b7d5d3293768"),
    "userId" : ObjectId("5901a4c63541b7d5d3293766"),
    "address" : "Gurgaon",
    "mob" : "9876543211"
}
7 collection from the sample dataset.

Bạn có thể truy vấn cho nhiều tài liệu trong một bộ sưu tập với comcand.find (). Phương thức Find () sử dụng tài liệu truy vấn mà bạn cung cấp để khớp với tập hợp con của các tài liệu trong bộ sưu tập phù hợp với truy vấn.

Làm cách nào để truy vấn nhiều giá trị trong MongoDB?

  1. MongoDB cung cấp Find () được sử dụng để tìm nhiều giá trị hoặc tài liệu từ bộ sưu tập. Phương thức Find () trả về một con trỏ của tập kết quả và in tất cả các tài liệu. Để tìm nhiều giá trị, chúng ta có thể sử dụng các hoạt động tổng hợp được cung cấp bởi chính MongoDB.

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

  3. Trên trang này

  4. Tạo chức năng dịch vụ ứng dụng để xác định chế độ xem được thực hiện theo yêu cầu

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.

Tạo chức năng JavaScript trong UI Dịch vụ ứng dụng có tên

{
    "_id" : ObjectId("5901b0f6d318b072ceea44fb"),
    "userId" : ObjectId("5901a4c63541b7d5d3293766"),
    "fbURLs" : "http://www.facebook.com",
    "twitterURLs" : "http://www.twitter.com"
}
0. Hàm xác định một quan điểm được vật chất hóa có chứa thông tin bán hàng hàng tháng tích lũy từ
{
    "_id" : ObjectId("5901a5f83541b7d5d3293768"),
    "userId" : ObjectId("5901a4c63541b7d5d3293766"),
    "address" : "Gurgaon",
    "mob" : "9876543211"
}
7.

Hàm

{
    "_id" : ObjectId("5901b0f6d318b072ceea44fb"),
    "userId" : ObjectId("5901a4c63541b7d5d3293766"),
    "fbURLs" : "http://www.facebook.com",
    "twitterURLs" : "http://www.twitter.com"
}
0 xác định chế độ xem được vật chất hóa
{
    "_id" : ObjectId("5901b0f6d318b072ceea44fb"),
    "userId" : ObjectId("5901a4c63541b7d5d3293766"),
    "fbURLs" : "http://www.facebook.com",
    "twitterURLs" : "http://www.twitter.com"
}
1 có chứa thông tin bán hàng hàng tháng tích lũy. Chức năng cập nhật thông tin bán hàng hàng tháng cho doanh số được thực hiện qua điện thoại. Ví dụ sau đây xác định chức năng:

exports = function(){
var pipeline = [
{ $match: {purchaseMethod: "Phone"} },
{ $unwind: {path: "$items"}},
{ $group: {
_id: { $dateToString:
{ format: "%Y-%m", date: "$saleDate" } },
sales_quantity: { $sum: "$items.quantity"},
sales_price: { $sum: "$items.price"}
}},
{ $set: { sales_price: { $toDouble: "$sales_price"}}},
{ $merge: { into: "monthlysales", whenMatched: "replace" } }
]
var monthlysales = context.services.get("mongodb-atlas").db("sample_supplies").collection("sales");
return monthlysales.aggregate(pipeline);
};

Hàm sử dụng các giai đoạn đường ống tổng hợp sau để cập nhật

{
    "_id" : ObjectId("5901b0f6d318b072ceea44fb"),
    "userId" : ObjectId("5901a4c63541b7d5d3293766"),
    "fbURLs" : "http://www.facebook.com",
    "twitterURLs" : "http://www.twitter.com"
}
1:

  • Giai đoạn

    db.doc1.aggregate([
        { $match: { _id: ObjectId("5901a4c63541b7d5d3293766") } },
        {
            $lookup:
            {
                from: "doc2",
                localField: "_id",
                foreignField: "userId",
                as: "address"
            }
        },
        {
            $unwind: "$address"
        },
        {
            $project: {
                __v: 0,
                "address.__v": 0,
                "address._id": 0,
                "address.userId": 0,
                "address.mob": 0
            }
        },
        {
            $lookup:
            {
                from: "doc3",
                localField: "_id",
                foreignField: "userId",
                as: "social"
            }
        },
        {
            $unwind: "$social"
        },
    
      {   
        $project: {      
               __v: 0,      
               "social.__v": 0,      
               "social._id": 0,      
               "social.userId": 0
           }
     }
    
    ]).pretty();
    
    2 lọc dữ liệu để chỉ xử lý các doanh số được hoàn thành trong
    db.doc1.aggregate([
        { $match: { _id: ObjectId("5901a4c63541b7d5d3293766") } },
        {
            $lookup:
            {
                from: "doc2",
                localField: "_id",
                foreignField: "userId",
                as: "address"
            }
        },
        {
            $unwind: "$address"
        },
        {
            $project: {
                __v: 0,
                "address.__v": 0,
                "address._id": 0,
                "address.userId": 0,
                "address.mob": 0
            }
        },
        {
            $lookup:
            {
                from: "doc3",
                localField: "_id",
                foreignField: "userId",
                as: "social"
            }
        },
        {
            $unwind: "$social"
        },
    
      {   
        $project: {      
               __v: 0,      
               "social.__v": 0,      
               "social._id": 0,      
               "social.userId": 0
           }
     }
    
    ]).pretty();
    
    3.
    db.doc1.aggregate([
        { $match: { _id: ObjectId("5901a4c63541b7d5d3293766") } },
        {
            $lookup:
            {
                from: "doc2",
                localField: "_id",
                foreignField: "userId",
                as: "address"
            }
        },
        {
            $unwind: "$address"
        },
        {
            $project: {
                __v: 0,
                "address.__v": 0,
                "address._id": 0,
                "address.userId": 0,
                "address.mob": 0
            }
        },
        {
            $lookup:
            {
                from: "doc3",
                localField: "_id",
                foreignField: "userId",
                as: "social"
            }
        },
        {
            $unwind: "$social"
        },
    
      {   
        $project: {      
               __v: 0,      
               "social.__v": 0,      
               "social._id": 0,      
               "social.userId": 0
           }
     }
    
    ]).pretty();
    
    2
    stage filters the data to process only those sales that were completed over the
    db.doc1.aggregate([
        { $match: { _id: ObjectId("5901a4c63541b7d5d3293766") } },
        {
            $lookup:
            {
                from: "doc2",
                localField: "_id",
                foreignField: "userId",
                as: "address"
            }
        },
        {
            $unwind: "$address"
        },
        {
            $project: {
                __v: 0,
                "address.__v": 0,
                "address._id": 0,
                "address.userId": 0,
                "address.mob": 0
            }
        },
        {
            $lookup:
            {
                from: "doc3",
                localField: "_id",
                foreignField: "userId",
                as: "social"
            }
        },
        {
            $unwind: "$social"
        },
    
      {   
        $project: {      
               __v: 0,      
               "social.__v": 0,      
               "social._id": 0,      
               "social.userId": 0
           }
     }
    
    ]).pretty();
    
    3.

  • Giai đoạn ____34 nhóm thông tin bán hàng vào năm. Giai đoạn này xuất ra các tài liệu có biểu mẫu:

    db.doc1.aggregate([
        { $match: { _id: ObjectId("5901a4c63541b7d5d3293766") } },
        {
            $lookup:
            {
                from: "doc2",
                localField: "_id",
                foreignField: "userId",
                as: "address"
            }
        },
        {
            $unwind: "$address"
        },
        {
            $project: {
                __v: 0,
                "address.__v": 0,
                "address._id": 0,
                "address.userId": 0,
                "address.mob": 0
            }
        },
        {
            $lookup:
            {
                from: "doc3",
                localField: "_id",
                foreignField: "userId",
                as: "social"
            }
        },
        {
            $unwind: "$social"
        },
    
      {   
        $project: {      
               __v: 0,      
               "social.__v": 0,      
               "social._id": 0,      
               "social.userId": 0
           }
     }
    
    ]).pretty();
    
    4 stage groups the sales information by the year-month. This stage outputs documents that have the form:

    { "_id" : "", "sales_quantity" : , "sales_amount" : <NumberDecimal> }

  • Giai đoạn

    db.doc1.aggregate([
        { $match: { _id: ObjectId("5901a4c63541b7d5d3293766") } },
        {
            $lookup:
            {
                from: "doc2",
                localField: "_id",
                foreignField: "userId",
                as: "address"
            }
        },
        {
            $unwind: "$address"
        },
        {
            $project: {
                __v: 0,
                "address.__v": 0,
                "address._id": 0,
                "address.userId": 0,
                "address.mob": 0
            }
        },
        {
            $lookup:
            {
                from: "doc3",
                localField: "_id",
                foreignField: "userId",
                as: "social"
            }
        },
        {
            $unwind: "$social"
        },
    
      {   
        $project: {      
               __v: 0,      
               "social.__v": 0,      
               "social._id": 0,      
               "social.userId": 0
           }
     }
    
    ]).pretty();
    
    5 thay đổi loại dữ liệu của trường
    db.doc1.aggregate([
        { $match: { _id: ObjectId("5901a4c63541b7d5d3293766") } },
        {
            $lookup:
            {
                from: "doc2",
                localField: "_id",
                foreignField: "userId",
                as: "address"
            }
        },
        {
            $unwind: "$address"
        },
        {
            $project: {
                __v: 0,
                "address.__v": 0,
                "address._id": 0,
                "address.userId": 0,
                "address.mob": 0
            }
        },
        {
            $lookup:
            {
                from: "doc3",
                localField: "_id",
                foreignField: "userId",
                as: "social"
            }
        },
        {
            $unwind: "$social"
        },
    
      {   
        $project: {      
               __v: 0,      
               "social.__v": 0,      
               "social._id": 0,      
               "social.userId": 0
           }
     }
    
    ]).pretty();
    
    6 thành
    db.doc1.aggregate([
        { $match: { _id: ObjectId("5901a4c63541b7d5d3293766") } },
        {
            $lookup:
            {
                from: "doc2",
                localField: "_id",
                foreignField: "userId",
                as: "address"
            }
        },
        {
            $unwind: "$address"
        },
        {
            $project: {
                __v: 0,
                "address.__v": 0,
                "address._id": 0,
                "address.userId": 0,
                "address.mob": 0
            }
        },
        {
            $lookup:
            {
                from: "doc3",
                localField: "_id",
                foreignField: "userId",
                as: "social"
            }
        },
        {
            $unwind: "$social"
        },
    
      {   
        $project: {      
               __v: 0,      
               "social.__v": 0,      
               "social._id": 0,      
               "social.userId": 0
           }
     }
    
    ]).pretty();
    
    7. Các nhà khai thác tìm kiếm ATLAS
    {
        "_id" : ObjectId("5901a5f83541b7d5d3293768"),
        "userId" : ObjectId("5901a4c63541b7d5d3293766"),
        "address" : "Gurgaon",
        "mob" : "9876543211"
    }
    
    9 không hỗ trợ kiểu dữ liệu
    db.doc1.aggregate([
        { $match: { _id: ObjectId("5901a4c63541b7d5d3293766") } },
        {
            $lookup:
            {
                from: "doc2",
                localField: "_id",
                foreignField: "userId",
                as: "address"
            }
        },
        {
            $unwind: "$address"
        },
        {
            $project: {
                __v: 0,
                "address.__v": 0,
                "address._id": 0,
                "address.userId": 0,
                "address.mob": 0
            }
        },
        {
            $lookup:
            {
                from: "doc3",
                localField: "_id",
                foreignField: "userId",
                as: "social"
            }
        },
        {
            $unwind: "$social"
        },
    
      {   
        $project: {      
               __v: 0,      
               "social.__v": 0,      
               "social._id": 0,      
               "social.userId": 0
           }
     }
    
    ]).pretty();
    
    9. Thay đổi kiểu dữ liệu của trường
    db.doc1.aggregate([
        { $match: { _id: ObjectId("5901a4c63541b7d5d3293766") } },
        {
            $lookup:
            {
                from: "doc2",
                localField: "_id",
                foreignField: "userId",
                as: "address"
            }
        },
        {
            $unwind: "$address"
        },
        {
            $project: {
                __v: 0,
                "address.__v": 0,
                "address._id": 0,
                "address.userId": 0,
                "address.mob": 0
            }
        },
        {
            $lookup:
            {
                from: "doc3",
                localField: "_id",
                foreignField: "userId",
                as: "social"
            }
        },
        {
            $unwind: "$social"
        },
    
      {   
        $project: {      
               __v: 0,      
               "social.__v": 0,      
               "social._id": 0,      
               "social.userId": 0
           }
     }
    
    ]).pretty();
    
    6 cho phép bạn truy vấn trường này bằng các chỉ mục tìm kiếm ATLAS.
    db.doc1.aggregate([
        { $match: { _id: ObjectId("5901a4c63541b7d5d3293766") } },
        {
            $lookup:
            {
                from: "doc2",
                localField: "_id",
                foreignField: "userId",
                as: "address"
            }
        },
        {
            $unwind: "$address"
        },
        {
            $project: {
                __v: 0,
                "address.__v": 0,
                "address._id": 0,
                "address.userId": 0,
                "address.mob": 0
            }
        },
        {
            $lookup:
            {
                from: "doc3",
                localField: "_id",
                foreignField: "userId",
                as: "social"
            }
        },
        {
            $unwind: "$social"
        },
    
      {   
        $project: {      
               __v: 0,      
               "social.__v": 0,      
               "social._id": 0,      
               "social.userId": 0
           }
     }
    
    ]).pretty();
    
    5
    stage changes the data type of the
    db.doc1.aggregate([
        { $match: { _id: ObjectId("5901a4c63541b7d5d3293766") } },
        {
            $lookup:
            {
                from: "doc2",
                localField: "_id",
                foreignField: "userId",
                as: "address"
            }
        },
        {
            $unwind: "$address"
        },
        {
            $project: {
                __v: 0,
                "address.__v": 0,
                "address._id": 0,
                "address.userId": 0,
                "address.mob": 0
            }
        },
        {
            $lookup:
            {
                from: "doc3",
                localField: "_id",
                foreignField: "userId",
                as: "social"
            }
        },
        {
            $unwind: "$social"
        },
    
      {   
        $project: {      
               __v: 0,      
               "social.__v": 0,      
               "social._id": 0,      
               "social.userId": 0
           }
     }
    
    ]).pretty();
    
    6 field to
    db.doc1.aggregate([
        { $match: { _id: ObjectId("5901a4c63541b7d5d3293766") } },
        {
            $lookup:
            {
                from: "doc2",
                localField: "_id",
                foreignField: "userId",
                as: "address"
            }
        },
        {
            $unwind: "$address"
        },
        {
            $project: {
                __v: 0,
                "address.__v": 0,
                "address._id": 0,
                "address.userId": 0,
                "address.mob": 0
            }
        },
        {
            $lookup:
            {
                from: "doc3",
                localField: "_id",
                foreignField: "userId",
                as: "social"
            }
        },
        {
            $unwind: "$social"
        },
    
      {   
        $project: {      
               __v: 0,      
               "social.__v": 0,      
               "social._id": 0,      
               "social.userId": 0
           }
     }
    
    ]).pretty();
    
    7. Atlas Search
    {
        "_id" : ObjectId("5901a5f83541b7d5d3293768"),
        "userId" : ObjectId("5901a4c63541b7d5d3293766"),
        "address" : "Gurgaon",
        "mob" : "9876543211"
    }
    
    9 operators don't support the
    db.doc1.aggregate([
        { $match: { _id: ObjectId("5901a4c63541b7d5d3293766") } },
        {
            $lookup:
            {
                from: "doc2",
                localField: "_id",
                foreignField: "userId",
                as: "address"
            }
        },
        {
            $unwind: "$address"
        },
        {
            $project: {
                __v: 0,
                "address.__v": 0,
                "address._id": 0,
                "address.userId": 0,
                "address.mob": 0
            }
        },
        {
            $lookup:
            {
                from: "doc3",
                localField: "_id",
                foreignField: "userId",
                as: "social"
            }
        },
        {
            $unwind: "$social"
        },
    
      {   
        $project: {      
               __v: 0,      
               "social.__v": 0,      
               "social._id": 0,      
               "social.userId": 0
           }
     }
    
    ]).pretty();
    
    9 data type. Changing the
    db.doc1.aggregate([
        { $match: { _id: ObjectId("5901a4c63541b7d5d3293766") } },
        {
            $lookup:
            {
                from: "doc2",
                localField: "_id",
                foreignField: "userId",
                as: "address"
            }
        },
        {
            $unwind: "$address"
        },
        {
            $project: {
                __v: 0,
                "address.__v": 0,
                "address._id": 0,
                "address.userId": 0,
                "address.mob": 0
            }
        },
        {
            $lookup:
            {
                from: "doc3",
                localField: "_id",
                foreignField: "userId",
                as: "social"
            }
        },
        {
            $unwind: "$social"
        },
    
      {   
        $project: {      
               __v: 0,      
               "social.__v": 0,      
               "social._id": 0,      
               "social.userId": 0
           }
     }
    
    ]).pretty();
    
    6 field's data type allows you to query this field using Atlas Search indexes.

  • Giai đoạn

    {
        "_id" : ObjectId("5901a5f83541b7d5d3293768"),
        "userId" : ObjectId("5901a4c63541b7d5d3293766"),
        "address" : "Gurgaon",
        "mob" : "9876543211"
    }
    
    8 viết đầu ra cho bộ sưu tập
    {
        "_id" : ObjectId("5901b0f6d318b072ceea44fb"),
        "userId" : ObjectId("5901a4c63541b7d5d3293766"),
        "fbURLs" : "http://www.facebook.com",
        "twitterURLs" : "http://www.twitter.com"
    }
    
    1.
    {
        "_id" : ObjectId("5901a5f83541b7d5d3293768"),
        "userId" : ObjectId("5901a4c63541b7d5d3293766"),
        "address" : "Gurgaon",
        "mob" : "9876543211"
    }
    
    8
    stage writes the output to the
    {
        "_id" : ObjectId("5901b0f6d318b072ceea44fb"),
        "userId" : ObjectId("5901a4c63541b7d5d3293766"),
        "fbURLs" : "http://www.facebook.com",
        "twitterURLs" : "http://www.twitter.com"
    }
    
    1 collection.

    Dựa trên trường

    {
        "_id" : ObjectId("5901a4c63541b7d5d3293766"),
        "firstName" : "shubham",
        "lastName" : "verma",
    
        "address" : {
            "address" : "Gurgaon"
        },
        "social" : {
            "fbURLs" : "http://www.facebook.com",
            "twitterURLs" : "http://www.twitter.com"
        }
    }
    
    3, (mặc định cho các bộ sưu tập đầu ra không có đầu cơ), giai đoạn kiểm tra xem tài liệu trong kết quả tổng hợp có khớp với một tài liệu hiện có trong bộ sưu tập không: on the
    {
        "_id" : ObjectId("5901a4c63541b7d5d3293766"),
        "firstName" : "shubham",
        "lastName" : "verma",
    
        "address" : {
            "address" : "Gurgaon"
        },
        "social" : {
            "fbURLs" : "http://www.facebook.com",
            "twitterURLs" : "http://www.twitter.com"
        }
    }
    
    3 field, (the default for unsharded output collections), the stage checks if the document in the aggregation results matches an existing document in the collection:

    • Khi Atlas Search tìm thấy một trận đấu (nghĩa là một tài liệu có cùng một tháng đã tồn tại trong bộ sưu tập), Atlas Search thay thế tài liệu hiện có bằng tài liệu từ kết quả tổng hợp như được chỉ định trong giai đoạn.match (that is, a document with the same year-month already exists in the collection), Atlas Search replaces the existing document with the document from the aggregation results as specified in the stage.

    • Khi Atlas Search không tìm thấy một trận đấu, Atlas Search sẽ chèn tài liệu từ kết quả tổng hợp vào bộ sưu tập như được chỉ định trong giai đoạn. Đây là hành vi mặc định khi không có khớp cho trường.match, Atlas Search inserts the document from the aggregation results into the collection as specified in the stage. This is the default behavior when there is no match for the field.

Bây giờ bạn đã hiểu cách thức hoạt động của chức năng UpdatemonthlySales, hãy tạo chức năng trong UI Dịch vụ ứng dụng:updateMonthlySales function works, create the function in the App Services UI:

Để xác định chức năng phía máy chủ mới từ giao diện người dùng, trước tiên bạn phải tạo ứng dụng Dịch vụ ứng dụng:

  1. Nếu bạn chưa làm như vậy, hãy nhấp vào tab Dịch vụ ứng dụng.App Services tab.

  2. Tạo ứng dụng:

    • Nếu bạn đang tạo ứng dụng Dịch vụ ứng dụng đầu tiên của mình trong dự án, bạn sẽ được hiển thị tùy chọn bắt đầu mà không cần mẫu (xây dựng ứng dụng của riêng bạn). Chọn tùy chọn xây dựng ứng dụng của riêng bạn.Build your own App). Select the Build your own App option.

    • Nếu bạn đã tạo ít nhất một ứng dụng dịch vụ ứng dụng trong dự án, hãy nhấp vào Tạo một ứng dụng mới.Create a New App.

  3. Trong trường Tên, nhập

    {
        "_id" : ObjectId("5901a4c63541b7d5d3293766"),
        "firstName" : "shubham",
        "lastName" : "verma",
    
        "address" : {
            "address" : "Gurgaon"
        },
        "social" : {
            "fbURLs" : "http://www.facebook.com",
            "twitterURLs" : "http://www.twitter.com"
        }
    }
    
    4 làm tên của hàm.Name field, enter
    {
        "_id" : ObjectId("5901a4c63541b7d5d3293766"),
        "firstName" : "shubham",
        "lastName" : "verma",
    
        "address" : {
            "address" : "Gurgaon"
        },
        "social" : {
            "fbURLs" : "http://www.facebook.com",
            "twitterURLs" : "http://www.twitter.com"
        }
    }
    
    4 as the name of the function.

  4. Trong trường liên kết của cơ sở dữ liệu của bạn, chọn sử dụng tùy chọn Nguồn dữ liệu MongoDB Atlas hiện có.Link your database field, select the Use an existing MongoDB Atlas Data Source option.

  5. Từ thả xuống, chọn cụm Atlas bạn đã tạo trong các điều kiện tiên quyết.

  6. Nhấp vào Tạo dịch vụ ứng dụng.Create App Service.

Để xác định chức năng phía máy chủ mới từ UI:

  1. Nhấp vào các chức năng trong menu điều hướng bên trái.Functions in the left navigation menu.

  2. Nhấp vào Tạo chức năng mới.Create New Function.

  3. Nhập

    {
        "_id" : ObjectId("5901b0f6d318b072ceea44fb"),
        "userId" : ObjectId("5901a4c63541b7d5d3293766"),
        "fbURLs" : "http://www.facebook.com",
        "twitterURLs" : "http://www.twitter.com"
    }
    
    0 dưới dạng tên của hàm.

  4. Theo xác thực, chọn hệ thống.Authentication, select System.

  1. Nhấp vào tab Trình chỉnh sửa chức năng.Function Editor tab.

  2. Thêm mã JavaScript vào hàm

    {
        "_id" : ObjectId("5901a4c63541b7d5d3293766"),
        "firstName" : "shubham",
        "lastName" : "verma",
    
        "address" : {
            "address" : "Gurgaon"
        },
        "social" : {
            "fbURLs" : "http://www.facebook.com",
            "twitterURLs" : "http://www.twitter.com"
        }
    }
    
    6. Tối thiểu, mã phải gán một hàm cho biến toàn cầu
    {
        "_id" : ObjectId("5901a4c63541b7d5d3293766"),
        "firstName" : "shubham",
        "lastName" : "verma",
    
        "address" : {
            "address" : "Gurgaon"
        },
        "social" : {
            "fbURLs" : "http://www.facebook.com",
            "twitterURLs" : "http://www.twitter.com"
        }
    }
    
    6:

    exports = function(){
    var pipeline = [
    { $match: {purchaseMethod: "Phone"} },
    { $unwind: {path: "$items"}},
    { $group: {
    _id: { $dateToString:{ format: "%Y-%m", date: "$saleDate" } },
    sales_quantity: { $sum: "$items.quantity"},
    sales_price: { $sum: "$items.price"}
    }
    },
    { $set: { sales_price: { $toDouble: "$sales_price"}}},
    { $merge: { into: "monthlysales", whenMatched: "replace" } }
    ]
    var monthlysales = context.services.get("mongodb-atlas").db("sample_supplies").collection("sales");
    return monthlysales.aggregate(pipeline);
    };

  3. Nhấp vào nút Run ở góc dưới bên phải của Trình chỉnh sửa chức năng để tạo chế độ xem

    {
        "_id" : ObjectId("5901b0f6d318b072ceea44fb"),
        "userId" : ObjectId("5901a4c63541b7d5d3293766"),
        "fbURLs" : "http://www.facebook.com",
        "twitterURLs" : "http://www.twitter.com"
    }
    
    1 được vật chất hóa.Run button in the lower right-hand corner of the Function Editor to create the
    {
        "_id" : ObjectId("5901b0f6d318b072ceea44fb"),
        "userId" : ObjectId("5901a4c63541b7d5d3293766"),
        "fbURLs" : "http://www.facebook.com",
        "twitterURLs" : "http://www.twitter.com"
    }
    
    1 materialized view.

  1. Mở

    {
        "_id" : ObjectId("5901a4c63541b7d5d3293766"),
        "firstName" : "shubham",
        "lastName" : "verma",
    
        "address" : {
            "address" : "Gurgaon"
        },
        "social" : {
            "fbURLs" : "http://www.facebook.com",
            "twitterURLs" : "http://www.twitter.com"
        }
    }
    
    9 trong cửa sổ thiết bị đầu cuối và kết nối với cụm của bạn. Để biết hướng dẫn chi tiết về kết nối, xem Connect qua
    {
        "_id" : ObjectId("5901a4c63541b7d5d3293766"),
        "firstName" : "shubham",
        "lastName" : "verma",
    
        "address" : {
            "address" : "Gurgaon"
        },
        "social" : {
            "fbURLs" : "http://www.facebook.com",
            "twitterURLs" : "http://www.twitter.com"
        }
    }
    
    9
    {
        "_id" : ObjectId("5901a4c63541b7d5d3293766"),
        "firstName" : "shubham",
        "lastName" : "verma",
    
        "address" : {
            "address" : "Gurgaon"
        },
        "social" : {
            "fbURLs" : "http://www.facebook.com",
            "twitterURLs" : "http://www.twitter.com"
        }
    }
    
    9
    in a terminal window and connect to your cluster. For detailed instructions on connecting, see Connect via
    {
        "_id" : ObjectId("5901a4c63541b7d5d3293766"),
        "firstName" : "shubham",
        "lastName" : "verma",
    
        "address" : {
            "address" : "Gurgaon"
        },
        "social" : {
            "fbURLs" : "http://www.facebook.com",
            "twitterURLs" : "http://www.twitter.com"
        }
    }
    
    9

  2. Sử dụng cơ sở dữ liệu

    {
                $project: {
                    __v: 0,
                    "address.__v": 0,
                    "address._id": 0,
                    "address.userId": 0,
                    "address.mob": 0
                }
            }
    
    {   
            $project: {      
                   "social.__v": 0,      
                   "social._id": 0,      
                   "social.userId": 0
               }
         }
    
    1:

  3. Truy vấn Bộ sưu tập

    {
                $project: {
                    __v: 0,
                    "address.__v": 0,
                    "address._id": 0,
                    "address.userId": 0,
                    "address.mob": 0
                }
            }
    
    {   
            $project: {      
                   "social.__v": 0,      
                   "social._id": 0,      
                   "social.userId": 0
               }
         }
    
    2. Lưu ý rằng việc bán hàng cuối cùng trong
    {
                $project: {
                    __v: 0,
                    "address.__v": 0,
                    "address._id": 0,
                    "address.userId": 0,
                    "address.mob": 0
                }
            }
    
    {   
            $project: {      
                   "social.__v": 0,      
                   "social._id": 0,      
                   "social.userId": 0
               }
         }
    
    2 xảy ra vào tháng 12 năm 2017:

    {
        "_id" : ObjectId("5901a5f83541b7d5d3293768"),
        "userId" : ObjectId("5901a4c63541b7d5d3293766"),
        "address" : "Gurgaon",
        "mob" : "9876543211"
    }
    
    0

  4. Xác nhận rằng chế độ xem được vật chất hóa đã được tạo trong cơ sở dữ liệu

    {
                $project: {
                    __v: 0,
                    "address.__v": 0,
                    "address._id": 0,
                    "address.userId": 0,
                    "address.mob": 0
                }
            }
    
    {   
            $project: {      
                   "social.__v": 0,      
                   "social._id": 0,      
                   "social.userId": 0
               }
         }
    
    1 của bạn:

    Lệnh liệt kê các bộ sưu tập của bạn, bao gồm chế độ xem

    {
        "_id" : ObjectId("5901b0f6d318b072ceea44fb"),
        "userId" : ObjectId("5901a4c63541b7d5d3293766"),
        "fbURLs" : "http://www.facebook.com",
        "twitterURLs" : "http://www.twitter.com"
    }
    
    1 mới được tạo ra.

  5. Truy vấn quan điểm cụ thể hóa

    {
        "_id" : ObjectId("5901b0f6d318b072ceea44fb"),
        "userId" : ObjectId("5901a4c63541b7d5d3293766"),
        "fbURLs" : "http://www.facebook.com",
        "twitterURLs" : "http://www.twitter.com"
    }
    
    1:

    {
        "_id" : ObjectId("5901a5f83541b7d5d3293768"),
        "userId" : ObjectId("5901a4c63541b7d5d3293766"),
        "address" : "Gurgaon",
        "mob" : "9876543211"
    }
    
    1

  6. Cập nhật bộ sưu tập

    {
                $project: {
                    __v: 0,
                    "address.__v": 0,
                    "address._id": 0,
                    "address.userId": 0,
                    "address.mob": 0
                }
            }
    
    {   
            $project: {      
                   "social.__v": 0,      
                   "social._id": 0,      
                   "social.userId": 0
               }
         }
    
    2 với dữ liệu bán hàng điện thoại mới từ tháng 1 năm 2018.

    {
        "_id" : ObjectId("5901a5f83541b7d5d3293768"),
        "userId" : ObjectId("5901a4c63541b7d5d3293766"),
        "address" : "Gurgaon",
        "mob" : "9876543211"
    }
    
    2

  7. Truy vấn Bộ sưu tập

    {
                $project: {
                    __v: 0,
                    "address.__v": 0,
                    "address._id": 0,
                    "address.userId": 0,
                    "address.mob": 0
                }
            }
    
    {   
            $project: {      
                   "social.__v": 0,      
                   "social._id": 0,      
                   "social.userId": 0
               }
         }
    
    2 một lần nữa để xác nhận các mục bán hàng mới. 2 kết quả truy vấn hàng đầu phản ánh rằng dữ liệu bán hàng hiện kết thúc vào tháng 1 năm 2018:

    {
        "_id" : ObjectId("5901a5f83541b7d5d3293768"),
        "userId" : ObjectId("5901a4c63541b7d5d3293766"),
        "address" : "Gurgaon",
        "mob" : "9876543211"
    }
    
    3

  1. Quay trở lại dự thảo chức năng của bạn trong giao diện người dùng Dịch vụ ứng dụng và nhấp vào nút Run ở góc dưới bên phải của Trình chỉnh sửa chức năng. Hàm

    {
        "_id" : ObjectId("5901b0f6d318b072ceea44fb"),
        "userId" : ObjectId("5901a4c63541b7d5d3293766"),
        "fbURLs" : "http://www.facebook.com",
        "twitterURLs" : "http://www.twitter.com"
    }
    
    0 làm mới chế độ xem
    {
        "_id" : ObjectId("5901b0f6d318b072ceea44fb"),
        "userId" : ObjectId("5901a4c63541b7d5d3293766"),
        "fbURLs" : "http://www.facebook.com",
        "twitterURLs" : "http://www.twitter.com"
    }
    
    1 được thực hiện bằng cách sử dụng dữ liệu bán hàng tháng 1 năm 2018.Run button in the lower right-hand corner of the Function Editor. The
    {
        "_id" : ObjectId("5901b0f6d318b072ceea44fb"),
        "userId" : ObjectId("5901a4c63541b7d5d3293766"),
        "fbURLs" : "http://www.facebook.com",
        "twitterURLs" : "http://www.twitter.com"
    }
    
    0 function refreshes the
    {
        "_id" : ObjectId("5901b0f6d318b072ceea44fb"),
        "userId" : ObjectId("5901a4c63541b7d5d3293766"),
        "fbURLs" : "http://www.facebook.com",
        "twitterURLs" : "http://www.twitter.com"
    }
    
    1 materialized view using the January 2018 sales data.

  2. Nhấp vào Lưu bản nháp từ tab Trình chỉnh sửa chức năng hoặc Cài đặt.Save Draft from either the Function Editor or Settings tab.

Quay trở lại

{
    "_id" : ObjectId("5901a4c63541b7d5d3293766"),
    "firstName" : "shubham",
    "lastName" : "verma",

    "address" : {
        "address" : "Gurgaon"
    },
    "social" : {
        "fbURLs" : "http://www.facebook.com",
        "twitterURLs" : "http://www.twitter.com"
    }
}
9 và truy vấn chế độ xem
{
    "_id" : ObjectId("5901b0f6d318b072ceea44fb"),
    "userId" : ObjectId("5901a4c63541b7d5d3293766"),
    "fbURLs" : "http://www.facebook.com",
    "twitterURLs" : "http://www.twitter.com"
}
1 được vật chất hóa để xác nhận bản cập nhật. Kết quả hàng đầu được trả về bởi truy vấn phản ánh dữ liệu
{
            $project: {
                __v: 0,
                "address.__v": 0,
                "address._id": 0,
                "address.userId": 0,
                "address.mob": 0
            }
        }

{   
        $project: {      
               "social.__v": 0,      
               "social._id": 0,      
               "social.userId": 0
           }
     }
2 được cập nhật cho tháng 1 năm 2018:
{
    "_id" : ObjectId("5901a4c63541b7d5d3293766"),
    "firstName" : "shubham",
    "lastName" : "verma",

    "address" : {
        "address" : "Gurgaon"
    },
    "social" : {
        "fbURLs" : "http://www.facebook.com",
        "twitterURLs" : "http://www.twitter.com"
    }
}
9
and query the
{
    "_id" : ObjectId("5901b0f6d318b072ceea44fb"),
    "userId" : ObjectId("5901a4c63541b7d5d3293766"),
    "fbURLs" : "http://www.facebook.com",
    "twitterURLs" : "http://www.twitter.com"
}
1 materialized view to confirm the update. The top result returned by the query reflects the updated
{
            $project: {
                __v: 0,
                "address.__v": 0,
                "address._id": 0,
                "address.userId": 0,
                "address.mob": 0
            }
        }

{   
        $project: {      
               "social.__v": 0,      
               "social._id": 0,      
               "social.userId": 0
           }
     }
2 data for January 2018:

{
    "_id" : ObjectId("5901a5f83541b7d5d3293768"),
    "userId" : ObjectId("5901a4c63541b7d5d3293766"),
    "address" : "Gurgaon",
    "mob" : "9876543211"
}
1

Lên lịch chức năng dịch vụ ứng dụng được tạo trong bước trước để chạy mỗi ngày một lần để giữ cho chế độ xem được vật chất hóa cập nhật.

  1. Đặt trường Loại kích hoạt thành lịch trình.Trigger Type field to Scheduled.

  2. Trong trường Tên, nhập

    {
        "_id" : ObjectId("5901b0f6d318b072ceea44fb"),
        "userId" : ObjectId("5901a4c63541b7d5d3293766"),
        "fbURLs" : "http://www.facebook.com",
        "twitterURLs" : "http://www.twitter.com"
    }
    
    0.Name field, enter
    {
        "_id" : ObjectId("5901b0f6d318b072ceea44fb"),
        "userId" : ObjectId("5901a4c63541b7d5d3293766"),
        "fbURLs" : "http://www.facebook.com",
        "twitterURLs" : "http://www.twitter.com"
    }
    
    0.

  3. Đặt trường Loại lịch trình thành BASIC.Schedule Type field to Basic.

  4. Trong lần lặp lại một lần bằng cách thả xuống, chọn

    {
        "_id" : ObjectId("5901a4c63541b7d5d3293766"),
        "firstName" : "shubham",
        "lastName" : "verma",
        "address" : {
            "_id" : ObjectId("5901a5f83541b7d5d3293768"),
            "userId" : ObjectId("5901a4c63541b7d5d3293766"),
            "address" : "Gurgaon",
            "mob" : "9876543211"
        },
        "social" : {
            "_id" : ObjectId("5901b0f6d318b072ceea44fb"),
            "userId" : ObjectId("5901a4c63541b7d5d3293766"),
            "fbURLs" : "http://www.facebook.com",
            "twitterURLs" : "http://www.twitter.com"
        }
    }
    
    5 và đặt giá trị thành ngày ưa thích của bạn.Repeat once by dropdown, select
    {
        "_id" : ObjectId("5901a4c63541b7d5d3293766"),
        "firstName" : "shubham",
        "lastName" : "verma",
        "address" : {
            "_id" : ObjectId("5901a5f83541b7d5d3293768"),
            "userId" : ObjectId("5901a4c63541b7d5d3293766"),
            "address" : "Gurgaon",
            "mob" : "9876543211"
        },
        "social" : {
            "_id" : ObjectId("5901b0f6d318b072ceea44fb"),
            "userId" : ObjectId("5901a4c63541b7d5d3293766"),
            "fbURLs" : "http://www.facebook.com",
            "twitterURLs" : "http://www.twitter.com"
        }
    }
    
    5 and set the value to your preferred date.

    Ghi chú

    Ngoài ra, cho mục đích thử nghiệm, hãy đặt lặp lại một lần bằng cách thả xuống đến xảy ra thường xuyên hơn, chẳng hạn như phút hoặc giờRepeat once by dropdown to a more frequent occurrence, such as Minute or Hour

  5. Đặt trường Chọn loại sự kiện thành hàm.Select An Event Type field to Function.

  6. Trong chức năng thả xuống, chọn

    {
        "_id" : ObjectId("5901b0f6d318b072ceea44fb"),
        "userId" : ObjectId("5901a4c63541b7d5d3293766"),
        "fbURLs" : "http://www.facebook.com",
        "twitterURLs" : "http://www.twitter.com"
    }
    
    0.Function dropdown, select
    {
        "_id" : ObjectId("5901b0f6d318b072ceea44fb"),
        "userId" : ObjectId("5901a4c63541b7d5d3293766"),
        "fbURLs" : "http://www.facebook.com",
        "twitterURLs" : "http://www.twitter.com"
    }
    
    0.

Tạo một chỉ mục tìm kiếm ATLAS trên bộ sưu tập

{
    "_id" : ObjectId("5901a4c63541b7d5d3293766"),
    "firstName" : "shubham",
    "lastName" : "verma",
    "address" : {
        "_id" : ObjectId("5901a5f83541b7d5d3293768"),
        "userId" : ObjectId("5901a4c63541b7d5d3293766"),
        "address" : "Gurgaon",
        "mob" : "9876543211"
    },
    "social" : {
        "_id" : ObjectId("5901b0f6d318b072ceea44fb"),
        "userId" : ObjectId("5901a4c63541b7d5d3293766"),
        "fbURLs" : "http://www.facebook.com",
        "twitterURLs" : "http://www.twitter.com"
    }
}
7.

Nhấp vào cơ sở dữ liệu ở góc trên cùng bên trái của Atlas để điều hướng đến trang triển khai cơ sở dữ liệu cho dự án của bạn.Databases in the top-left corner of Atlas to navigate to the Database Deployments page for your project.

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

    {
        "_id" : ObjectId("5901a4c63541b7d5d3293766"),
        "firstName" : "shubham",
        "lastName" : "verma",
        "address" : {
            "_id" : ObjectId("5901a5f83541b7d5d3293768"),
            "userId" : ObjectId("5901a4c63541b7d5d3293766"),
            "address" : "Gurgaon",
            "mob" : "9876543211"
        },
        "social" : {
            "_id" : ObjectId("5901b0f6d318b072ceea44fb"),
            "userId" : ObjectId("5901a4c63541b7d5d3293766"),
            "fbURLs" : "http://www.facebook.com",
            "twitterURLs" : "http://www.twitter.com"
        }
    }
    
    8.Index Name field, enter
    {
        "_id" : ObjectId("5901a4c63541b7d5d3293766"),
        "firstName" : "shubham",
        "lastName" : "verma",
        "address" : {
            "_id" : ObjectId("5901a5f83541b7d5d3293768"),
            "userId" : ObjectId("5901a4c63541b7d5d3293766"),
            "address" : "Gurgaon",
            "mob" : "9876543211"
        },
        "social" : {
            "_id" : ObjectId("5901b0f6d318b072ceea44fb"),
            "userId" : ObjectId("5901a4c63541b7d5d3293766"),
            "fbURLs" : "http://www.facebook.com",
            "twitterURLs" : "http://www.twitter.com"
        }
    }
    
    8.

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

    {
                $project: {
                    __v: 0,
                    "address.__v": 0,
                    "address._id": 0,
                    "address.userId": 0,
                    "address.mob": 0
                }
            }
    
    {   
            $project: {      
                   "social.__v": 0,      
                   "social._id": 0,      
                   "social.userId": 0
               }
         }
    
    1 và chọn bộ sưu tập
    {
        "_id" : ObjectId("5901b0f6d318b072ceea44fb"),
        "userId" : ObjectId("5901a4c63541b7d5d3293766"),
        "fbURLs" : "http://www.facebook.com",
        "twitterURLs" : "http://www.twitter.com"
    }
    
    1.Database and Collection section, find the
    {
                $project: {
                    __v: 0,
                    "address.__v": 0,
                    "address._id": 0,
                    "address.userId": 0,
                    "address.mob": 0
                }
            }
    
    {   
            $project: {      
                   "social.__v": 0,      
                   "social._id": 0,      
                   "social.userId": 0
               }
         }
    
    1 database, and select the
    {
        "_id" : ObjectId("5901b0f6d318b072ceea44fb"),
        "userId" : ObjectId("5901a4c63541b7d5d3293766"),
        "fbURLs" : "http://www.facebook.com",
        "twitterURLs" : "http://www.twitter.com"
    }
    
    1 collection.

  3. Nhấn tiếp.Next.

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.Close button.

Chỉ mục mới được tạo xuất hiện trên tab Tìm kiếm. Trong khi chỉ số đang xây dựng, trường trạng thái đọc được xây dựng trong tiến trình. Khi chỉ mục được xây dựng xong, trường trạng thái đọc hoạt động.Search tab. While the index is building, the Status field reads Build in Progress. When the index is finished building, the Status field reads Active.

Ghi chú

Ngoài ra, cho mục đích thử nghiệm, hãy đặt lặp lại một lần bằng cách thả xuống đến xảy ra thường xuyên hơn, chẳng hạn như phút hoặc giờ

Đặt trường Chọn loại sự kiện thành hàm.

Trong chức năng thả xuống, chọn

{
    "_id" : ObjectId("5901b0f6d318b072ceea44fb"),
    "userId" : ObjectId("5901a4c63541b7d5d3293766"),
    "fbURLs" : "http://www.facebook.com",
    "twitterURLs" : "http://www.twitter.com"
}
0.
{
    "_id" : ObjectId("5901a4c63541b7d5d3293766"),
    "firstName" : "shubham",
    "lastName" : "verma",

    "address" : {
        "address" : "Gurgaon"
    },
    "social" : {
        "fbURLs" : "http://www.facebook.com",
        "twitterURLs" : "http://www.twitter.com"
    }
}
9
prompt:

Tạo một chỉ mục tìm kiếm ATLAS trên bộ sưu tập

{
    "_id" : ObjectId("5901a4c63541b7d5d3293766"),
    "firstName" : "shubham",
    "lastName" : "verma",
    "address" : {
        "_id" : ObjectId("5901a5f83541b7d5d3293768"),
        "userId" : ObjectId("5901a4c63541b7d5d3293766"),
        "address" : "Gurgaon",
        "mob" : "9876543211"
    },
    "social" : {
        "_id" : ObjectId("5901b0f6d318b072ceea44fb"),
        "userId" : ObjectId("5901a4c63541b7d5d3293766"),
        "fbURLs" : "http://www.facebook.com",
        "twitterURLs" : "http://www.twitter.com"
    }
}
7.

{
    "_id" : ObjectId("5901a5f83541b7d5d3293768"),
    "userId" : ObjectId("5901a4c63541b7d5d3293766"),
    "address" : "Gurgaon",
    "mob" : "9876543211"
}
5

Nhấp vào cơ sở dữ liệu ở góc trên cùng bên trái của Atlas để điều hướng đến trang triển khai cơ sở dữ liệu cho dự án của bạn.

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

{
    "_id" : ObjectId("5901a4c63541b7d5d3293766"),
    "firstName" : "shubham",
    "lastName" : "verma",
    "address" : {
        "_id" : ObjectId("5901a5f83541b7d5d3293768"),
        "userId" : ObjectId("5901a4c63541b7d5d3293766"),
        "address" : "Gurgaon",
        "mob" : "9876543211"
    },
    "social" : {
        "_id" : ObjectId("5901b0f6d318b072ceea44fb"),
        "userId" : ObjectId("5901a4c63541b7d5d3293766"),
        "fbURLs" : "http://www.facebook.com",
        "twitterURLs" : "http://www.twitter.com"
    }
}
8.MongoDB Server Manual.

Tôi có thể truy vấn nhiều bộ sưu tập trong MongoDB không?

Tập hợp - Như nghĩa đen cho thấy nó liên quan đến việc kết hợp nhiều thứ khác nhau, tương tự trong tập hợp MongoDB là một kỹ thuật để truy vấn dữ liệu từ nhiều bộ sưu tập bằng cách nhóm hoặc tham gia chúng theo sau bằng cách thực hiện nhiều hoạt động khác nhau (được giải thích sau trong tài liệu này) và sau đó trả về kết quả được tính toán .

MongoDB có thể mô hình nhiều cho nhiều người không?

Một mối quan hệ nhiều người nhiều (N: M) vì không có lệnh nào để thực hiện mối quan hệ nhiều đến nhiều trong cơ sở dữ liệu quan hệ, khó khăn hơn một mối quan hệ một-nhiều.Điều tương tự cũng đúng khi sử dụng MongoDB để thực hiện chúng.Trên thực tế, bạn không thể sử dụng một lệnh để tạo bất kỳ loại mối quan hệ nào trong MongoDB.you can't use a command to create any type of relationship in MongoDB.

Bạn sẽ sử dụng phương pháp nào để tìm nhiều dữ liệu từ MongoDB?

Bạn có thể truy vấn cho nhiều tài liệu trong một bộ sưu tập với comcand.find ().Phương thức Find () sử dụng tài liệu truy vấn mà bạn cung cấp để khớp với tập hợp con của các tài liệu trong bộ sưu tập phù hợp với truy vấn.collection.find() . The find() method uses a query document that you provide to match the subset of the documents in the collection that match the query.

Làm cách nào để truy vấn nhiều giá trị trong MongoDB?

MongoDB cung cấp Find () được sử dụng để tìm nhiều giá trị hoặc tài liệu từ bộ sưu tập.Phương thức Find () trả về một con trỏ của tập kết quả và in tất cả các tài liệu.Để tìm nhiều giá trị, chúng ta có thể sử dụng các hoạt động tổng hợp được cung cấp bởi chính MongoDB.use the aggregation operations that are provided by MongoDB itself.