먼저 문서로 컬렉션을 만들어 보겠습니다. −
> db.demo371.insertOne({"Name":"David","CountryName":"US"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5e57f6982ae06a1609a00af2")
}
> db.demo371.insertOne({"Name":"John","CountryName":"UK"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5e57f69e2ae06a1609a00af3")
}
> db.demo371.insertOne({"Name":"Bob","CountryName":"AUS"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5e57f6a42ae06a1609a00af4")
}
> db.demo371.insertOne({"Name":"Mike","CountryName":"US"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5e57f6ba2ae06a1609a00af5")
} find() 메서드를 사용하여 컬렉션의 모든 문서 표시 -
> db.demo371.find();
이것은 다음과 같은 출력을 생성합니다 -
{ "_id" : ObjectId("5e57f6982ae06a1609a00af2"), "Name" : "David", "CountryName" : "US" }
{ "_id" : ObjectId("5e57f69e2ae06a1609a00af3"), "Name" : "John", "CountryName" : "UK" }
{ "_id" : ObjectId("5e57f6a42ae06a1609a00af4"), "Name" : "Bob", "CountryName" : "AUS" }
{ "_id" : ObjectId("5e57f6ba2ae06a1609a00af5"), "Name" : "Mike", "CountryName" : "US" } 다음은 특정 필드로 검색하는 쿼리입니다 -
> db.demo371.find({ $or: [ { Name: "David" }, { CountryName: "UK"} ] } ) 이것은 다음과 같은 출력을 생성합니다 -
{ "_id" : ObjectId("5e57f6982ae06a1609a00af2"), "Name" : "David", "CountryName" : "US" }
{ "_id" : ObjectId("5e57f69e2ae06a1609a00af3"), "Name" : "John", "CountryName" : "UK" }