예, findOne()을 사용할 수 있습니다. 다음은 구문입니다 -
db.yourCollectionName.findOne();
toArray()도 사용할 수 있습니다 -
db.yourCollectionName.find().toArray();
먼저 문서로 컬렉션을 만들어 보겠습니다. −
> db.betterFormatDemo.insertOne({"StudentName":"Adam Smith","StudentScores":[98,67,89]});
{
"acknowledged" : true,
"insertedId" : ObjectId("5cd7ab826d78f205348bc640")
}
> db.betterFormatDemo.insertOne({"StudentName":"John Doe","StudentScores":[67,89,56]});
{
"acknowledged" : true,
"insertedId" : ObjectId("5cd7ab936d78f205348bc641")
}
> db.betterFormatDemo.insertOne({"StudentName":"Sam Williams","StudentScores":[45,43,33]});
{
"acknowledged" : true,
"insertedId" : ObjectId("5cd7aba76d78f205348bc642")
} 다음은 find() 메서드를 사용하여 컬렉션의 모든 문서를 표시하는 쿼리입니다. -
> db.betterFormatDemo.find();
이것은 다음과 같은 출력을 생성합니다 -
{ "_id" : ObjectId("5cd7ab826d78f205348bc640"), "StudentName" : "Adam Smith", "StudentScores" : [ 98, 67, 89 ] }
{ "_id" : ObjectId("5cd7ab936d78f205348bc641"), "StudentName" : "John Doe", "StudentScores" : [ 67, 89, 56 ] }
{ "_id" : ObjectId("5cd7aba76d78f205348bc642"), "StudentName" : "Sam Williams", "StudentScores" : [ 45, 43, 33 ] } 사례 1 − findOne() 사용.
다음은 MongoDB 결과를 더 나은 형식으로 표시하는 쿼리입니다. −
> db.betterFormatDemo.findOne();
이것은 다음과 같은 출력을 생성합니다 -
{
"_id" : ObjectId("5cd7ab826d78f205348bc640"),
"StudentName" : "Adam Smith",
"StudentScores" : [
98,
67,
89
]
} 사례 2 − find().toArray() 사용.
다음은 MongoDB 결과를 더 나은 형식으로 표시하는 쿼리입니다. −
> db.betterFormatDemo.find().toArray();
이것은 다음과 같은 출력을 생성합니다 -
[
{
"_id" : ObjectId("5cd7ab826d78f205348bc640"),
"StudentName" : "Adam Smith",
"StudentScores" : [
98,
67,
89
]
},
{
"_id" : ObjectId("5cd7ab936d78f205348bc641"),
"StudentName" : "John Doe",
"StudentScores" : [
67,
89,
56
]
},
{
"_id" : ObjectId("5cd7aba76d78f205348bc642"),
"StudentName" : "Sam Williams",
"StudentScores" : [
45,
43,
33
]
}
]