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

MongoDB 집계 프레임워크 일치 또는 가능합니까?

<시간/>

먼저 문서로 컬렉션을 생성해 보겠습니다. 문서로 컬렉션을 생성하는 쿼리는 다음과 같습니다 -

> db.aggregationFrameworkWithOrMatchDemo.insertOne({"StudentFirstName":"John",
   "StudentLastName":"Smith","StudentAge":23});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5c8ac3a96cea1f28b7aa080d")
}
> db.aggregationFrameworkWithOrMatchDemo.insertOne({"StudentFirstName":"Carol",
   "StudentLastName":"Tayor","StudentAge":24});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5c8ac3bc6cea1f28b7aa080e")
}
> db.aggregationFrameworkWithOrMatchDemo.insertOne({"StudentFirstName":"David",
   "StudentLastName":"Miller","StudentAge":21});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5c8ac3ce6cea1f28b7aa080f")
}
> db.aggregationFrameworkWithOrMatchDemo.insertOne({"StudentFirstName":"Bob",
   "StudentLastName":"Taylor","StudentAge":20});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5c8ac3e16cea1f28b7aa0810")
}
> db.aggregationFrameworkWithOrMatchDemo.insertOne({"StudentFirstName":"Robert",
   "StudentLastName":"Smith","StudentAge":20});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5c8ac3fb6cea1f28b7aa0811")
}
> db.aggregationFrameworkWithOrMatchDemo.insertOne({"StudentFirstName":"Mike",
   "StudentLastName":"Miller","StudentAge":27});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5c8ac4166cea1f28b7aa0812")
}

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

> db.aggregationFrameworkWithOrMatchDemo.find().pretty();

다음은 출력입니다 -

{
   "_id" : ObjectId("5c8ac3a96cea1f28b7aa080d"),
   "StudentFirstName" : "John",
   "StudentLastName" : "Smith",
   "StudentAge" : 23
}
{
   "_id" : ObjectId("5c8ac3bc6cea1f28b7aa080e"),
   "StudentFirstName" : "Carol",
   "StudentLastName" : "Tayor",
   "StudentAge" : 24
}
{
   "_id" : ObjectId("5c8ac3ce6cea1f28b7aa080f"),
   "StudentFirstName" : "David",
   "StudentLastName" : "Miller",
   "StudentAge" : 21
}
{
   "_id" : ObjectId("5c8ac3e16cea1f28b7aa0810"),
   "StudentFirstName" : "Bob",
   "StudentLastName" : "Taylor",
   "StudentAge" : 20
}
{
   "_id" : ObjectId("5c8ac3fb6cea1f28b7aa0811"),
   "StudentFirstName" : "Robert",
   "StudentLastName" : "Smith",
   "StudentAge" : 20
}
{
   "_id" : ObjectId("5c8ac4166cea1f28b7aa0812"),
   "StudentFirstName" : "Mike",
   "StudentLastName" : "Miller",
   "StudentAge" : 27
}

다음은 프레임워크 일치 OR을 집계하는 쿼리입니다. StudentLastName을 Smith 또는 Miller로 고려 중입니다. −

> db.aggregationFrameworkWithOrMatchDemo.aggregate({
... $match: { $or: [{ StudentLastName: 'Smith' }, { StudentLastName: 'Miller' }] }}).pretty();

다음은 출력입니다 -

{
   "_id" : ObjectId("5c8ac3a96cea1f28b7aa080d"),
   "StudentFirstName" : "John",
   "StudentLastName" : "Smith",
   "StudentAge" : 23
}
{
   "_id" : ObjectId("5c8ac3ce6cea1f28b7aa080f"),
   "StudentFirstName" : "David",
   "StudentLastName" : "Miller",
   "StudentAge" : 21
}
{
   "_id" : ObjectId("5c8ac3fb6cea1f28b7aa0811"),
   "StudentFirstName" : "Robert",
   "StudentLastName" : "Smith",
   "StudentAge" : 20
}
{
   "_id" : ObjectId("5c8ac4166cea1f28b7aa0812"),
   "StudentFirstName" : "Mike",
   "StudentLastName" : "Miller",
   "StudentAge" : 27
}