Mongodb过滤器filter选择要返回的数组子集操作方法


    目录
  • Mongodb过滤器
  • 插入测试数据
  • 聚合管道更新

    Mongodb过滤器
    Mongodb使用过滤器 $filter 根据指定条件选择要返回的数组子集。返回仅包含与条件匹配的元素的数组。返回的元素按原始顺序排列。也就是说,这个是选择你要的文档,而不是排除。 如果该数组没有符合条件 ,则为 空 []
    $filter: 选择数组的子集以返回仅包含与过滤条件匹配的元素的数组。
    插入测试数据
    
sit_rs1:PRIMARY> db.user.insertMany([
...  { name: "user1", age: 10, letter: [{"x":"X1", "y":"Y1"}, {"x":"X2", "y":"Y2"}, {"x":"X3", "y":"Y3"}], number: [ { "n1":2, "n2":6 }, { "n1":3, "n2":3 }, { "n1":5, "n2":6 } ] },
...  { name: "user2", age: 20, letter: [{"V":"V1", "y":"Y1"}, {"V":"V2", "y":"Y2"}, {"V":"V3", "y":"Y3"}], number: [ { "n1":1, "n2":3 }, { "n1":4, "n2":5 }, { "n1":7, "n2":6 } ] },
...  { name: "user3", age: 30, letter: [{"V":"V1", "w":"W1"}, {"V":"V2", "w":"W2"}, {"V":"V3", "w":"W3"}], number: [ { "n1":1, "n2":8 }, { "n1":2, "n2":6 }, { "n1":2, "n2":6 } ] },
...  { name: "user4", age: 45, letter: [{"z":"Z1", "w":"W1"}, {"z":"Z2", "w":"W2"}, {"z":"Z3", "w":"W3"}], number: [ { "n1":9, "n2":8 }, { "n1":2, "n2":4 }, { "n1":3, "n2":6 } ] },
...  { name: "user5", age: 55, letter: [{"z":"Z1", "u":"U1"}, {"z":"Z2", "u":"U2"}, {"z":"Z3", "u":"U3"}], number: [ { "n1":7, "n2":8 }, { "n1":8, "n2":4 }, { "n1":4, "n2":6 } ] },
...  { name: "user6", age: 55, letter: [{"t":"T1", "u":"U1"}, {"t":"T2", "u":"U2"}, {"t":"T3", "u":"U3"}], number: [ { "n1":5, "n2":8 }, { "n1":8, "n2":5 }, { "n1":7, "n2":6 } ] },
... ])
{
        "acknowledged" : true,
        "insertedIds" : [
                ObjectId("64b661869be837dc8e997b74"),
                ObjectId("64b661869be837dc8e997b75"),
                ObjectId("64b661869be837dc8e997b76"),
                ObjectId("64b661869be837dc8e997b77"),
                ObjectId("64b661869be837dc8e997b78"),
                ObjectId("64b661869be837dc8e997b79")
        ]
}
sit_rs1:PRIMARY> db.user.find()
{ "_id" : ObjectId("64b661869be837dc8e997b74"), "name" : "user1", "age" : 10, "letter" : [ { "x" : "X1", "y" : "Y1" }, { "x" : "X2", "y" : "Y2" }, { "x" : "X3", "y" : "Y3" } ], "number" : [ { "n1" : 2, "n2" : 6 }, { "n1" : 3, "n2" : 3 }, { "n1" : 5, "n2" : 6 } ] }
{ "_id" : ObjectId("64b661869be837dc8e997b75"), "name" : "user2", "age" : 20, "letter" : [ { "V" : "V1", "y" : "Y1" }, { "V" : "V2", "y" : "Y2" }, { "V" : "V3", "y" : "Y3" } ], "number" : [ { "n1" : 1, "n2" : 3 }, { "n1" : 4, "n2" : 5 }, { "n1" : 7, "n2" : 6 } ] }
{ "_id" : ObjectId("64b661869be837dc8e997b76"), "name" : "user3", "age" : 30, "letter" : [ { "V" : "V1", "w" : "W1" }, { "V" : "V2", "w" : "W2" }, { "V" : "V3", "w" : "W3" } ], "number" : [ { "n1" : 1, "n2" : 8 }, { "n1" : 2, "n2" : 6 }, { "n1" : 2, "n2" : 6 } ] }
{ "_id" : ObjectId("64b661869be837dc8e997b77"), "name" : "user4", "age" : 45, "letter" : [ { "z" : "Z1", "w" : "W1" }, { "z" : "Z2", "w" : "W2" }, { "z" : "Z3", "w" : "W3" } ], "number" : [ { "n1" : 9, "n2" : 8 }, { "n1" : 2, "n2" : 4 }, { "n1" : 3, "n2" : 6 } ] }
{ "_id" : ObjectId("64b661869be837dc8e997b78"), "name" : "user5", "age" : 55, "letter" : [ { "z" : "Z1", "u" : "U1" }, { "z" : "Z2", "u" : "U2" }, { "z" : "Z3", "u" : "U3" } ], "number" : [ { "n1" : 7, "n2" : 8 }, { "n1" : 8, "n2" : 4 }, { "n1" : 4, "n2" : 6 } ] }
{ "_id" : ObjectId("64b661869be837dc8e997b79"), "name" : "user6", "age" : 55, "letter" : [ { "t" : "T1", "u" : "U1" }, { "t" : "T2", "u" : "U2" }, { "t" : "T3", "u" : "U3" } ], "number" : [ { "n1" : 5, "n2" : 8 }, { "n1" : 8, "n2" : 5 }, { "n1" : 7, "n2" : 6 } ] }

    $filter 具有以下语法:
    
{ $filter: { input: <array>, as: <string>, cond: <expression> } }

    
  • input: 解析为数组的表达式。
  • as: 可选的。代表数组中每个单独元素的变量名称input。如果未指定名称,则变量名称默认为this。
  • cond: 解析为布尔值的表达式,用于确定是否应将某个元素包含在输出数组中。表达式使用as中指定的变量名分别引用输入数组的每个元素。

    
# 以下示例选择 number 数组 n1 字段 仅包含 “大于5” 的文档:
sit_rs1:PRIMARY> db.user.aggregate([
...    {
...       $project: {
...          number: {
...             $filter: {
...                input: "$number",
...                as: "number",
...                cond: { $gt: [ "$$number.n1", 5 ] }
...             }
...          }
...       }
...    }
... ])
{ "_id" : ObjectId("64b661869be837dc8e997b74"), "number" : [ ] }
{ "_id" : ObjectId("64b661869be837dc8e997b75"), "number" : [ { "n1" : 7, "n2" : 6 } ] }
{ "_id" : ObjectId("64b661869be837dc8e997b76"), "number" : [ ] }
{ "_id" : ObjectId("64b661869be837dc8e997b77"), "number" : [ { "n1" : 9, "n2" : 8 } ] }
{ "_id" : ObjectId("64b661869be837dc8e997b78"), "number" : [ { "n1" : 7, "n2" : 8 }, { "n1" : 8, "n2" : 4 } ] }
{ "_id" : ObjectId("64b661869be837dc8e997b79"), "number" : [ { "n1" : 8, "n2" : 5 }, { "n1" : 7, "n2" : 6 } ] }

    as: 是可选的。如果未指定名称,则变量名称默认为this。下面使用 this 代替。
    
sit_rs1:PRIMARY> db.user.aggregate([
...    {
...       $project: {
...          number: {
...             $filter: {
...                input: "$number",
...                cond: { $gt: [ "$$this.n1", 5 ] }
...             }
...          }
...       }
...    }
... ])
{ "_id" : ObjectId("64b661869be837dc8e997b74"), "number" : [ ] }
{ "_id" : ObjectId("64b661869be837dc8e997b75"), "number" : [ { "n1" : 7, "n2" : 6 } ] }
{ "_id" : ObjectId("64b661869be837dc8e997b76"), "number" : [ ] }
{ "_id" : ObjectId("64b661869be837dc8e997b77"), "number" : [ { "n1" : 9, "n2" : 8 } ] }
{ "_id" : ObjectId("64b661869be837dc8e997b78"), "number" : [ { "n1" : 7, "n2" : 8 }, { "n1" : 8, "n2" : 4 } ] }
{ "_id" : ObjectId("64b661869be837dc8e997b79"), "number" : [ { "n1" : 8, "n2" : 5 }, { "n1" : 7, "n2" : 6 } ] }

    聚合管道更新
    从 MongoDB 4.2 开始,您可以使用聚合管道进行更新:使用聚合管道可以实现更具表现力的更新语句,例如基于当前字段值表达条件更新或使用另一个字段的值更新一个字段。
    以下示例,使用 filter 选择数组的子集, 再更新原来的数组。
    
# 以下示例选择 number 数组 n1 字段 仅包含 “大于5” 的文档:并更新替换原来的数组 
sit_rs1:PRIMARY> db.user.updateMany(
...   { "number": { $exists: true } }, 
...   [{ 
...       $set: { 
...           "number": { 
...               $filter: { 
...   			      input: "$number", 
...   			      as: "number", 
...   			      cond: { $gt: [ "$$number.n1", 5 ] }
...   			  } 
...   		   } 
...    	  } 
...   }] 
... )
{ "acknowledged" : true, "matchedCount" : 6, "modifiedCount" : 6 }
sit_rs1:PRIMARY> db.user.find()
{ "_id" : ObjectId("64b661869be837dc8e997b74"), "name" : "user1", "age" : 10, "letter" : [ { "x" : "X1", "y" : "Y1" }, { "x" : "X2", "y" : "Y2" }, { "x" : "X3", "y" : "Y3" } ], "number" : [ ] }
{ "_id" : ObjectId("64b661869be837dc8e997b75"), "name" : "user2", "age" : 20, "letter" : [ { "V" : "V1", "y" : "Y1" }, { "V" : "V2", "y" : "Y2" }, { "V" : "V3", "y" : "Y3" } ], "number" : [ { "n1" : 7, "n2" : 6 } ] }
{ "_id" : ObjectId("64b661869be837dc8e997b76"), "name" : "user3", "age" : 30, "letter" : [ { "V" : "V1", "w" : "W1" }, { "V" : "V2", "w" : "W2" }, { "V" : "V3", "w" : "W3" } ], "number" : [ ] }
{ "_id" : ObjectId("64b661869be837dc8e997b77"), "name" : "user4", "age" : 45, "letter" : [ { "z" : "Z1", "w" : "W1" }, { "z" : "Z2", "w" : "W2" }, { "z" : "Z3", "w" : "W3" } ], "number" : [ { "n1" : 9, "n2" : 8 } ] }
{ "_id" : ObjectId("64b661869be837dc8e997b78"), "name" : "user5", "age" : 55, "letter" : [ { "z" : "Z1", "u" : "U1" }, { "z" : "Z2", "u" : "U2" }, { "z" : "Z3", "u" : "U3" } ], "number" : [ { "n1" : 7, "n2" : 8 }, { "n1" : 8, "n2" : 4 } ] }
{ "_id" : ObjectId("64b661869be837dc8e997b79"), "name" : "user6", "age" : 55, "letter" : [ { "t" : "T1", "u" : "U1" }, { "t" : "T2", "u" : "U2" }, { "t" : "T3", "u" : "U3" } ], "number" : [ { "n1" : 8, "n2" : 5 }, { "n1" : 7, "n2" : 6 } ] }

    到此这篇关于Mongodb过滤器filter选择要返回的数组子集的文章就介绍到这了,更多相关Mongodb数组子集内容请搜索电脑手机教程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持电脑手机教程网!