Computer >> 컴퓨터 >  >> 프로그램 작성 >> MongoDB

특정 레코드와 일치하는 MongoDB 정규식?

<시간/>

먼저 문서로 컬렉션을 만들어 보겠습니다. −

> dbworkingOfRegularExpressionDemoinsertOne({ "StudentDetails" : { "StudentName" : "John" },"StudentAge":21 });
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5cf227acb64a577be5a2bc07")
}
> dbworkingOfRegularExpressionDemoinsertOne({ "StudentDetails" : { "StudentName" : "JOHN" },"StudentAge":19 });
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5cf227b8b64a577be5a2bc08")
}
> dbworkingOfRegularExpressionDemoinsertOne({ "StudentDetails" : { "StudentName" : "Carol" },"StudentAge":20 });
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5cf227c2b64a577be5a2bc09")
}

다음은 find() 메서드를 사용하여 컬렉션의 모든 문서를 표시하는 쿼리입니다. -

> dbworkingOfRegularExpressionDemofind();

이것은 다음 문서를 생성합니다 -

{ "_id" : ObjectId("5cf227acb64a577be5a2bc07"), "StudentDetails" : { "StudentName" : "John" }, "StudentAge" : 21 }
{ "_id" : ObjectId("5cf227b8b64a577be5a2bc08"), "StudentDetails" : { "StudentName" : "JOHN" }, "StudentAge" : 19 }
{ "_id" : ObjectId("5cf227c2b64a577be5a2bc09"), "StudentDetails" : { "StudentName" : "Carol" }, "StudentAge" : 20 }
Following is the regular expression to get the document with StudentName JOHN:
> dbworkingOfRegularExpressionDemofind({'StudentDetailsStudentName': /JOHN/});

문서를 가져오는 정규식입니다.

이것은 다음 문서를 생성합니다 -

{ "_id" : ObjectId("5cf227b8b64a577be5a2bc08"), "StudentDetails" : { "StudentName" : "JOHN" }, "StudentAge" : 19 }