Hướng dẫn fetch data from mongodb atlas - tìm nạp dữ liệu từ tập bản đồ mongodb

DOCS Home → Dịch vụ ứng dụng AtlasAtlas App Services

Trên trang này

  • Tổng quan
  • Mô hình dữ liệu
  • Thiết lập đoạn trích
  • Phương pháp truy vấn
  • Tìm một tài liệu duy nhất (
    const query = { "quantity": { "$gte": 25 } };
    const projection = {
    "title": 1,
    "quantity": 1,
    }
    return itemsCollection.findOne(query, projection)
    .then(result => {
    if(result) {
    console.log(`Successfully found document: ${result}.`);
    } else {
    console.log("No document matches the provided query.");
    }
    return result;
    })
    .catch(err => console.error(`Failed to find document: ${err}`));
    7)
  • Tìm một hoặc nhiều tài liệu (
    const query = { "quantity": { "$gte": 25 } };
    const projection = {
    "title": 1,
    "quantity": 1,
    }
    return itemsCollection.findOne(query, projection)
    .then(result => {
    if(result) {
    console.log(`Successfully found document: ${result}.`);
    } else {
    console.log("No document matches the provided query.");
    }
    return result;
    })
    .catch(err => console.error(`Failed to find document: ${err}`));
    8)
  • Đếm tài liệu trong bộ sưu tập (
    const query = { "quantity": { "$gte": 25 } };
    const projection = {
    "title": 1,
    "quantity": 1,
    }
    return itemsCollection.findOne(query, projection)
    .then(result => {
    if(result) {
    console.log(`Successfully found document: ${result}.`);
    } else {
    console.log("No document matches the provided query.");
    }
    return result;
    })
    .catch(err => console.error(`Failed to find document: ${err}`));
    9)
  • Mẫu truy vấn
  • Tìm bởi ID tài liệu
  • Tìm theo ngày
  • Khớp một trường cấp độ gốc
  • Khớp nhiều trường
  • Khớp một trường tài liệu nhúng
  • Khớp với một mảng các giá trị
  • Khớp với một phần tử mảng
  • Các nhà khai thác truy vấn
  • So sánh các giá trị
  • Đánh giá một biểu thức logic
  • Đánh giá một biểu thức chính quy

Các ví dụ trên trang này trình bày cách sử dụng API truy vấn MongoDB trong một hàm để đọc các tài liệu từ cụm Atlas của bạn.

Tìm hiểu về các phương thức mà bạn có thể gọi đến dữ liệu truy vấn, các nhà khai thác cho phép bạn viết các bộ lọc phù hợp biểu cảm và một số mẫu để kết hợp chúng để xử lý các trường hợp sử dụng phổ biến.methods that you can call to query data, the operators that let you write expressive match filters, and some patterns for combining them to handle common use cases.

Các ví dụ trên trang này sử dụng một bộ sưu tập có tên

const query = { "reviews.0": { "$exists": true } };
const projection = { "_id": 0 };
return itemsCollection.find(query, projection)
.sort({ name: 1 })
.toArray()
.then(items => {
console.log(`Successfully found ${items.length} documents.`)
items.forEach(console.log)
return items
})
.catch(err => console.error(`Failed to find documents: ${err}`))
0 mô hình các mặt hàng khác nhau có sẵn để mua trong một cửa hàng trực tuyến. Mỗi mục có
const query = { "reviews.0": { "$exists": true } };
const projection = { "_id": 0 };
return itemsCollection.find(query, projection)
.sort({ name: 1 })
.toArray()
.then(items => {
console.log(`Successfully found ${items.length} documents.`)
items.forEach(console.log)
return items
})
.catch(err => console.error(`Failed to find documents: ${err}`))
1, hàng tồn kho
const query = { "reviews.0": { "$exists": true } };
const projection = { "_id": 0 };
return itemsCollection.find(query, projection)
.sort({ name: 1 })
.toArray()
.then(items => {
console.log(`Successfully found ${items.length} documents.`)
items.forEach(console.log)
return items
})
.catch(err => console.error(`Failed to find documents: ${err}`))
2 và một loạt khách hàng
const query = { "reviews.0": { "$exists": true } };
const projection = { "_id": 0 };
return itemsCollection.find(query, projection)
.sort({ name: 1 })
.toArray()
.then(items => {
console.log(`Successfully found ${items.length} documents.`)
items.forEach(console.log)
return items
})
.catch(err => console.error(`Failed to find documents: ${err}`))
3.

Lược đồ JSON cho Store.Items

{
"title": "Item",
"required": ["_id", "name", "quantity", "reviews"],
"properties": {
"_id": { "bsonType": "objectId" },
"name": { "bsonType": "string" },
"quantity": { "bsonType": "int" },
"reviews": {
"bsonType": "array",
"items": {
"bsonType": "object",
"required": ["username", "comment"],
"properties": {
"username": { "bsonType": "string" },
"comment": { "bsonType": "string" }
}
}
}
}
}

Để sử dụng đoạn mã trong một hàm, trước tiên bạn phải khởi tạo một tay cầm bộ sưu tập MongoDB:

exports = function() {
const mongodb = context.services.get("mongodb-atlas");
const itemsCollection = mongodb.db("store").collection("items");
const purchasesCollection = mongodb.db("store").collection("purchases");
// ... paste snippet here ...
}

Bạn có thể tìm thấy một tài liệu duy nhất bằng phương pháp

const query = { "reviews.0": { "$exists": true } };
const projection = { "_id": 0 };
return itemsCollection.find(query, projection)
.sort({ name: 1 })
.toArray()
.then(items => {
console.log(`Successfully found ${items.length} documents.`)
items.forEach(console.log)
return items
})
.catch(err => console.error(`Failed to find documents: ${err}`))
4.

Đoạn trích chức năng sau đây tìm thấy một tài liệu duy nhất từ ​​bộ sưu tập

const query = { "reviews.0": { "$exists": true } };
const projection = { "_id": 0 };
return itemsCollection.find(query, projection)
.sort({ name: 1 })
.toArray()
.then(items => {
console.log(`Successfully found ${items.length} documents.`)
items.forEach(console.log)
return items
})
.catch(err => console.error(`Failed to find documents: ${err}`))
5 có
const query = { "reviews.0": { "$exists": true } };
const projection = { "_id": 0 };
return itemsCollection.find(query, projection)
.sort({ name: 1 })
.toArray()
.then(items => {
console.log(`Successfully found ${items.length} documents.`)
items.forEach(console.log)
return items
})
.catch(err => console.error(`Failed to find documents: ${err}`))
2 lớn hơn hoặc bằng
const query = { "reviews.0": { "$exists": true } };
const projection = { "_id": 0 };
return itemsCollection.find(query, projection)
.sort({ name: 1 })
.toArray()
.then(items => {
console.log(`Successfully found ${items.length} documents.`)
items.forEach(console.log)
return items
})
.catch(err => console.error(`Failed to find documents: ${err}`))
7:

const query = { "quantity": { "$gte": 25 } };
const projection = {
"title": 1,
"quantity": 1,
}
return itemsCollection.findOne(query, projection)
.then(result => {
if(result) {
console.log(`Successfully found document: ${result}.`);
} else {
console.log("No document matches the provided query.");
}
return result;
})
.catch(err => console.error(`Failed to find document: ${err}`));

Bạn có thể tìm thấy nhiều tài liệu bằng phương pháp

const query = { "reviews.0": { "$exists": true } };
const projection = { "_id": 0 };
return itemsCollection.find(query, projection)
.sort({ name: 1 })
.toArray()
.then(items => {
console.log(`Successfully found ${items.length} documents.`)
items.forEach(console.log)
return items
})
.catch(err => console.error(`Failed to find documents: ${err}`))
8.

Đoạn trích chức năng sau đây tìm thấy tất cả các tài liệu trong bộ sưu tập

const query = { "reviews.0": { "$exists": true } };
const projection = { "_id": 0 };
return itemsCollection.find(query, projection)
.sort({ name: 1 })
.toArray()
.then(items => {
console.log(`Successfully found ${items.length} documents.`)
items.forEach(console.log)
return items
})
.catch(err => console.error(`Failed to find documents: ${err}`))
5 có ít nhất một đánh giá và trả về chúng được sắp xếp bởi
const query = { "reviews.0": { "$exists": true } };
const projection = { "_id": 0 };
return itemsCollection.find(query, projection)
.sort({ name: 1 })
.toArray()
.then(items => {
console.log(`Successfully found ${items.length} documents.`)
items.forEach(console.log)
return items
})
.catch(err => console.error(`Failed to find documents: ${err}`))
1 với trường
return itemsCollection.count({ "reviews.0": { "$exists": true } })
.then(numDocs => console.log(`${numDocs} items have a review.`))
.catch(err => console.error("Failed to count documents: ", err))
1 bị bỏ qua:

const query = { "reviews.0": { "$exists": true } };
const projection = { "_id": 0 };
return itemsCollection.find(query, projection)
.sort({ name: 1 })
.toArray()
.then(items => {
console.log(`Successfully found ${items.length} documents.`)
items.forEach(console.log)
return items
})
.catch(err => console.error(`Failed to find documents: ${err}`))

Bạn có thể đếm các tài liệu trong một bộ sưu tập bằng phương pháp

return itemsCollection.count({ "reviews.0": { "$exists": true } })
.then(numDocs => console.log(`${numDocs} items have a review.`))
.catch(err => console.error("Failed to count documents: ", err))
2. Bạn có thể chỉ định một truy vấn để kiểm soát tài liệu nào để đếm. Nếu bạn không chỉ định truy vấn, phương thức sẽ tính tất cả các tài liệu trong bộ sưu tập.

Đoạn trích chức năng sau đây đếm số lượng tài liệu trong bộ sưu tập

const query = { "reviews.0": { "$exists": true } };
const projection = { "_id": 0 };
return itemsCollection.find(query, projection)
.sort({ name: 1 })
.toArray()
.then(items => {
console.log(`Successfully found ${items.length} documents.`)
items.forEach(console.log)
return items
})
.catch(err => console.error(`Failed to find documents: ${err}`))
5 có ít nhất một đánh giá:

return itemsCollection.count({ "reviews.0": { "$exists": true } })
.then(numDocs => console.log(`${numDocs} items have a review.`))
.catch(err => console.error("Failed to count documents: ", err))

Bạn có thể truy vấn một bộ sưu tập để tìm một tài liệu có ID được chỉ định. MongoDB tự động lưu trữ ID của mỗi tài liệu dưới dạng giá trị

return itemsCollection.count({ "reviews.0": { "$exists": true } })
.then(numDocs => console.log(`${numDocs} items have a review.`))
.catch(err => console.error("Failed to count documents: ", err))
4 trong trường
return itemsCollection.count({ "reviews.0": { "$exists": true } })
.then(numDocs => console.log(`${numDocs} items have a review.`))
.catch(err => console.error("Failed to count documents: ", err))
1 của tài liệu.

Thí dụ

Truy vấn sau phù hợp với một tài liệu trong bộ sưu tập có giá trị

return itemsCollection.count({ "reviews.0": { "$exists": true } })
.then(numDocs => console.log(`${numDocs} items have a review.`))
.catch(err => console.error("Failed to count documents: ", err))
1 là
return itemsCollection.count({ "reviews.0": { "$exists": true } })
.then(numDocs => console.log(`${numDocs} items have a review.`))
.catch(err => console.error("Failed to count documents: ", err))
7:

{ "_id": BSON.ObjectId("5ad84b81b8b998278f773c1b") }

Bạn có thể truy vấn một bộ sưu tập để tìm các tài liệu có trường có giá trị ngày cụ thể hoặc truy vấn cho một tài liệu trong một phạm vi ngày.

{ "": <Date | Expression> }

Thí dụ

Truy vấn sau phù hợp với một tài liệu trong bộ sưu tập có giá trị

return itemsCollection.count({ "reviews.0": { "$exists": true } })
.then(numDocs => console.log(`${numDocs} items have a review.`))
.catch(err => console.error("Failed to count documents: ", err))
1 là
return itemsCollection.count({ "reviews.0": { "$exists": true } })
.then(numDocs => console.log(`${numDocs} items have a review.`))
.catch(err => console.error("Failed to count documents: ", err))
7:

{ "createdAt": new Date("2019-01-23T05:00:00.000Z") }

Thí dụ

Truy vấn sau phù hợp với một tài liệu trong bộ sưu tập có giá trị

return itemsCollection.count({ "reviews.0": { "$exists": true } })
.then(numDocs => console.log(`${numDocs} items have a review.`))
.catch(err => console.error("Failed to count documents: ", err))
1 là
return itemsCollection.count({ "reviews.0": { "$exists": true } })
.then(numDocs => console.log(`${numDocs} items have a review.`))
.catch(err => console.error("Failed to count documents: ", err))
7:

{
"createdAt": {
"$gte": new Date("2019-01-01T00:00:00.000Z"),
"$lt": new Date("2020-01-01T00:00:00.000Z"),
}
}

Bạn có thể truy vấn một bộ sưu tập để tìm các tài liệu có trường có giá trị ngày cụ thể hoặc truy vấn cho một tài liệu trong một phạm vi ngày.

Các truy vấn sau đây phù hợp với các tài liệu trong bộ sưu tập có ngày

return itemsCollection.count({ "reviews.0": { "$exists": true } })
.then(numDocs => console.log(`${numDocs} items have a review.`))
.catch(err => console.error("Failed to count documents: ", err))
8 ngày 23 tháng 1 năm 2019:Query Documents tutorial in the MongoDB Server Manual.

Các truy vấn sau đây phù hợp với các tài liệu trong bộ sưu tập có ngày

return itemsCollection.count({ "reviews.0": { "$exists": true } })
.then(numDocs => console.log(`${numDocs} items have a review.`))
.catch(err => console.error("Failed to count documents: ", err))
8 một thời gian trong năm 2019:

Thí dụ

Truy vấn sau phù hợp với một tài liệu trong bộ sưu tập có giá trị

return itemsCollection.count({ "reviews.0": { "$exists": true } })
.then(numDocs => console.log(`${numDocs} items have a review.`))
.catch(err => console.error("Failed to count documents: ", err))
1 là
return itemsCollection.count({ "reviews.0": { "$exists": true } })
.then(numDocs => console.log(`${numDocs} items have a review.`))
.catch(err => console.error("Failed to count documents: ", err))
7:

Bạn có thể truy vấn một bộ sưu tập để tìm các tài liệu có trường có giá trị ngày cụ thể hoặc truy vấn cho một tài liệu trong một phạm vi ngày.

Các truy vấn sau đây phù hợp với các tài liệu trong bộ sưu tập có ngày

return itemsCollection.count({ "reviews.0": { "$exists": true } })
.then(numDocs => console.log(`${numDocs} items have a review.`))
.catch(err => console.error("Failed to count documents: ", err))
8 ngày 23 tháng 1 năm 2019:Query on Embedded/Nested Documents tutorial in the MongoDB Server Manual.

exports = function() {
const mongodb = context.services.get("mongodb-atlas");
const itemsCollection = mongodb.db("store").collection("items");
const purchasesCollection = mongodb.db("store").collection("purchases");
// ... paste snippet here ...
}
0

Thí dụ

Truy vấn sau phù hợp với một tài liệu trong bộ sưu tập có giá trị

return itemsCollection.count({ "reviews.0": { "$exists": true } })
.then(numDocs => console.log(`${numDocs} items have a review.`))
.catch(err => console.error("Failed to count documents: ", err))
1 là
return itemsCollection.count({ "reviews.0": { "$exists": true } })
.then(numDocs => console.log(`${numDocs} items have a review.`))
.catch(err => console.error("Failed to count documents: ", err))
7:

exports = function() {
const mongodb = context.services.get("mongodb-atlas");
const itemsCollection = mongodb.db("store").collection("items");
const purchasesCollection = mongodb.db("store").collection("purchases");
// ... paste snippet here ...
}
1

Bạn có thể truy vấn một bộ sưu tập để tìm các tài liệu có trường có giá trị ngày cụ thể hoặc truy vấn cho một tài liệu trong một phạm vi ngày.dot notation.

Các truy vấn sau đây phù hợp với các tài liệu trong bộ sưu tập có ngày

return itemsCollection.count({ "reviews.0": { "$exists": true } })
.then(numDocs => console.log(`${numDocs} items have a review.`))
.catch(err => console.error("Failed to count documents: ", err))
8 ngày 23 tháng 1 năm 2019:Query on Embedded/Nested Documents tutorial in the MongoDB Server Manual.

exports = function() {
const mongodb = context.services.get("mongodb-atlas");
const itemsCollection = mongodb.db("store").collection("items");
const purchasesCollection = mongodb.db("store").collection("purchases");
// ... paste snippet here ...
}
2

exports = function() {
const mongodb = context.services.get("mongodb-atlas");
const itemsCollection = mongodb.db("store").collection("items");
const purchasesCollection = mongodb.db("store").collection("purchases");
// ... paste snippet here ...
}
3

Thí dụ

Truy vấn sau phù hợp với một tài liệu trong bộ sưu tập có giá trị

return itemsCollection.count({ "reviews.0": { "$exists": true } })
.then(numDocs => console.log(`${numDocs} items have a review.`))
.catch(err => console.error("Failed to count documents: ", err))
1 là
return itemsCollection.count({ "reviews.0": { "$exists": true } })
.then(numDocs => console.log(`${numDocs} items have a review.`))
.catch(err => console.error("Failed to count documents: ", err))
7:

exports = function() {
const mongodb = context.services.get("mongodb-atlas");
const itemsCollection = mongodb.db("store").collection("items");
const purchasesCollection = mongodb.db("store").collection("purchases");
// ... paste snippet here ...
}
4

Bạn có thể truy vấn một bộ sưu tập để tìm các tài liệu có trường có giá trị ngày cụ thể hoặc truy vấn cho một tài liệu trong một phạm vi ngày.

Các truy vấn sau đây phù hợp với các tài liệu trong bộ sưu tập có ngày

return itemsCollection.count({ "reviews.0": { "$exists": true } })
.then(numDocs => console.log(`${numDocs} items have a review.`))
.catch(err => console.error("Failed to count documents: ", err))
8 ngày 23 tháng 1 năm 2019:$all operator.

Các truy vấn sau đây phù hợp với các tài liệu trong bộ sưu tập có ngày

return itemsCollection.count({ "reviews.0": { "$exists": true } })
.then(numDocs => console.log(`${numDocs} items have a review.`))
.catch(err => console.error("Failed to count documents: ", err))
8 một thời gian trong năm 2019:Query an Array tutorial in the MongoDB Server Manual.

exports = function() {
const mongodb = context.services.get("mongodb-atlas");
const itemsCollection = mongodb.db("store").collection("items");
const purchasesCollection = mongodb.db("store").collection("purchases");
// ... paste snippet here ...
}
5

Thí dụ

Truy vấn sau phù hợp với một tài liệu trong bộ sưu tập có giá trị

return itemsCollection.count({ "reviews.0": { "$exists": true } })
.then(numDocs => console.log(`${numDocs} items have a review.`))
.catch(err => console.error("Failed to count documents: ", err))
1 là
return itemsCollection.count({ "reviews.0": { "$exists": true } })
.then(numDocs => console.log(`${numDocs} items have a review.`))
.catch(err => console.error("Failed to count documents: ", err))
7:

exports = function() {
const mongodb = context.services.get("mongodb-atlas");
const itemsCollection = mongodb.db("store").collection("items");
const purchasesCollection = mongodb.db("store").collection("purchases");
// ... paste snippet here ...
}
6

Thí dụ

Truy vấn sau phù hợp với các tài liệu trong đó mảng

const query = { "reviews.0": { "$exists": true } };
const projection = { "_id": 0 };
return itemsCollection.find(query, projection)
.sort({ name: 1 })
.toArray()
.then(items => {
console.log(`Successfully found ${items.length} documents.`)
items.forEach(console.log)
return items
})
.catch(err => console.error(`Failed to find documents: ${err}`))
3 chứa một hoặc nhiều phần tử khớp với tất cả các tài liệu được chỉ định:

exports = function() {
const mongodb = context.services.get("mongodb-atlas");
const itemsCollection = mongodb.db("store").collection("items");
const purchasesCollection = mongodb.db("store").collection("purchases");
// ... paste snippet here ...
}
7

Bạn có thể truy vấn một bộ sưu tập dựa trên giá trị của một hoặc nhiều phần tử trong trường mảng.

Nếu bạn truy vấn một trường mảng có biểu thức truy vấn có nhiều điều kiện, MongoDB sẽ trả về các tài liệu trong đó bất kỳ sự kết hợp nào của các yếu tố của mảng đều thỏa mãn biểu thức. Nếu bạn muốn MongoDB trả về các tài liệu trong đó một phần tử mảng duy nhất thỏa mãn tất cả các điều kiện biểu thức, hãy sử dụng toán tử $ elemmatch.$elemMatch operator.

Để biết thêm thông tin, hãy xem Truy vấn hướng dẫn mảng trong Hướng dẫn sử dụng máy chủ MongoDB.Query an Array tutorial in the MongoDB Server Manual.

exports = function() {
const mongodb = context.services.get("mongodb-atlas");
const itemsCollection = mongodb.db("store").collection("items");
const purchasesCollection = mongodb.db("store").collection("purchases");
// ... paste snippet here ...
}
8

Thí dụ

Truy vấn sau đây phù hợp với các tài liệu trong đó cả hai điều kiện trong biểu thức nhúng được đáp ứng bởi bất kỳ sự kết hợp nào của các phần tử trong mảng

const query = { "reviews.0": { "$exists": true } };
const projection = { "_id": 0 };
return itemsCollection.find(query, projection)
.sort({ name: 1 })
.toArray()
.then(items => {
console.log(`Successfully found ${items.length} documents.`)
items.forEach(console.log)
return items
})
.catch(err => console.error(`Failed to find documents: ${err}`))
3. Các giá trị

{ "": <Date | Expression> }

0 và

{ "": <Date | Expression> }

1 được chỉ định không cần phải có trong cùng một tài liệu:

exports = function() {
const mongodb = context.services.get("mongodb-atlas");
const itemsCollection = mongodb.db("store").collection("items");
const purchasesCollection = mongodb.db("store").collection("purchases");
// ... paste snippet here ...
}
9

Thí dụ

Truy vấn sau phù hợp với các tài liệu trong đó cả hai điều kiện trong biểu thức nhúng được đáp ứng bởi một phần tử duy nhất trong mảng

const query = { "reviews.0": { "$exists": true } };
const projection = { "_id": 0 };
return itemsCollection.find(query, projection)
.sort({ name: 1 })
.toArray()
.then(items => {
console.log(`Successfully found ${items.length} documents.`)
items.forEach(console.log)
return items
})
.catch(err => console.error(`Failed to find documents: ${err}`))
3.

{ "": <Date | Expression> }

0 và

{ "": <Date | Expression> }

1 được chỉ định phải nằm trong cùng một tài liệu:

const query = { "quantity": { "$gte": 25 } };
const projection = {
"title": 1,
"quantity": 1,
}
return itemsCollection.findOne(query, projection)
.then(result => {
if(result) {
console.log(`Successfully found document: ${result}.`);
} else {
console.log("No document matches the provided query.");
}
return result;
})
.catch(err => console.error(`Failed to find document: ${err}`));
0

Bạn có thể sử dụng toán tử so sánh để so sánh giá trị của trường tài liệu với giá trị khác.comparison operator to compare the value of a document field to another value.

const query = { "quantity": { "$gte": 25 } };
const projection = {
"title": 1,
"quantity": 1,
}
return itemsCollection.findOne(query, projection)
.then(result => {
if(result) {
console.log(`Successfully found document: ${result}.`);
} else {
console.log("No document matches the provided query.");
}
return result;
})
.catch(err => console.error(`Failed to find document: ${err}`));
1

Các toán tử so sánh sau đây có sẵn:

Nhà điều hành so sánh

Sự mô tả

$eq

Khớp với các tài liệu trong đó giá trị của một trường bằng một giá trị được chỉ định.

$ne

Khớp với các tài liệu trong đó giá trị của một trường không bằng một giá trị được chỉ định.

$gt

Khớp với các tài liệu trong đó giá trị của một trường lớn hơn một giá trị được chỉ định.

$gte

Khớp với các tài liệu trong đó giá trị của một trường lớn hơn hoặc bằng một giá trị được chỉ định.

$lt

Khớp với các tài liệu trong đó giá trị của một trường hoàn toàn nhỏ hơn một giá trị được chỉ định.

$lte

Khớp với các tài liệu trong đó giá trị của trường nhỏ hơn hoặc bằng với giá trị được chỉ định.

$in

Khớp với các tài liệu trong đó giá trị của một trường được bao gồm trong một mảng giá trị được chỉ định.

$nin

Khớp với các tài liệu trong đó giá trị của một trường không được bao gồm trong một mảng giá trị được chỉ định.

Thí dụ

Truy vấn sau phù hợp với các tài liệu trong đó

const query = { "reviews.0": { "$exists": true } };
const projection = { "_id": 0 };
return itemsCollection.find(query, projection)
.sort({ name: 1 })
.toArray()
.then(items => {
console.log(`Successfully found ${items.length} documents.`)
items.forEach(console.log)
return items
})
.catch(err => console.error(`Failed to find documents: ${err}`))
2 lớn hơn 0 và nhỏ hơn hoặc bằng mười.

const query = { "quantity": { "$gte": 25 } };
const projection = {
"title": 1,
"quantity": 1,
}
return itemsCollection.findOne(query, projection)
.then(result => {
if(result) {
console.log(`Successfully found document: ${result}.`);
} else {
console.log("No document matches the provided query.");
}
return result;
})
.catch(err => console.error(`Failed to find document: ${err}`));
2

Bạn có thể sử dụng toán tử logic để đánh giá nhiều biểu thức cho một trường duy nhất.logical operator to evaluate multiple expressions for a single field.

const query = { "quantity": { "$gte": 25 } };
const projection = {
"title": 1,
"quantity": 1,
}
return itemsCollection.findOne(query, projection)
.then(result => {
if(result) {
console.log(`Successfully found document: ${result}.`);
} else {
console.log("No document matches the provided query.");
}
return result;
})
.catch(err => console.error(`Failed to find document: ${err}`));
3

Các toán tử logic sau đây có sẵn:

Toán tử logic

Sự mô tả

$and

Khớp với các tài liệu trong đó giá trị của một trường bằng một giá trị được chỉ định.

$or

Khớp với các tài liệu trong đó giá trị của một trường không bằng một giá trị được chỉ định.

$nor

Khớp với các tài liệu trong đó giá trị của một trường lớn hơn một giá trị được chỉ định.

$not

Khớp với các tài liệu trong đó giá trị của một trường lớn hơn hoặc bằng một giá trị được chỉ định.

Thí dụ

Khớp với các tài liệu trong đó giá trị của một trường hoàn toàn nhỏ hơn một giá trị được chỉ định.

const query = { "quantity": { "$gte": 25 } };
const projection = {
"title": 1,
"quantity": 1,
}
return itemsCollection.findOne(query, projection)
.then(result => {
if(result) {
console.log(`Successfully found document: ${result}.`);
} else {
console.log("No document matches the provided query.");
}
return result;
})
.catch(err => console.error(`Failed to find document: ${err}`));
4

Khớp với các tài liệu trong đó giá trị của trường nhỏ hơn hoặc bằng với giá trị được chỉ định.$regex query operator to return documents with fields that match a regular expression. To avoid ambiguity with the

{ "": <Date | Expression> }

8 EJSON type, you must use a BSON.BSONRegExp object.

const query = { "quantity": { "$gte": 25 } };
const projection = {
"title": 1,
"quantity": 1,
}
return itemsCollection.findOne(query, projection)
.then(result => {
if(result) {
console.log(`Successfully found document: ${result}.`);
} else {
console.log("No document matches the provided query.");
}
return result;
})
.catch(err => console.error(`Failed to find document: ${err}`));
5

Thí dụ

Khớp với các tài liệu trong đó giá trị của một trường được bao gồm trong một mảng giá trị được chỉ định.

const query = { "quantity": { "$gte": 25 } };
const projection = {
"title": 1,
"quantity": 1,
}
return itemsCollection.findOne(query, projection)
.then(result => {
if(result) {
console.log(`Successfully found document: ${result}.`);
} else {
console.log("No document matches the provided query.");
}
return result;
})
.catch(err => console.error(`Failed to find document: ${err}`));
6

Làm thế nào tìm nạp dữ liệu từ MongoDB?

Bạn có thể sử dụng các hoạt động đọc để truy xuất dữ liệu từ cơ sở dữ liệu MongoDB của bạn. Có nhiều loại hoạt động đọc truy cập dữ liệu theo các cách khác nhau. Nếu bạn muốn yêu cầu kết quả dựa trên một tập hợp các tiêu chí từ bộ dữ liệu hiện có, bạn có thể sử dụng một thao tác tìm kiếm như các phương thức Find () hoặc findOne ().use read operations to retrieve data from your MongoDB database. There are multiple types of read operations that access the data in different ways. If you want to request results based on a set of criteria from the existing set of data, you can use a find operation such as the find() or findOne() methods.

Làm thế nào để tôi truy vấn trong Atlas MongoDB?

Trong tab Tập hợp La bàn MongoDB, theo cách thủ công đường ống tổng hợp của bạn ...
Một cụm Atlas chạy MongoDB phiên bản 4.2 trở lên ..
Chỉ số tìm kiếm Atlas trên bộ sưu tập của bạn trong cụm Atlas ..
Kết nối với máy khách bạn muốn sử dụng để chạy truy vấn ..

Làm cách nào để xuất dữ liệu từ MongoDB Atlas?

Chỉ cần cài đặt Compass Mongo Kết nối với Atlas Remote DB của bạn: Nhận tên máy chủ như "Cluster0-shard-00-00-rcapo.mongodb.net xxxxx" từ cụm altas từ xa của bạn sau đó kết nối với cơ sở dữ liệu.Sau đó, bạn có thể tải xuống từng tài liệu dưới dạng định dạng JSON hoặc CSV.install mongo compass connect to your atlas remote DB: get the hostname like "cluster0-shard-00-00-rcapo.mongodb.net XXXXX" from your remote altas cluster then connect to the database. then you can download each document as JSON or CSV format.

API dữ liệu trong Atlas MongoDB là gì?

API dữ liệu ATLAS MongoDB cho phép bạn đọc và ghi dữ liệu trong ATLAS với các yêu cầu HTTPS tiêu chuẩn.Để sử dụng API dữ liệu, tất cả những gì bạn cần là máy khách HTTPS và khóa API hợp lệ.Khách hàng gửi các yêu cầu đến các điểm cuối cụ thể, mỗi điểm đại diện cho một hoạt động MongoDB.lets you read and write data in Atlas with standard HTTPS requests. To use the Data API, all you need is an HTTPS client and a valid API key. Clients send requests to specific endpoints, which each represent a MongoDB operation.