Mongo查询数组

huangapple go评论65阅读模式
英文:

Mongo Query on array

问题

我需要一个针对这个示例模式的匹配聚合查询

查询1:如果所有名称都是null或未分配,则获取文档
查询2:如果至少有一个名称被填充,则获取文档

我尝试过使用“addFields”和求和null计数,但我想知道是否有更好的方法
英文:

I need to a match aggregation query for this sample schema

{
 "items": [
   {name:'Test1'},{name: null}
 ]
}

Query1: get documents if all name is null or unassigned
Query2: get documents if at least 1 name is filled

I tried "addFields" with sum null count but i was wondering if there is a better way

答案1

得分: 1

您可以在聚合管道的 $match 阶段中使用 $elemMatch$ne 运算符来实现您提到的两个查询。

对于 Query1,$elemMatch 运算符用于匹配至少有一个数组元素满足指定条件的文档。在这种情况下,它检查 "items" 数组中的任何 "name" 值是否不等于 null,而 $not 运算符否定条件,以匹配所有 "name" 值都为 null 的文档。以下是一个示例聚合管道:

db.collection.aggregate([
  {
    $match: {
      "items": { $not: { $elemMatch: { "name": { $ne: null } } } }
    }
  }
])

上面的聚合管道返回所有 "items" 数组中所有 "name" 值都为 null,且没有非空值的文档。

对于 Query2,您可以使用 $elemMatch 运算符来匹配至少有一个元素在 items 数组中具有非空的 name 值的文档。以下是一个示例聚合管道:

db.collection.aggregate([
  {
    $match: {
      "items": { $elemMatch: { "name": { $ne: null } } }
    }
  }
])

上述管道匹配具有至少一个元素在 items 数组中具有非空 name 值的文档。

您可以向管道添加更多阶段,根据需要进一步转换或投影匹配的文档。

英文:

You can use the $elemMatch and $ne operators in the $match stage of the aggregation pipeline to achieve the two queries you mentioned.

> For Query1, the $elemMatch operator is used to match documents where at least one array element satisfies the specified condition. In this case, it checks if any "name" value in the "items" array is not equal to null, and the $not operator negates the condition to match documents where all "name" values are null. Here's an example aggregation pipeline:

db.collection.aggregate([
  {
    $match: {
      "items": { $not: { $elemMatch: { "name": { $ne: null } } } }
    }
  }
])

The above aggregation pipeline returns all documents where all "name" values in the "items" array are null, and none of them are non-null.

> For Query2, you can use the $elemMatch operator to match documents where at least one element in the items array has a non-null name value. Here's an example aggregation pipeline:

db.collection.aggregate([
  {
    $match: {
      "items": { $elemMatch: { "name": { $ne: null } } }
    }
  }
])

The above pipeline matches documents where at least one element in the items array has a name value that is not null.

You can add more stages to the pipelines to further transform or project the matching documents as needed.

答案2

得分: 0

以下是翻译好的部分:

MongoDb存储未定义(undefined)以及可以使用null。
因为如果键存在,它的值是null,所以我们可以在两种情况下与null匹配。
如果键具有某些已存在的值,那么我们可以与非null进行匹配,然后从数据库中返回已存在的值。

1. 当我们需要的数据不是(null或undefined):

db.collection.aggregate([
  {
    "$unwind": "$items"
  },
  {
    $match: {
      "items.name": {
        $ne: null
      }
    }
  }
])

2. 当我们需要的数据是null或undefined:

db.collection.aggregate([
  {
    "$unwind": "$items"
  },
  {
    $match: {
      "items.name": {
        $eq: null
      }
    }
  }
])
英文:

MongoDb store undefined as well but we can with with null as well.
because of if key exists it has null so we can match with null in both cases.
if key has some exists value then we can match with not equal to null then it
returns existed values from db

1. when we want data not (null or undefined) :

 db.collection.aggregate([
  {
    "$unwind": "$items"
  },
  {
    $match: {
      "items.name": {
        $ne: null
      }
    }
  }
])

2. when we want data null or undefined :

    db.collection.aggregate([
  {
    "$unwind": "$items"
  },
  {
    $match: {
      "items.name": {
        $eq: null
      }
    }
  }
])

huangapple
  • 本文由 发表于 2023年2月27日 14:08:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/75577225.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定