MongoDB 聚合框架匹配 OR 可能吗?
让我们首先用文档创建一个集合。使用文档创建集合的查询如下-
> 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
}这是聚合框架匹配或的查询。我们将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
}热门推荐
10 对生活祝福语简短精辟
11 老人掉牙祝福语简短语
12 买房的祝福语高级简短
13 潮汕生日谚语祝福语简短
14 父亲祝福语大全简短语
15 祝美术老师祝福语简短
16 生日祝福语简短独特的
17 班级集体生日祝福语简短
18 元旦的祝福语简短 公司