查询非嵌套文档数组
非嵌套文档的数组,数组由数字、字符串等元素组成。 以下方法对数组字段进行查询操作的示例,包括对数组匹配查询,元素的增、删、改操作,空数组、非空数组查询等。
测试数据
连接到 mongodb 数据库, 创建集合 user, 批量插入如下测试数据 :
sit_rs1:PRIMARY> db.user.insertMany([ ... { name: "user1", age: 10, letter: ["x", "y"], number: [ 2, 6 ] }, ... { name: "user2", age: 20, letter: ["z", "w"], number: [ 2, 8 ] }, ... { name: "user3", age: 30, letter: ["x", "y", "v"], number: [ 2, 21 ] }, ... { name: "user4", age: 45, letter: ["v", "x"], number: [ 2.8, 32 ] }, ... { name: "user5", age: 55, letter: ["w"], number: [ 6, 5.6 ] }, ... { name: "user6", age: 55, letter: ["y", "x"], number: [ 8, 11 ] } ... ]); { "acknowledged" : true, "insertedIds" : [ ObjectId("64b119018d24a8217001e494"), ObjectId("64b119018d24a8217001e495"), ObjectId("64b119018d24a8217001e496"), ObjectId("64b119018d24a8217001e497"), ObjectId("64b119018d24a8217001e498"), ObjectId("64b119018d24a8217001e499") ] } sit_rs1:PRIMARY> db.user.find() { "_id" : ObjectId("64b119018d24a8217001e497"), "name" : "user4", "age" : 45, "letter" : [ "v", "x" ], "number" : [ 2.8, 32 ] } { "_id" : ObjectId("64b119018d24a8217001e495"), "name" : "user2", "age" : 20, "letter" : [ "z", "w" ], "number" : [ 2, 8 ] } { "_id" : ObjectId("64b119018d24a8217001e499"), "name" : "user6", "age" : 55, "letter" : [ "y", "x" ], "number" : [ 8, 11 ] } { "_id" : ObjectId("64b119018d24a8217001e496"), "name" : "user3", "age" : 30, "letter" : [ "x", "y", "v" ], "number" : [ 2, 21 ] } { "_id" : ObjectId("64b119018d24a8217001e498"), "name" : "user5", "age" : 55, "letter" : [ "w" ], "number" : [ 6, 5.6 ] } { "_id" : ObjectId("64b119018d24a8217001e494"), "name" : "user1", "age" : 10, "letter" : [ "x", "y" ], "number" : [ 2, 6 ] }
匹配查询
在指定数组上的使用相等条件,请使用查询文档 { : } 其中 是要匹配的确切数组,包括元素的顺序。
以下示例查询数组 letter 仅包含两个元素 [ “x”, “y” ](按指定顺序)的所有文档:
sit_rs1:PRIMARY> db.user.find( { letter: ["x", "y"] } ); { "_id" : ObjectId("64b117978d24a8217001e48f"), "name" : "user1", "age" : 10, "letter" : [ "x", "y" ], "number" : [ 2, 6 ] }
如果对于排序不敏感,不关心排序 ,可以使用 $all 运算符,查询包括 “x”, “y” 的元素, 注意,这个是查询要有包含两个元素的文档都会匹配, 两个元素都要有才行。 $all 和 $in 不同,$in 是匹配数组中指定的任何值。如下:
# 匹配包含查询中指定的所有元素的数组。 sit_rs1:PRIMARY> db.user.find( { letter: { $all: ["x", "y"] } } ) { "_id" : ObjectId("64b119018d24a8217001e499"), "name" : "user6", "age" : 55, "letter" : [ "y", "x" ], "number" : [ 8, 11 ] } { "_id" : ObjectId("64b119018d24a8217001e496"), "name" : "user3", "age" : 30, "letter" : [ "x", "y", "v" ], "number" : [ 2, 21 ] } { "_id" : ObjectId("64b119018d24a8217001e494"), "name" : "user1", "age" : 10, "letter" : [ "x", "y" ], "number" : [ 2, 6 ] } # 匹配数组中指定的任何值 sit_rs1:PRIMARY> db.user.find( { letter: { $in: ["x", "y"] } } ) { "_id" : ObjectId("64b119018d24a8217001e494"), "name" : "user1", "age" : 10, "letter" : [ "x", "y" ], "number" : [ 2, 6 ] } { "_id" : ObjectId("64b119018d24a8217001e496"), "name" : "user3", "age" : 30, "letter" : [ "x", "y", "v" ], "number" : [ 2, 21 ] } { "_id" : ObjectId("64b119018d24a8217001e497"), "name" : "user4", "age" : 45, "letter" : [ "v", "x" ], "number" : [ 2.8, 32 ] } { "_id" : ObjectId("64b119018d24a8217001e499"), "name" : "user6", "age" : 55, "letter" : [ "y", "x" ], "number" : [ 8, 11 ] }
要查询数组字段是否至少包含一个具有指定值的元素,请使用过滤器 { : } 其中 是元素值。 这个查询也可以使用 $all 操作符。
sit_rs1:PRIMARY> db.user.find( { letter: "x" } ) { "_id" : ObjectId("64b119018d24a8217001e497"), "name" : "user4", "age" : 45, "letter" : [ "v", "x" ], "number" : [ 2.8, 32 ] } { "_id" : ObjectId("64b119018d24a8217001e499"), "name" : "user6", "age" : 55, "letter" : [ "y", "x" ], "number" : [ 8, 11 ] } { "_id" : ObjectId("64b119018d24a8217001e496"), "name" : "user3", "age" : 30, "letter" : [ "x", "y", "v" ], "number" : [ 2, 21 ] } { "_id" : ObjectId("64b119018d24a8217001e494"), "name" : "user1", "age" : 10, "letter" : [ "x", "y" ], "number" : [ 2, 6 ] } sit_rs1:PRIMARY> db.user.find( { letter: { $all: ["x"] } } ) { "_id" : ObjectId("64b119018d24a8217001e494"), "name" : "user1", "age" : 10, "letter" : [ "x", "y" ], "number" : [ 2, 6 ] } { "_id" : ObjectId("64b119018d24a8217001e496"), "name" : "user3", "age" : 30, "letter" : [ "x", "y", "v" ], "number" : [ 2, 21 ] } { "_id" : ObjectId("64b119018d24a8217001e497"), "name" : "user4", "age" : 45, "letter" : [ "v", "x" ], "number" : [ 2.8, 32 ] } { "_id" : ObjectId("64b119018d24a8217001e499"), "name" : "user6", "age" : 55, "letter" : [ "y", "x" ], "number" : [ 8, 11 ] }
要指定数组字段中元素的条件, 请使用 { : { : , … } } ,比如查询 number 至少包含一个值大于8 的, $elemMatch , 如果数组字段中的元素符合所有指定$elemMatch条件,则选择文档。
# 查询 number 至少包含一个值大于8 的 sit_rs1:PRIMARY> db.user.find( { number: { $gt: 8 } } ) { "_id" : ObjectId("64b119018d24a8217001e496"), "name" : "user3", "age" : 30, "letter" : [ "x", "y", "v" ], "number" : [ 2, 21 ] } { "_id" : ObjectId("64b119018d24a8217001e497"), "name" : "user4", "age" : 45, "letter" : [ "v", "x" ], "number" : [ 2.8, 32 ] } { "_id" : ObjectId("64b119018d24a8217001e499"), "name" : "user6", "age" : 55, "letter" : [ "y", "x" ], "number" : [ 8, 11 ] } # 查询 number 至少包含一个值大于8 的 并且小于 20 的 sit_rs1:PRIMARY> db.user.find( { number: { $elemMatch: { $gt: 8, $lt: 20 } } } ) { "_id" : ObjectId("64b119018d24a8217001e499"), "name" : "user6", "age" : 55, "letter" : [ "y", "x" ], "number" : [ 8, 11 ] }
索引下标
通过数组索引位置查询元素, 使用点表示法,您可以为数组的特定索引或位置处的元素指定查询条件。该数组使用从零开始的索引。注意: 使用点表示法查询时,字段和嵌套字段必须在引号内。
# 以下示例查询数组中 number 第一个元素大于5 的所有文档: sit_rs1:PRIMARY> db.user.find( { "number.0": { $gt: 5 } } ) { "_id" : ObjectId("64b119018d24a8217001e498"), "name" : "user5", "age" : 55, "letter" : [ "w" ], "number" : [ 6, 5.6 ] } { "_id" : ObjectId("64b119018d24a8217001e499"), "name" : "user6", "age" : 55, "letter" : [ "y", "x" ], "number" : [ 8, 11 ] } # 以下示例查询数组中 number 第二个元素大于8 的所有文档: sit_rs1:PRIMARY> db.user.find( { "number.1": { $gt: 8 } } ) { "_id" : ObjectId("64b119018d24a8217001e496"), "name" : "user3", "age" : 30, "letter" : [ "x", "y", "v" ], "number" : [ 2, 21 ] } { "_id" : ObjectId("64b119018d24a8217001e497"), "name" : "user4", "age" : 45, "letter" : [ "v", "x" ], "number" : [ 2.8, 32 ] } { "_id" : ObjectId("64b119018d24a8217001e499"), "name" : "user6", "age" : 55, "letter" : [ "y", "x" ], "number" : [ 8, 11 ] }
使用 $size 运算符按元素数量查询数组。例如,以下选择数组 letter 具有 2 个元素的文档。通过 $size 运算符可以用来判断数组是否为空。
# 查询 数组 letter 具有 2 个元素的文档 sit_rs1:PRIMARY> db.user.find( { "letter": { $size: 2 } } ) { "_id" : ObjectId("64b119018d24a8217001e494"), "name" : "user1", "age" : 10, "letter" : [ "x", "y" ], "number" : [ 2, 6 ] } { "_id" : ObjectId("64b119018d24a8217001e495"), "name" : "user2", "age" : 20, "letter" : [ "z", "w" ], "number" : [ 2, 8 ] } { "_id" : ObjectId("64b119018d24a8217001e497"), "name" : "user4", "age" : 45, "letter" : [ "v", "x" ], "number" : [ 2.8, 32 ] } { "_id" : ObjectId("64b119018d24a8217001e499"), "name" : "user6", "age" : 55, "letter" : [ "y", "x" ], "number" : [ 8, 11 ] } sit_rs1:PRIMARY> db.user.insertMany([ ... { name: "user7", age: 10, letter: [], number: [ ] }, ... { name: "user8", age: 11, letter: [], number: [ ] } ... ]) { "acknowledged" : true, "insertedIds" : [ ObjectId("64b505228233a7361a2079b5"), ObjectId("64b505228233a7361a2079b6") ] } # 查询 数组 letter 为空的文档 sit_rs1:PRIMARY> db.user.find( { "letter": { $size: 0 } } ) { "_id" : ObjectId("64b505228233a7361a2079b6"), "name" : "user8", "age" : 11, "letter" : [ ], "number" : [ ] } { "_id" : ObjectId("64b505228233a7361a2079b5"), "name" : "user7", "age" : 10, "letter" : [ ], "number" : [ ] } # 查询 数组 letter 为空的文档--通过匹配的方法 sit_rs1:PRIMARY> db.user.find( { "letter": [] } ) { "_id" : ObjectId("64b505228233a7361a2079b6"), "name" : "user8", "age" : 11, "letter" : [ ], "number" : [ ] } { "_id" : ObjectId("64b505228233a7361a2079b5"), "name" : "user7", "age" : 10, "letter" : [ ], "number" : [ ] }
如何查询数组元素不为空呢? $size 只能查询如果数组字段具有指定大小,则选择文档。 我们要查询不为空的,就是数组长度大于0的数组,或者不为[]的数组。或者使用$where运算符。如下:
$where: 运算符将包含 JavaScript 表达式的字符串或完整的 JavaScript 函数传递给查询系统。这提供了更大的灵活性,但要求数据库处理集合中每个文档的 JavaScript 表达式或函数。this使用或引用 JavaScript 表达式或函数中的文档obj。
# $ne匹配所有不等于指定值的值。 sit_rs1:PRIMARY> db.user.find( { "letter": { $ne: []}} ) { "_id" : ObjectId("64b119018d24a8217001e497"), "name" : "user4", "age" : 45, "letter" : [ "v", "x" ], "number" : [ 2.8, 32 ] } { "_id" : ObjectId("64b119018d24a8217001e495"), "name" : "user2", "age" : 20, "letter" : [ "z", "w" ], "number" : [ 2, 8 ] } { "_id" : ObjectId("64b119018d24a8217001e499"), "name" : "user6", "age" : 55, "letter" : [ "y", "x" ], "number" : [ 8, 11 ] } { "_id" : ObjectId("64b119018d24a8217001e496"), "name" : "user3", "age" : 30, "letter" : [ "x", "y", "v" ], "number" : [ 2, 21 ] } { "_id" : ObjectId("64b119018d24a8217001e498"), "name" : "user5", "age" : 55, "letter" : [ "w" ], "number" : [ 6, 5.6 ] } { "_id" : ObjectId("64b119018d24a8217001e494"), "name" : "user1", "age" : 10, "letter" : [ "x", "y" ], "number" : [ 2, 6 ] } # 查询数组存在第一个元素,存在即不为空数组 sit_rs1:PRIMARY> db.user.find( { "letter.0": { $exists: true } } ) { "_id" : ObjectId("64b119018d24a8217001e497"), "name" : "user4", "age" : 45, "letter" : [ "v", "x" ], "number" : [ 2.8, 32 ] } { "_id" : ObjectId("64b119018d24a8217001e495"), "name" : "user2", "age" : 20, "letter" : [ "z", "w" ], "number" : [ 2, 8 ] } { "_id" : ObjectId("64b119018d24a8217001e499"), "name" : "user6", "age" : 55, "letter" : [ "y", "x" ], "number" : [ 8, 11 ] } { "_id" : ObjectId("64b119018d24a8217001e496"), "name" : "user3", "age" : 30, "letter" : [ "x", "y", "v" ], "number" : [ 2, 21 ] } { "_id" : ObjectId("64b119018d24a8217001e498"), "name" : "user5", "age" : 55, "letter" : [ "w" ], "number" : [ 6, 5.6 ] } { "_id" : ObjectId("64b119018d24a8217001e494"), "name" : "user1", "age" : 10, "letter" : [ "x", "y" ], "number" : [ 2, 6 ] } # $where匹配满足 JavaScript 表达式的文档。 sit_rs1:PRIMARY> db.user.find( { $where: "this.letter.length>0" } ) { "_id" : ObjectId("64b119018d24a8217001e497"), "name" : "user4", "age" : 45, "letter" : [ "v", "x" ], "number" : [ 2.8, 32 ] } { "_id" : ObjectId("64b119018d24a8217001e495"), "name" : "user2", "age" : 20, "letter" : [ "z", "w" ], "number" : [ 2, 8 ] } { "_id" : ObjectId("64b119018d24a8217001e499"), "name" : "user6", "age" : 55, "letter" : [ "y", "x" ], "number" : [ 8, 11 ] } { "_id" : ObjectId("64b119018d24a8217001e496"), "name" : "user3", "age" : 30, "letter" : [ "x", "y", "v" ], "number" : [ 2, 21 ] } { "_id" : ObjectId("64b119018d24a8217001e498"), "name" : "user5", "age" : 55, "letter" : [ "w" ], "number" : [ 6, 5.6 ] } { "_id" : ObjectId("64b119018d24a8217001e494"), "name" : "user1", "age" : 10, "letter" : [ "x", "y" ], "number" : [ 2, 6 ] }
查询嵌套文档数组
连接到 mongodb 数据库, 创建集合 user, 批量插入如下测试数据 :
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("64b621ad9be837dc8e997b6e"), ObjectId("64b621ad9be837dc8e997b6f"), ObjectId("64b621ad9be837dc8e997b70"), ObjectId("64b621ad9be837dc8e997b71"), ObjectId("64b621ad9be837dc8e997b72"), ObjectId("64b621ad9be837dc8e997b73") ] } sit_rs1:PRIMARY> db.user.find() { "_id" : ObjectId("64b621ad9be837dc8e997b71"), "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("64b621ad9be837dc8e997b6e"), "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("64b621ad9be837dc8e997b70"), "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("64b621ad9be837dc8e997b72"), "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("64b621ad9be837dc8e997b73"), "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 } ] } { "_id" : ObjectId("64b621ad9be837dc8e997b6f"), "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 } ] }
以下示例选择数组中的元素与 letter 指定文档匹配的所有文档:整个嵌入/嵌套文档的相等匹配需要 指定文档的精确匹配,包括字段顺序。示例 letter 数组第二个元素为: { “x” : “X2”, “y” : “Y2” } 与搜索条件匹配。
sit_rs1:PRIMARY> db.user.find( { "letter": {"x":"X2", "y":"Y2"} } ) { "_id" : ObjectId("64b621ad9be837dc8e997b6e"), "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 } ] } # 上面的查询条件如果调换顺序,则无法匹配!!!!! sit_rs1:PRIMARY> db.user.find( { "letter": {"y":"Y2", "x":"X2" } } ).count() 0
指定文档数组中字段的查询条件
如果您不知道数组中嵌套文档的索引位置,请使用点 ( .) 连接数组字段的名称。以下示例选择 number 数组中 n1 值 至少有一个 大于或等于 5 的所有文档:
注: 使用点表示法查询时,字段和索引必须位于引号内。
sit_rs1:PRIMARY> db.user.find( { 'number.n1': { $gte: 5 } } ) { "_id" : ObjectId("64b621ad9be837dc8e997b71"), "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("64b621ad9be837dc8e997b6e"), "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("64b621ad9be837dc8e997b72"), "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("64b621ad9be837dc8e997b73"), "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 } ] } { "_id" : ObjectId("64b621ad9be837dc8e997b6f"), "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 } ] }
你也可以使用索引,为文档中特定索引或数组位置的字段指定查询条件。该数组使用从零开始的索引。
sit_rs1:PRIMARY> db.user.find( { 'number.0.n1': { $gte: 5 } } ) { "_id" : ObjectId("64b621ad9be837dc8e997b71"), "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("64b621ad9be837dc8e997b72"), "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("64b621ad9be837dc8e997b73"), "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 } ] }
为文档数组指定多个条件, 当对嵌套在文档数组中的多个字段指定条件时,您可以指定查询,使得单个文档满足这些条件,或者数组中的任意文档组合(包括单个文档)满足条件。
- 单个文档满足条件
使用 $elemMatch 运算符对一组嵌入文档指定多个条件,以便至少一个嵌入文档满足所有指定的条件。注意: 这里是 n1 这个指段(同一文档)满足大于5且小于8,如果数组存在至少一个这样的文档,则匹配。
# 以下示例查询数组中 number 至少有一个嵌入文档, n1 大于5且小于8的字段的文档: sit_rs1:PRIMARY> db.user.find( { "number": { $elemMatch: { n1: { $gt: 5, $lt: 8 } } } } ) { "_id" : ObjectId("64b621ad9be837dc8e997b72"), "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("64b621ad9be837dc8e997b73"), "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 } ] } { "_id" : ObjectId("64b621ad9be837dc8e997b6f"), "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 } ] }
- 组合文档满足条件
如果数组字段上的复合查询条件不使用运算 $elemMatch符,则查询会选择数组中包含满足条件的任意元素组合的文档,如下:注意这里是组合满足。即数组中如果有两个文档组合满足条件,即匹配。
sit_rs1:PRIMARY> db.user.find( { "number.n1": { $gt: 5, $lt: 8 } } ) { "_id" : ObjectId("64b621ad9be837dc8e997b71"), "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("64b621ad9be837dc8e997b72"), "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("64b621ad9be837dc8e997b73"), "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 } ] } { "_id" : ObjectId("64b621ad9be837dc8e997b6f"), "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 } ] }
到此这篇关于Mongodb 对嵌套文档数组进行查询操作的文章就介绍到这了,更多相关Mongodb嵌套文档数组查询内容请搜索IT俱乐部以前的文章或继续浏览下面的相关文章希望大家以后多多支持IT俱乐部!