Liệt kê trong mongodb
Ghi chú. Mongoose sẽ tự động chuyển tên của bộ sưu tập thành chữ thường và chuyển sang nhiều dạng số. Ví dụ Người dùng -> người dùng Thao tác dữ liệuTạo tệp cầy mangut. người mẫu. js tại dự án gốc. Khởi tạo UserSchema và UserModel trong tệp mongoose.model.js. // File mongoose.model.js import mongoose from 'mongoose'; // connect to MongoDB mongoose.connect('mongodb://localhost/gettingstarted', {useNewUrlParser: true}); const UserSchema = mongoose.Schema({ email: String, password: String, fullname: String, gender: { type: String, enum: ['male', 'female', 'other'] }, createdAt: { type: Date, default: Date.now }, note: String }); const UserModel = mongoose.model('User', UserSchema); Tạo tài liệuMột thể hiện của mô hình được gọi là tài liệu. Sử dụng phương thức save() để lưu tài liệu xuống cơ sở dữ liệu MongoDB. // File mongoose.model.js // .. more code // Tạo User từ UserModel const createUser = () => { const newUser = new UserModel({ email: "[email protected]", password: '123456', fullname: 'Deft Blog', gender: "male" }); try { newUser.save(); } catch(err) { console.log(err); } } createUser(); Cửa sổ. . \node_modules\. bin\babel-nút. \cầy mangut. người mẫu. js MacOS. . /node_modules/. cầy mangut bin/babel-node. người mẫu. js Ghi chú. Sẽ không có Người dùng mới nào được lưu hoặc xóa cho đến khi kết nối của mô hình được thiết lập. The default model of you will use the default connection of cầy mangut, mongoose.connect('mongodb://localhost/gettingstarted', {useNewUrlParser: true}); Nếu bạn muốn khởi tạo một kết nối tùy chỉnh, chúng ta sẽ sử dụng model() của kết nối tùy chỉnh mà chúng ta vừa tạo ra. var connection = mongoose.createConnection('mongodb://localhost:27017/test'); var UserModel = connection.model('User', UserSchema); Truy vấnMongoose hỗ trợ rất nhiều hàm truy vấn dữ liệu, có thể kể đến như tìm , . Trong khuôn khổ bài này chúng ta sẽ không đi sâu và truy vấn dữ liệu. Mình sẽ demo cách tìm , findOne etc. Trong khuôn khổ bài này chúng ta sẽ không đi sâu và truy vấn dữ liệu. Mình sẽ demo cách tìm User theo email. // File mongoose.model.js // .. more code const findUser = async () => { try { const userFind = await UserModel.findOne({email: "[email protected]"}); console.log("Find user:" + userFind); } catch(err) { console.log(err); } } findUser();
Cập nhậtMỗi mô hình đều có phương thức updateOne, update v.v. . dữ liệu trong collection tương ứng. // File mongoose.model.js // .. more code const updateUser = async () => { try { await UserModel.updateOne({email: "[email protected]"}, {password: "abczyz"}); const userFind = await UserModel.findOne({email: "[email protected]"}); console.log("Update user: " + userFind); } catch(err) { console.log(err); } } updateUser();
xóaMô hình trong cầy mangut có sẵn các phương thức tĩnh deleteOne , deleteMany dùng để xóa tất cả các document khớp với condition. // File mongoose.model.js // .. more code // delete user const deleteUser = async () => { try { await UserModel.deleteOne({email: "[email protected]"}); const userFind = await UserModel.findOne({email: "[email protected]"}); console.log("Delete user: " + userFind); } catch(err) { console.log(err); } } deleteUser();
Kết quảỞ các phần trên chúng tôi đã tìm hiểu được cách khởi động một Mô hình từ Schema, . tạo và sao lưu document xuống database, truy vấn dữ liệu etc. Để đảm bảo rằng các bạn có thể chạy thử nghiệm mình đã đẩy mã lên gitlab. Các bạn có thể download code về và chạy thử xem kết quả. Ở đầu file mình có hướng dẫn cụ thể mà các bước chạy cho đúng để được kết quả mong muốn các bạn nhớ đón đọc |