Sử dụng find() để lấy dữ liệu, pretty() để định dạng đẹp. Tạo collection phức tạp:
> db.getSpecificData.insertOne({
"StudentName": "John",
"Information": {
"FatherName": "Chris",
"Place": { "CountryName": "US", "ZipCode": "111344" },
"id": "1"
}
});
{ "acknowledged" : true, "insertedId" : ObjectId("5e039abdf5e889d7a5199509") }
> db.getSpecificData.insertOne({
"StudentName": "Carol",
"Information": {
"FatherName": "Robert",
"Place": { "CountryName": "UK", "ZipCode": "746464" },
"id": "2"
}
});
{ "acknowledged" : true, "insertedId" : ObjectId("5e039ae6f5e889d7a519950a") }
> db.getSpecificData.insertOne({
"StudentName": "David",
"Information": {
"FatherName": "Carol",
"Place": { "CountryName": "US", "ZipCode": "567334" },
"id": "3"
}
});
{ "acknowledged" : true, "insertedId" : ObjectId("5e039ae7f5e889d7a519950b") }
> db.getSpecificData.insertOne({
"StudentName": "Jace",
"Information": {
"FatherName": "Bob",
"Place": { "CountryName": "US", "ZipCode": "999999" },
"id": "4"
}
});
{ "acknowledged" : true, "insertedId" : ObjectId("5e039ae8f5e889d7a519950c") }
Truy vấn tìm CountryName="US" với limit 2, dùng pretty():
> db.getSpecificData.find({'Information.Place.CountryName':"US"}, {}, {limit: 2}).pretty();
Kết quả định dạng đẹp (3 tài liệu vì limit không hoạt động với callback):
Truy vấn đúng cú pháp limit(2) (không dùng callback):
> db.getSpecificData.find({'Information.Place.CountryName':"US"}).limit(2);Kết quả compact (2 tài liệu):
Lưu ý:
pretty()chỉ tác động hiển thị trong shell.limit()phải gọi trên cursor, không dùng trong object tùy chọn thứ 3 của find() (đó là options cho cursor.toArray).