Visual Studio Code có hỗ trợ tuyệt vời để làm việc với cơ sở dữ liệu MongoDB, dù là phiên bản của riêng bạn hay trong Azure với MongoDB Atlas. Với tiện ích mở rộng MongoDB cho Mã VS, bạn có thể tạo, quản lý và truy vấn cơ sở dữ liệu MongoDB từ bên trong Mã VS
Cài đặt tiện ích mở rộng
Hỗ trợ MongoDB cho Mã VS được cung cấp bởi tiện ích mở rộng MongoDB cho Mã VS. Để cài đặt tiện ích mở rộng MongoDB cho Mã VS, hãy mở chế độ xem Tiện ích mở rộng bằng cách nhấn ⇧⌘X [Windows, Linux Ctrl . Chọn tiện ích mở rộng MongoDB cho Mã VS. ] and search for 'MongoDB' to filter the results. Select the MongoDB for VS Code extension.
Kết nối với MongoDB
Khi bạn đã cài đặt tiện ích mở rộng MongoDB cho Mã VS, bạn sẽ nhận thấy có một chế độ xem Thanh hoạt động MongoDB mới. Chọn chế độ xem MongoDB và bạn sẽ thấy MongoDB Explorer
Để kết nối với cơ sở dữ liệu MongoDB, chọn Thêm kết nối và nhập chi tiết kết nối cho cơ sở dữ liệu rồi Kết nối, mặc định là máy chủ MongoDB cục bộ tại
db.orders.insertMany[ [ { _id: 1, item: "apple", qty: "5", price: 10 }, { _id: 2, item: "pie", qty: "10", price: NumberDecimal["20.0"] }, { _id: 3, item: "ice cream", qty: "2", price: "4.99" }, { _id: 4, item: "almonds" , qty: "5", price: 5 }] ]
1. Bạn cũng có thể nhập chuỗi kết nối, nhấp vào liên kết "kết nối với chuỗi kết nối" và dán chuỗi kết nốiGhi chú. Đảm bảo máy chủ MongoDB của bạn [mongod. exe] đang chạy nếu bạn đang kết nối với máy chủ MongoDB cục bộ
Sau khi đính kèm, bạn có thể làm việc với máy chủ MongoDB, quản lý Cơ sở dữ liệu, Bộ sưu tập và Tài liệu MongoDB
Bạn có thể mở rộng cơ sở dữ liệu để xem các bộ sưu tập của chúng với lược đồ và chỉ mục của chúng và bạn có thể chọn các Tài liệu MongoDB riêng lẻ để xem JSON của chúng
Bạn cũng có thể đính kèm trình bao MongoDB vào kết nối đang hoạt động, chỉ bằng cách nhấp chuột phải vào chính kết nối đó
Ghi chú. Đảm bảo trình bao MongoDB [_______22 hoặc
3] đã được cài đặt và nằm trên đường dẫn của bạn. Trong cài đặt của tiện ích mở rộng, bạn có thể chọn trình bao bạn đang sử dụngdb.orders.insertMany[ [ { _id: 1, item: "apple", qty: "5", price: 10 }, { _id: 2, item: "pie", qty: "10", price: NumberDecimal["20.0"] }, { _id: 3, item: "ice cream", qty: "2", price: "4.99" }, { _id: 4, item: "almonds" , qty: "5", price: 5 }] ]
Lệnh MongoDB
Có các lệnh cụ thể của MongoDB có sẵn trong VS Code Command Palette [ ⇧⌘P [Windows, Linux Ctrl+Shift+P]] as well as through Explorer context menus.
Sử dụng sân chơi
Một trong những tính năng mạnh mẽ nhất của tích hợp VS Code MongoDB là Mongo Playgrounds. Sân chơi cho phép bạn tạo, chạy và lưu các lệnh MongoDB từ trình chỉnh sửa VS Code. Tạo một sân chơi mới với MongoDB. Tạo lệnh MongoDB Playground
Trong một sân chơi, bạn có thể tham khảo các thực thể và lệnh MongoDB và bạn nhận được IntelliSense phong phú khi bạn nhập. Sân chơi rất hữu ích cho các hoạt động và truy vấn cơ sở dữ liệu nguyên mẫu. Thực thi các dòng đã chọn trong truy vấn sân chơi với MongoDB. Chạy lệnh Selected Lines From Playground
MongoDB trên Azure
Bạn có thể dễ dàng tạo cụm MongoDB trên Azure miễn phí với MongoDB Atlas
Chọn Tạo cụm mới từ bảng điều khiển và chọn Azure làm Nhà cung cấp đám mây. Khi cụm được tạo, hãy kết nối với chuỗi kết nối do MongoDB Atlas cung cấp
Bạn không thể chuyển đổi một giá trị kép có giá trị bị cắt bớt nhỏ hơn giá trị số nguyên tối thiểu hoặc lớn hơn giá trị số nguyên tối đa
Số thập phân
Trả về giá trị bị cắt bớt
Giá trị thập phân bị cắt ngắn phải nằm trong giá trị tối thiểu và tối đa cho một số nguyên
Bạn không thể chuyển đổi giá trị thập phân có giá trị bị cắt bớt nhỏ hơn giá trị số nguyên tối thiểu hoặc lớn hơn giá trị số nguyên tối đa
số nguyên
Không ra đâu. Trả về giá trị số nguyên
Dài
Trả về giá trị dài dưới dạng số nguyên
Giá trị dài phải nằm trong giá trị tối thiểu và tối đa cho một số nguyên
Bạn không thể chuyển đổi một giá trị dài nhỏ hơn giá trị số nguyên tối thiểu hoặc lớn hơn giá trị số nguyên tối đa
Sợi dây
Trả về giá trị số của chuỗi dưới dạng số nguyên
Giá trị chuỗi phải là số nguyên cơ số 10; . g.
db.orders.insertMany[ [ { _id: 1, item: "apple", qty: "5", price: 10 }, { _id: 2, item: "pie", qty: "10", price: NumberDecimal["20.0"] }, { _id: 3, item: "ice cream", qty: "2", price: "4.99" }, { _id: 4, item: "almonds" , qty: "5", price: 5 }] ]
5, db.orders.insertMany[ [ { _id: 1, item: "apple", qty: "5", price: 10 }, { _id: 2, item: "pie", qty: "10", price: NumberDecimal["20.0"] }, { _id: 3, item: "ice cream", qty: "2", price: "4.99" }, { _id: 4, item: "almonds" , qty: "5", price: 5 }] ]
6]Bạn không thể chuyển đổi giá trị chuỗi của số thực hoặc số thập phân hoặc số không phải cơ số 10 [e. g.
db.orders.insertMany[ [ { _id: 1, item: "apple", qty: "5", price: 10 }, { _id: 2, item: "pie", qty: "10", price: NumberDecimal["20.0"] }, { _id: 3, item: "ice cream", qty: "2", price: "4.99" }, { _id: 4, item: "almonds" , qty: "5", price: 5 }] ]
7, db.orders.insertMany[ [ { _id: 1, item: "apple", qty: "5", price: 10 }, { _id: 2, item: "pie", qty: "10", price: NumberDecimal["20.0"] }, { _id: 3, item: "ice cream", qty: "2", price: "4.99" }, { _id: 4, item: "almonds" , qty: "5", price: 5 }] ]
8]Bảng sau đây liệt kê một số ví dụ chuyển đổi sang số nguyên
Thí dụ
Kết quả
9db.orders.insertMany[ [ { _id: 1, item: "apple", qty: "5", price: 10 }, { _id: 2, item: "pie", qty: "10", price: NumberDecimal["20.0"] }, { _id: 3, item: "ice cream", qty: "2", price: "4.99" }, { _id: 4, item: "almonds" , qty: "5", price: 5 }] ]
1
0// Define stage to add convertedPrice and convertedQty fields with the converted price and qty valuespriceQtyConversionStage = { $addFields: { convertedPrice: { $toDecimal: "$price" }, convertedQty: { $toInt: "$qty" }, }};// Define stage to calculate total price by multiplying convertedPrice and convertedQty fieldstotalPriceCalculationStage = { $project: { item: 1, totalPrice: { $multiply: [ "$convertedPrice", "$convertedQty" ] } }};db.orders.aggregate[ [ priceQtyConversionStage, totalPriceCalculationStage] ]
0
1// Define stage to add convertedPrice and convertedQty fields with the converted price and qty valuespriceQtyConversionStage = { $addFields: { convertedPrice: { $toDecimal: "$price" }, convertedQty: { $toInt: "$qty" }, }};// Define stage to calculate total price by multiplying convertedPrice and convertedQty fieldstotalPriceCalculationStage = { $project: { item: 1, totalPrice: { $multiply: [ "$convertedPrice", "$convertedQty" ] } }};db.orders.aggregate[ [ priceQtyConversionStage, totalPriceCalculationStage] ]
1
2// Define stage to add convertedPrice and convertedQty fields with the converted price and qty valuespriceQtyConversionStage = { $addFields: { convertedPrice: { $toDecimal: "$price" }, convertedQty: { $toInt: "$qty" }, }};// Define stage to calculate total price by multiplying convertedPrice and convertedQty fieldstotalPriceCalculationStage = { $project: { item: 1, totalPrice: { $multiply: [ "$convertedPrice", "$convertedQty" ] } }};db.orders.aggregate[ [ priceQtyConversionStage, totalPriceCalculationStage] ]
5
3// Define stage to add convertedPrice and convertedQty fields with the converted price and qty valuespriceQtyConversionStage = { $addFields: { convertedPrice: { $toDecimal: "$price" }, convertedQty: { $toInt: "$qty" }, }};// Define stage to calculate total price by multiplying convertedPrice and convertedQty fieldstotalPriceCalculationStage = { $project: { item: 1, totalPrice: { $multiply: [ "$convertedPrice", "$convertedQty" ] } }};db.orders.aggregate[ [ priceQtyConversionStage, totalPriceCalculationStage] ]
Lỗi
4// Define stage to add convertedPrice and convertedQty fields with the converted price and qty valuespriceQtyConversionStage = { $addFields: { convertedPrice: { $toDecimal: "$price" }, convertedQty: { $toInt: "$qty" }, }};// Define stage to calculate total price by multiplying convertedPrice and convertedQty fieldstotalPriceCalculationStage = { $project: { item: 1, totalPrice: { $multiply: [ "$convertedPrice", "$convertedQty" ] } }};db.orders.aggregate[ [ priceQtyConversionStage, totalPriceCalculationStage] ]
5000
5// Define stage to add convertedPrice and convertedQty fields with the converted price and qty valuespriceQtyConversionStage = { $addFields: { convertedPrice: { $toDecimal: "$price" }, convertedQty: { $toInt: "$qty" }, }};// Define stage to calculate total price by multiplying convertedPrice and convertedQty fieldstotalPriceCalculationStage = { $project: { item: 1, totalPrice: { $multiply: [ "$convertedPrice", "$convertedQty" ] } }};db.orders.aggregate[ [ priceQtyConversionStage, totalPriceCalculationStage] ]
Lỗi
6// Define stage to add convertedPrice and convertedQty fields with the converted price and qty valuespriceQtyConversionStage = { $addFields: { convertedPrice: { $toDecimal: "$price" }, convertedQty: { $toInt: "$qty" }, }};// Define stage to calculate total price by multiplying convertedPrice and convertedQty fieldstotalPriceCalculationStage = { $project: { item: 1, totalPrice: { $multiply: [ "$convertedPrice", "$convertedQty" ] } }};db.orders.aggregate[ [ priceQtyConversionStage, totalPriceCalculationStage] ]
-2
7// Define stage to add convertedPrice and convertedQty fields with the converted price and qty valuespriceQtyConversionStage = { $addFields: { convertedPrice: { $toDecimal: "$price" }, convertedQty: { $toInt: "$qty" }, }};// Define stage to calculate total price by multiplying convertedPrice and convertedQty fieldstotalPriceCalculationStage = { $project: { item: 1, totalPrice: { $multiply: [ "$convertedPrice", "$convertedQty" ] } }};db.orders.aggregate[ [ priceQtyConversionStage, totalPriceCalculationStage] ]
Lỗi
8// Define stage to add convertedPrice and convertedQty fields with the converted price and qty valuespriceQtyConversionStage = { $addFields: { convertedPrice: { $toDecimal: "$price" }, convertedQty: { $toInt: "$qty" }, }};// Define stage to calculate total price by multiplying convertedPrice and convertedQty fieldstotalPriceCalculationStage = { $project: { item: 1, totalPrice: { $multiply: [ "$convertedPrice", "$convertedQty" ] } }};db.orders.aggregate[ [ priceQtyConversionStage, totalPriceCalculationStage] ]
vô giá trị
Tạo bộ sưu tập
// Define stage to add convertedPrice and convertedQty fields with the converted price and qty valuespriceQtyConversionStage = { $addFields: { convertedPrice: { $toDecimal: "$price" }, convertedQty: { $toInt: "$qty" }, }};// Define stage to calculate total price by multiplying convertedPrice and convertedQty fieldstotalPriceCalculationStage = { $project: { item: 1, totalPrice: { $multiply: [ "$convertedPrice", "$convertedQty" ] } }};db.orders.aggregate[ [ priceQtyConversionStage, totalPriceCalculationStage] ]
9 với các tài liệu saudb.orders.insertMany[ [ { _id: 1, item: "apple", qty: "5", price: 10 }, { _id: 2, item: "pie", qty: "10", price: NumberDecimal["20.0"] }, { _id: 3, item: "ice cream", qty: "2", price: "4.99" }, { _id: 4, item: "almonds" , qty: "5", price: 5 }] ]
Hoạt động tổng hợp sau đây
chuyển đổi
60 thành một số nguyên,db.orders.insertMany[ [ { _id: 1, item: "apple", qty: "5", price: 10 }, { _id: 2, item: "pie", qty: "10", price: NumberDecimal["20.0"] }, { _id: 3, item: "ice cream", qty: "2", price: "4.99" }, { _id: 4, item: "almonds" , qty: "5", price: 5 }] ]
chuyển đổi
61 thành số thập phân,db.orders.insertMany[ [ { _id: 1, item: "apple", qty: "5", price: 10 }, { _id: 2, item: "pie", qty: "10", price: NumberDecimal["20.0"] }, { _id: 3, item: "ice cream", qty: "2", price: "4.99" }, { _id: 4, item: "almonds" , qty: "5", price: 5 }] ]
tính tổng giá
// Define stage to add convertedPrice and convertedQty fields with the converted price and qty valuespriceQtyConversionStage = { $addFields: { convertedPrice: { $toDecimal: "$price" }, convertedQty: { $toInt: "$qty" }, }};// Define stage to calculate total price by multiplying convertedPrice and convertedQty fieldstotalPriceCalculationStage = { $project: { item: 1, totalPrice: { $multiply: [ "$convertedPrice", "$convertedQty" ] } }};db.orders.aggregate[ [ priceQtyConversionStage, totalPriceCalculationStage] ]
Hoạt động trả về các tài liệu sau
db.orders.insertMany[ [ { _id: 1, item: "apple", qty: "5", price: 10 }, { _id: 2, item: "pie", qty: "10", price: NumberDecimal["20.0"] }, { _id: 3, item: "ice cream", qty: "2", price: "4.99" }, { _id: 4, item: "almonds" , qty: "5", price: 5 }] ]
6Ghi chú
Nếu thao tác chuyển đổi gặp lỗi, thao tác tổng hợp sẽ dừng và đưa ra lỗi. Để ghi đè hành vi này, thay vào đó hãy sử dụng
db.orders.insertMany[ [ { _id: 1, item: "apple", qty: "5", price: 10 }, { _id: 2, item: "pie", qty: "10", price: NumberDecimal["20.0"] }, { _id: 3, item: "ice cream", qty: "2", price: "4.99" }, { _id: 4, item: "almonds" , qty: "5", price: 5 }] ]
0Làm cách nào để thay đổi kiểu dữ liệu của một cột trong MongoDB?
Bạn có thể thay đổi loại dữ liệu của trường bằng cách sử dụng bộ chọn loại dữ liệu ở bên phải trường trong Cụm đám mây Atlas cũng như MongoDB Compass