Node.js: Tìm dữ liệu trong bộ sưu tập (FindOne và FindAll)

Các khóa học qua video:
Python SQL Server PHP C# Lập trình C Java HTML5-CSS3-JavaScript
Học trên YouTube <76K/tháng. Đăng ký Hội viên
Viết nhanh hơn - Học tốt hơn
Giải phóng thời gian, khai phóng năng lực

Tìm một dữ liệu phù hợp với yêu cầu trong Collection

Để chọn dữ liệu từ một tập hợp trong MongoDB, chúng ta có thể sử dụng phương thức findOne().

Phương thức findOne() trả về dữ liệu đầu tiên trong mảng kết quả.

const { MongoClient } = require('mongodb');
const url = "mongodb://localhost:27017/";

MongoClient.connect(url, function(err, client) {
  if (err) throw err;
  const db = client.db("mydb");
  db.collection("customers").findOne({}, function(err, result) {
    if (err) throw err;
    console.log(result);
    client.close();
  });
});

 Với Mongoose:

const mongoose = require('mongoose');
const url = "mongodb://localhost:27017/mydb";
const schema = new mongoose.Schema({name: "string", address: "string"})
const userinfo = mongoose.model("userinfo", schema, "customers")

mongoose.connect(url, function(err) {
  if (err) throw err;
  userinfo.findOne({}, function(err, result){
    if (err) throw err;
    console.log(result)
    mongoose.connection.close()
  })
});

Tìm tất cả dữ liệu phù hợp với yêu cầu trong Collection

Để chọn dữ liệu từ một bảng trong MongoDB, chúng ta cũng có thể sử dụng phương thức find ().

Phương thức find() trả về tất cả các lần xuất hiện trong vùng chọn.

const MongoClient } = require('mongodb');
const url = "mongodb://localhost:27017/";

MongoClient.connect(url, function(err, client) {
  if (err) throw err;
  const db = client.db("mydb");
  db.collection("customers").find({}).toArray(function(err, result) {
    if (err) throw err;
    console.log(result);
    client.close();
  });
});

Với mongoose

const mongoose = require('mongoose');
const url = "mongodb://localhost:27017/mydb";
const schema = new mongoose.Schema({name: "string", address: "string"})
const userinfo = mongoose.model("userinfo", schema, "customers")

mongoose.connect(url, function(err) {
  if (err) throw err;
  userinfo.find({}, function(err, result){
    if (err) throw err;
    console.log(result)
    mongoose.connection.close()
  })
});

Kết quả:

[
  {
    _id: new ObjectId("630058ca24477bbbc95cf5eb"),
    name: 'John',
    address: 'Highway 71'
  },
  {
    _id: new ObjectId("630058ca24477bbbc95cf5ec"),
    name: 'Peter',
    address: 'Lowstreet 4'
  },
  {
    _id: new ObjectId("630058ca24477bbbc95cf5ed"),
    name: 'Amy',
    address: 'Apple st 652'
  },
  {
    _id: new ObjectId("630058ca24477bbbc95cf5ee"),
    name: 'Hannah',
    address: 'Mountain 21'
  }
]

Xử lý dữ liệu trả về của find()

Tham số thứ hai của phương thức find() là đối tượng chiếu mô tả những trường nào cần đưa vào kết quả (không bắt buộc).

const MongoClient } = require('mongodb');
const url = "mongodb://localhost:27017/";

MongoClient.connect(url, function(err, client) {
  if (err) throw err;
  const db = client.db("mydb");
  db.collection("customers").find({}, { projection: { _id: 0, name: 1, address: 1 } }
).toArray(function(err, result) {
    if (err) throw err;
    console.log(result);
    client.close();
  });
});

Với Mongoose

const mongoose = require('mongoose');
const url = "mongodb://localhost:27017/mydb";
const schema = new mongoose.Schema({name: "string", address: "string"})
const userinfo = mongoose.model("userinfo", schema, "customers")

mongoose.connect(url, function(err) {
  if (err) throw err;
  userinfo.find({}, { _id: 0, name: 1, address: 1 }, function(err, result){
    if (err) throw err;
    console.log(result)
    mongoose.connection.close()
  })
});

Kết quả:

[
  { name: 'John', address: 'Highway 71' },    
  { name: 'Peter', address: 'Lowstreet 4' },  
  { name: 'Amy', address: 'Apple st 652' },   
  { name: 'Hannah', address: 'Mountain 21' }, 

» Tiếp: Truy vấn dữ liệu trong bộ sưu tập (Query)
« Trước: Chèn dữ liệu vào bộ sưu tập (InsertOne và InsertMany)
Các khóa học qua video:
Python SQL Server PHP C# Lập trình C Java HTML5-CSS3-JavaScript
Học trên YouTube <76K/tháng. Đăng ký Hội viên
Viết nhanh hơn - Học tốt hơn
Giải phóng thời gian, khai phóng năng lực
Copied !!!