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
}

更新於: 2019-07-30

177 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始
廣告