Xử lý collection có tên trùng từ khóa dành riêng
Khi tên collection trùng với từ khóa dành riêng của MongoDB (như "version"), bạn không thể truy cập trực tiếp qua db.version. Giải pháp là dùng db.getCollection().
Tạo và truy cập collection "version"
> db.createCollection('version');
{ "ok": 1 }
Thêm document
> db.getCollection('version').insertOne({"VersionName": "1.0"});
> db.getCollection('version').insertOne({"VersionName": "1.1"});
> db.getCollection('version').insertOne({"VersionName": "1.2"});
Truy vấn dữ liệu
> db.getCollection('version').find();
{ "_id": ObjectId("..."), "VersionName": "1.0" }
{ "_id": ObjectId("..."), "VersionName": "1.1" }
{ "_id": ObjectId("..."), "VersionName": "1.2" }
Mẹo: Cách này áp dụng cho bất kỳ tên collection nào chứa ký tự đặc biệt hoặc trùng từ khóa: db.getCollection('tên-collection').