Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> MongoDB

Truy vấn collection MongoDB bắt đầu bằng dấu gạch dưới (_)?

Tạo collection tên bắt đầu bằng _ cần dùng createCollection() và getCollection():

db.createCollection('_yourCollectionName');
db.getCollection('_yourCollectionName').insertOne({"yourFieldName1":"yourValue1","yourFieldName2":yourValue2});

Ví dụ:

> db.createCollection('_testUnderscoreCollectionDemo');
{ "ok" : 1 }
>db.getCollection('_testUnderscoreCollectionDemo').insertOne({"StudentFirstName":"John","StudentAge":23});
{ "acknowledged" : true, "insertedId" : ObjectId("5ccfb4a6140b992277dae0e4") }
>db.getCollection('_testUnderscoreCollectionDemo').insertOne({"StudentFirstName":"Carol","StudentAge":21});
{ "acknowledged" : true, "insertedId" : ObjectId("5ccfb4af140b992277dae0e5") }

Truy vấn document:

> db.getCollection('_testUnderscoreCollectionDemo').find().pretty();

Kết quả: