MongoDB에서 $regex로 이름을 보내 입력과 유사한 이름을 찾습니다. 문서로 컬렉션을 만들자 −
> db.demo514.insertOne({"Information":{"FullName":"John Doe"}});{
"acknowledged" : true,
"insertedId" : ObjectId("5e885116987b6e0e9d18f58c")
}
> db.demo514.insertOne({"Information":{"FullName":"John Smith"}});{
"acknowledged" : true,
"insertedId" : ObjectId("5e88515e987b6e0e9d18f58d")
}
> db.demo514.insertOne({"Information":{"FullName":"john doe"}});{
"acknowledged" : true,
"insertedId" : ObjectId("5e885169987b6e0e9d18f58e")
}
> db.demo514.insertOne({"Information":{"FullName":"Chris Brown"}});{
"acknowledged" : true,
"insertedId" : ObjectId("5e88516f987b6e0e9d18f58f")
} find() 메서드를 사용하여 컬렉션의 모든 문서 표시 -
> db.demo514.find();
이것은 다음과 같은 출력을 생성합니다 -
{ "_id" : ObjectId("5e885116987b6e0e9d18f58c"), "Information" : { "FullName" : "John Doe" } }
{ "_id" : ObjectId("5e88515e987b6e0e9d18f58d"), "Information" : { "FullName" : "John Smith" } }
{ "_id" : ObjectId("5e885169987b6e0e9d18f58e"), "Information" : { "FullName" : "john doe" } }
{ "_id" : ObjectId("5e88516f987b6e0e9d18f58f"), "Information" : { "FullName" : "Chris Brown" } } 다음은 입력과 유사한 이름을 찾는 쿼리입니다 -
> db.demo514.find({'Information.FullName': {$regex: "John Doe", $options: 'i'}}); 이것은 다음과 같은 출력을 생성합니다 -
{ "_id" : ObjectId("5e885116987b6e0e9d18f58c"), "Information" : { "FullName" : "John Doe" } }
{ "_id" : ObjectId("5e885169987b6e0e9d18f58e"), "Information" : { "FullName" : "john doe" } }