检查数组中的每个元素是否都符合条件

我有一些文件:


date: Date

users: [

  { user: 1, group: 1 }

  { user: 5, group: 2 }

]


date: Date

users: [

  { user: 1, group: 1 }

  { user: 3, group: 2 }

]

我想查询该集合以查找所有文档,其中我的用户数组中的每个用户ID都位于另一个数组[1、5、7]中。在此示例中,仅第一个文档匹配。


我一直能找到的最佳解决方案是:


$where: function() { 

  var ids = [1, 5, 7];

  return this.users.every(function(u) { 

    return ids.indexOf(u.user) !== -1;

  });

}

不幸的是,这似乎损害了性能,在$ where文档中指出:


$ where评估JavaScript,无法利用索引。


如何改善此查询?


噜噜哒
浏览 996回答 3
3回答

阿波罗的战车

您想要的查询是这样的:db.collection.find({"users":{"$not":{"$elemMatch":{"user":{$nin:[1,5,7]}}}}})这就是说,找到所有没有元素不在列表1,5,7之外的文档。

森栏

我不知道更好,但是有几种不同的方法可以解决此问题,具体取决于您可用的MongoDB版本。不太确定这是否符合您的意图,但是所示查询将与第一个文档示例匹配,因为在实现逻辑时,您正在匹配文档数组中必须包含在样本数组中的元素。因此,如果您实际上希望文档包含所有这些元素,那么$all操作员将是显而易见的选择:db.collection.find({ "users.user": { "$all": [ 1, 5, 7 ] } })但是,在假设您的逻辑实际上是预期的前提下,至少根据建议,您可以通过与$in运算符组合来“过滤”这些结果,从而减少需要您处理的文档$where**条件在评估的JavaScript中:db.collection.find({&nbsp; &nbsp; "users.user": { "$in": [ 1, 5, 7 ] },&nbsp; &nbsp; "$where": function() {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; var ids = [1, 5, 7];&nbsp; &nbsp; &nbsp; &nbsp; return this.users.every(function(u) {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return ids.indexOf(u.user) !== -1;&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }})尽管实际扫描的结果将与匹配文档中数组中元素的数量相乘,但您会得到一个索引,但是与没有附加过滤器相比,它仍然更好。甚至可能是你考虑的逻辑抽象$and结合使用,运营商$or也可能是$size根据您的实际情况数组操作:db.collection.find({&nbsp; &nbsp; "$or": [&nbsp; &nbsp; &nbsp; &nbsp; { "users.user": { "$all": [ 1, 5, 7 ] } },&nbsp; &nbsp; &nbsp; &nbsp; { "users.user": { "$all": [ 1, 5 ] } },&nbsp; &nbsp; &nbsp; &nbsp; { "users.user": { "$all": [ 1, 7 ] } },&nbsp; &nbsp; &nbsp; &nbsp; { "users": { "$size": 1 }, "users.user": 1 },&nbsp; &nbsp; &nbsp; &nbsp; { "users": { "$size": 1 }, "users.user": 5 },&nbsp; &nbsp; &nbsp; &nbsp; { "users": { "$size": 1 }, "users.user": 7 }&nbsp; &nbsp; ]})因此,这是匹配条件所有可能排列的产物,但是性能可能会根据可用的安装版本而有所不同。注意:实际上,在这种情况下完全失败,因为这样做完全不同,并且实际上导致逻辑上的失败。$in备选方案是使用聚合框架,这取决于收集中的文档数量,MongoDB 2.6及更高版本的一种方法可能会影响哪种效率最高。db.problem.aggregate([&nbsp; &nbsp; // Match documents that "could" meet the conditions&nbsp; &nbsp; { "$match": {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; "users.user": { "$in": [ 1, 5, 7 ] }&nbsp;&nbsp; &nbsp; }},&nbsp; &nbsp; // Keep your original document and a copy of the array&nbsp; &nbsp; { "$project": {&nbsp; &nbsp; &nbsp; &nbsp; "_id": {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "_id": "$_id",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "date": "$date",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "users": "$users"&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; "users": 1,&nbsp; &nbsp; }},&nbsp; &nbsp; // Unwind the array copy&nbsp; &nbsp; { "$unwind": "$users" },&nbsp; &nbsp; // Just keeping the "user" element value&nbsp; &nbsp; { "$group": {&nbsp; &nbsp; &nbsp; &nbsp; "_id": "$_id",&nbsp; &nbsp; &nbsp; &nbsp; "users": { "$push": "$users.user" }&nbsp; &nbsp; }},&nbsp; &nbsp; // Compare to see if all elements are a member of the desired match&nbsp; &nbsp; { "$project": {&nbsp; &nbsp; &nbsp; &nbsp; "match": { "$setEquals": [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { "$setIntersection": [ "$users", [ 1, 5, 7 ] ] },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "$users"&nbsp; &nbsp; &nbsp; &nbsp; ]}&nbsp; &nbsp; }},&nbsp; &nbsp; // Filter out any documents that did not match&nbsp; &nbsp; { "$match": { "match": true } },&nbsp; &nbsp; // Return the original document form&nbsp; &nbsp; { "$project": {&nbsp; &nbsp; &nbsp; &nbsp; "_id": "$_id._id",&nbsp; &nbsp; &nbsp; &nbsp; "date": "$_id.date",&nbsp; &nbsp; &nbsp; &nbsp; "users": "$_id.users"&nbsp; &nbsp; }}])因此,该方法使用一些新引入的集合运算符来比较内容,但是当然您需要重组数组才能进行比较。如所指出的,有一个直接的运算符可以做到这一点,$setIsSubset其中在单个运算符中可以实现上述组合运算符的等效功能:db.collection.aggregate([&nbsp; &nbsp; { "$match": {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; "users.user": { "$in": [ 1,5,7 ] }&nbsp;&nbsp; &nbsp; }},&nbsp; &nbsp; { "$project": {&nbsp; &nbsp; &nbsp; &nbsp; "_id": {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "_id": "$_id",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "date": "$date",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "users": "$users"&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; "users": 1,&nbsp; &nbsp; }},&nbsp; &nbsp; { "$unwind": "$users" },&nbsp; &nbsp; { "$group": {&nbsp; &nbsp; &nbsp; &nbsp; "_id": "$_id",&nbsp; &nbsp; &nbsp; &nbsp; "users": { "$push": "$users.user" }&nbsp; &nbsp; }},&nbsp; &nbsp; { "$project": {&nbsp; &nbsp; &nbsp; &nbsp; "match": { "$setIsSubset": [ "$users", [ 1, 5, 7 ] ] }&nbsp; &nbsp; }},&nbsp; &nbsp; { "$match": { "match": true } },&nbsp; &nbsp; { "$project": {&nbsp; &nbsp; &nbsp; &nbsp; "_id": "$_id._id",&nbsp; &nbsp; &nbsp; &nbsp; "date": "$_id.date",&nbsp; &nbsp; &nbsp; &nbsp; "users": "$_id.users"&nbsp; &nbsp; }}])或者采用另一种方法,同时仍然利用$sizeMongoDB 2.6 中的运算符:db.collection.aggregate([&nbsp; &nbsp; // Match documents that "could" meet the conditions&nbsp; &nbsp; { "$match": {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; "users.user": { "$in": [ 1, 5, 7 ] }&nbsp;&nbsp; &nbsp; }},&nbsp; &nbsp; // Keep your original document and a copy of the array&nbsp; &nbsp; // and a note of it's current size&nbsp; &nbsp; { "$project": {&nbsp; &nbsp; &nbsp; &nbsp; "_id": {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "_id": "$_id",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "date": "$date",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "users": "$users"&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; "users": 1,&nbsp; &nbsp; &nbsp; &nbsp; "size": { "$size": "$users" }&nbsp; &nbsp; }},&nbsp; &nbsp; // Unwind the array copy&nbsp; &nbsp; { "$unwind": "$users" },&nbsp; &nbsp; // Filter array contents that do not match&nbsp; &nbsp; { "$match": {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; "users.user": { "$in": [ 1, 5, 7 ] }&nbsp;&nbsp; &nbsp; }},&nbsp; &nbsp; // Count the array elements that did match&nbsp; &nbsp; { "$group": {&nbsp; &nbsp; &nbsp; &nbsp; "_id": "$_id",&nbsp; &nbsp; &nbsp; &nbsp; "size": { "$first": "$size" },&nbsp; &nbsp; &nbsp; &nbsp; "count": { "$sum": 1 }&nbsp; &nbsp; }},&nbsp; &nbsp; // Compare the original size to the matched count&nbsp; &nbsp; { "$project": {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; "match": { "$eq": [ "$size", "$count" ] }&nbsp;&nbsp; &nbsp; }},&nbsp; &nbsp; // Filter out documents that were not the same&nbsp; &nbsp; { "$match": { "match": true } },&nbsp; &nbsp; // Return the original document form&nbsp; &nbsp; { "$project": {&nbsp; &nbsp; &nbsp; &nbsp; "_id": "$_id._id",&nbsp; &nbsp; &nbsp; &nbsp; "date": "$_id.date",&nbsp; &nbsp; &nbsp; &nbsp; "users": "$_id.users"&nbsp; &nbsp; }}])当然,哪一个仍然可以完成,尽管在2.6之前的版本中要花更长的时间:db.collection.aggregate([&nbsp; &nbsp; // Match documents that "could" meet the conditions&nbsp; &nbsp; { "$match": {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; "users.user": { "$in": [ 1, 5, 7 ] }&nbsp;&nbsp; &nbsp; }},&nbsp; &nbsp; // Keep your original document and a copy of the array&nbsp; &nbsp; { "$project": {&nbsp; &nbsp; &nbsp; &nbsp; "_id": {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "_id": "$_id",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "date": "$date",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "users": "$users"&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; "users": 1,&nbsp; &nbsp; }},&nbsp; &nbsp; // Unwind the array copy&nbsp; &nbsp; { "$unwind": "$users" },&nbsp; &nbsp; // Group it back to get it's original size&nbsp; &nbsp; { "$group": {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; "_id": "$_id",&nbsp; &nbsp; &nbsp; &nbsp; "users": { "$push": "$users" },&nbsp; &nbsp; &nbsp; &nbsp; "size": { "$sum": 1 }&nbsp; &nbsp; }},&nbsp; &nbsp; // Unwind the array copy again&nbsp; &nbsp; { "$unwind": "$users" },&nbsp; &nbsp; // Filter array contents that do not match&nbsp; &nbsp; { "$match": {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; "users.user": { "$in": [ 1, 5, 7 ] }&nbsp;&nbsp; &nbsp; }},&nbsp; &nbsp; // Count the array elements that did match&nbsp; &nbsp; { "$group": {&nbsp; &nbsp; &nbsp; &nbsp; "_id": "$_id",&nbsp; &nbsp; &nbsp; &nbsp; "size": { "$first": "$size" },&nbsp; &nbsp; &nbsp; &nbsp; "count": { "$sum": 1 }&nbsp; &nbsp; }},&nbsp; &nbsp; // Compare the original size to the matched count&nbsp; &nbsp; { "$project": {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; "match": { "$eq": [ "$size", "$count" ] }&nbsp;&nbsp; &nbsp; }},&nbsp; &nbsp; // Filter out documents that were not the same&nbsp; &nbsp; { "$match": { "match": true } },&nbsp; &nbsp; // Return the original document form&nbsp; &nbsp; { "$project": {&nbsp; &nbsp; &nbsp; &nbsp; "_id": "$_id._id",&nbsp; &nbsp; &nbsp; &nbsp; "date": "$_id.date",&nbsp; &nbsp; &nbsp; &nbsp; "users": "$_id.users"&nbsp; &nbsp; }}])通常,这会找出不同的方法,尝试一下,看看哪种方法最适合您。$in与您现有表单的简单组合很可能是最好的组合。但是在所有情况下,请确保您具有可以选择的索引:db.collection.ensureIndex({ "users.user": 1 })只要您以某种方式访问它,这将为您提供最佳性能,如此处的所有示例所示。判决我对此很感兴趣,因此最终设计了一个测试用例,以查看性能最佳的产品。因此,首先生成一些测试数据:var batch = [];for ( var n = 1; n <= 10000; n++ ) {&nbsp; &nbsp; var elements = Math.floor(Math.random(10)*10)+1;&nbsp; &nbsp; var obj = { date: new Date(), users: [] };&nbsp; &nbsp; for ( var x = 0; x < elements; x++ ) {&nbsp; &nbsp; &nbsp; &nbsp; var user = Math.floor(Math.random(10)*10)+1,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; group = Math.floor(Math.random(10)*10)+1;&nbsp; &nbsp; &nbsp; &nbsp; obj.users.push({ user: user, group: group });&nbsp; &nbsp; }&nbsp; &nbsp; batch.push( obj );&nbsp; &nbsp; if ( n % 500 == 0 ) {&nbsp; &nbsp; &nbsp; &nbsp; db.problem.insert( batch );&nbsp; &nbsp; &nbsp; &nbsp; batch = [];&nbsp; &nbsp; }}&nbsp;集合中有10000个文档,其中长度为1..10的随机数组保持1..0的随机值,我得出了430个文档的匹配计数(从$inmatch的7749减少),结果如下(平均):JavaScript with $in子句:420ms总有$size:395ms带有组数组计数的聚合:650ms包含两个集合运算符的集合:275ms聚合时间$setIsSubset:250ms请注意,除了最后两个样本外,所有样本均完成了约100ms 的峰值方差,并且最后两个样本均显示了220ms的响应。最大的变化是在JavaScript查询中,该查询的结果也慢了100毫秒。但是这里的要点是与硬件相关的,在我的笔记本电脑上的VM下,硬件并不是特别出色,但是可以提供一个思路。因此,总体而言,特别是具有集合运算符的MongoDB 2.6.1版本显然会在性能上胜出,而$setIsSubset作为单个运算符还会带来一点额外收益。鉴于(如2.4兼容方法所示),此过程中的最大开销将是$unwind语句(超过100ms avg),因此,这特别有趣,因此,$in选择的平均时间约为32ms,其余流水线阶段将在不到100ms内执行一般。这样就给出了聚合与JavaScript性能的相对概念。

小唯快跑啊

我只是花了大部分时间试图通过对象比较而不是严格的相等性来实现上述Asya的解决方案。所以我想在这里分享。假设您将问题从userIds扩展到了完整用户。您想查找所有文档,其中其users数组中的每个项目都出现在另一个用户数组中:[{user: 1, group: 3}, {user: 2, group: 5},...]这是行不通的:db.collection.find({"users":{"$not":{"$elemMatch":{"$nin":[{user: 1, group: 3},{user: 2, group: 5},...]}}}}})因为$ nin仅适用于严格的平等。因此,我们需要找到一种不同的方式来表示对象数组的“不在数组中”。并且使用$where会大大降低查询速度。解:db.collection.find({&nbsp;"users": {&nbsp; &nbsp;"$not": {&nbsp; &nbsp; &nbsp;"$elemMatch": {&nbsp; &nbsp; &nbsp; &nbsp;// if all of the OR-blocks are true, element is not in array&nbsp; &nbsp; &nbsp; &nbsp;"$and": [{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// each OR-block == true if element != that user&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"$or": [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"user": { "ne": 1 },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"group": { "ne": 3 }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;]&nbsp; &nbsp; &nbsp; &nbsp;}, {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"$or": [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"user": { "ne": 2 },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"group": { "ne": 5 }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;]&nbsp; &nbsp; &nbsp; &nbsp;}, {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// more users...&nbsp; &nbsp; &nbsp; &nbsp;}]&nbsp; &nbsp; &nbsp;}&nbsp; &nbsp;}&nbsp;}})完善逻辑:$ elemMatch匹配数组中没有用户的所有文档。因此$ not将匹配数组中所有用户的所有文档。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

MongoDB