查询数组大小大于1的文档

发布于 2024-12-10 18:02:19 字数 655 浏览 1 评论 0原文

我有一个 MongoDB 集合,其中包含以下格式的文档:

{
  "_id" : ObjectId("4e8ae86d08101908e1000001"),
  "name" : ["Name"],
  "zipcode" : ["2223"]
}
{
  "_id" : ObjectId("4e8ae86d08101908e1000002"),
  "name" : ["Another ", "Name"],
  "zipcode" : ["2224"]
}

我当前可以获得与特定数组大小匹配的文档:

db.accommodations.find({ name : { $size : 2 }})

这会正确返回 name 数组中包含 2 个元素的文档。但是,我无法执行 $gt 命令来返回 name 字段的数组大小大于 2 的

db.accommodations.find({ name : { $size: { $gt : 1 } }})

所有文档:How can I select all document with a name 数组的大小大于一(最好不必修改当前数据结构)?

I have a MongoDB collection with documents in the following format:

{
  "_id" : ObjectId("4e8ae86d08101908e1000001"),
  "name" : ["Name"],
  "zipcode" : ["2223"]
}
{
  "_id" : ObjectId("4e8ae86d08101908e1000002"),
  "name" : ["Another ", "Name"],
  "zipcode" : ["2224"]
}

I can currently get documents that match a specific array size:

db.accommodations.find({ name : { $size : 2 }})

This correctly returns the documents with 2 elements in the name array. However, I can't do a $gt command to return all documents where the name field has an array size of greater than 2:

db.accommodations.find({ name : { $size: { $gt : 1 } }})

How can I select all documents with a name array of a size greater than one (preferably without having to modify the current data structure)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(16

穿越时光隧道 2024-12-17 18:02:19

现在,您可以在查询对象键中使用数字数组索引(从 0 开始),在 MongoDB 2.2+ 中,有一种更有效的方法可以做到这一点。

// Find all docs that have at least two name array elements.
db.accommodations.find({'name.1': {$exists: true}})

您可以通过使用部分过滤表达式的索引来支持此查询(需要 3.2+):

// index for at least two name array elements
db.accommodations.createIndex(
    {'name.1': 1},
    {partialFilterExpression: {'name.1': {$exists: true}}}
);

There's a more efficient way to do this in MongoDB 2.2+ now that you can use numeric array indexes (0 based) in query object keys.

// Find all docs that have at least two name array elements.
db.accommodations.find({'name.1': {$exists: true}})

You can support this query with an index that uses a partial filter expression (requires 3.2+):

// index for at least two name array elements
db.accommodations.createIndex(
    {'name.1': 1},
    {partialFilterExpression: {'name.1': {$exists: true}}}
);
甜扑 2024-12-17 18:02:19

更新:

对于 mongodb 版本2.2+,更有效的方法由 @JohnnyHK 在另一个 答案


  1. 使用$where

    db.accommodations.find( { $where: "this.name.length > 1" } );

但...

Javascript 的执行速度比上面列出的本机运算符慢
这个页面,可是非常灵活的。参见服务器端处理页面
了解更多信息。

  1. 创建额外字段NamesArrayLength,使用名称数组长度更新它,然后在查询中使用:

    db.accommodations.find({"NamesArrayLength": {$gt: 1} });

这将是更好的解决方案,并且工作速度更快(您可以在其上创建索引)。

Update:

For mongodb versions 2.2+ more efficient way to do this described by @JohnnyHK in another answer.


  1. Using $where

    db.accommodations.find( { $where: "this.name.length > 1" } );

But...

Javascript executes more slowly than the native operators listed on
this page, but is very flexible. See the server-side processing page
for more information.

  1. Create extra field NamesArrayLength, update it with names array length and then use in queries:

    db.accommodations.find({"NamesArrayLength": {$gt: 1} });

It will be better solution, and will work much faster (you can create index on it).

も星光 2024-12-17 18:02:19

我相信这是回答您的问题最快的查询,因为它不使用解释的 $where 子句 - 它使用 $size 数组运算符$exists 元素运算符$nor 逻辑运算符

{$nor: [
    {name: {$exists: false}},
    {name: {$size: 0}},
    {name: {$size: 1}}
]}

它的意思是“除了那些没有一个名称(不存在或空数组)或只有一个名称。”

测试:

> db.test.save({})
> db.test.save({name: []})
> db.test.save({name: ['George']})
> db.test.save({name: ['George', 'Raymond']})
> db.test.save({name: ['George', 'Raymond', 'Richard']})
> db.test.save({name: ['George', 'Raymond', 'Richard', 'Martin']})
> db.test.find({$nor: [{name: {$exists: false}}, {name: {$size: 0}}, {name: {$size: 1}}]})
{ "_id" : ObjectId("511907e3fb13145a3d2e225b"), "name" : [ "George", "Raymond" ] }
{ "_id" : ObjectId("511907e3fb13145a3d2e225c"), "name" : [ "George", "Raymond", "Richard" ] }
{ "_id" : ObjectId("511907e3fb13145a3d2e225d"), "name" : [ "George", "Raymond", "Richard", "Martin" ] }
>

I believe this is the fastest query that answers your question, because it doesn't use an interpreted $where clause -- it uses the $size array operator and the $exists element operator combined with $nor logical operator

{$nor: [
    {name: {$exists: false}},
    {name: {$size: 0}},
    {name: {$size: 1}}
]}

It means "all documents except those without a name (either non existant or empty array) or with just one name."

Test:

> db.test.save({})
> db.test.save({name: []})
> db.test.save({name: ['George']})
> db.test.save({name: ['George', 'Raymond']})
> db.test.save({name: ['George', 'Raymond', 'Richard']})
> db.test.save({name: ['George', 'Raymond', 'Richard', 'Martin']})
> db.test.find({$nor: [{name: {$exists: false}}, {name: {$size: 0}}, {name: {$size: 1}}]})
{ "_id" : ObjectId("511907e3fb13145a3d2e225b"), "name" : [ "George", "Raymond" ] }
{ "_id" : ObjectId("511907e3fb13145a3d2e225c"), "name" : [ "George", "Raymond", "Richard" ] }
{ "_id" : ObjectId("511907e3fb13145a3d2e225d"), "name" : [ "George", "Raymond", "Richard", "Martin" ] }
>
等数载,海棠开 2024-12-17 18:02:19

您也可以使用聚合:

db.accommodations.aggregate(
[
     {$project: {_id:1, name:1, zipcode:1, 
                 size_of_name: {$size: "$name"}
                }
     },
     {$match: {"size_of_name": {$gt: 1}}}
])

// 将“size_of_name”添加到运输文档并使用它来过滤名称的大小

You can use aggregate, too:

db.accommodations.aggregate(
[
     {$project: {_id:1, name:1, zipcode:1, 
                 size_of_name: {$size: "$name"}
                }
     },
     {$match: {"size_of_name": {$gt: 1}}}
])

// you add "size_of_name" to transit document and use it to filter the size of the name

筱果果 2024-12-17 18:02:19

您可以使用 $expr (3.6 mongo 版本运算符)来使用聚合常规查询中的函数。

比较 查询运算符聚合比较运算符

db.accommodations.find({$expr:{$gt:[{$size:"$name"}, 1]}})

You can use $expr ( 3.6 mongo version operator ) to use aggregation functions in regular query.

Compare query operators vs aggregation comparison operators.

db.accommodations.find({$expr:{$gt:[{$size:"$name"}, 1]}})
辞别 2024-12-17 18:02:19

尝试执行以下操作:

db.getCollection('collectionName').find({'ArrayName.1': {$exists: true}})

1 是数字,如果要获取大于 50 的记录,则执行 ArrayName.50
谢谢。

Try to do something like this:

db.getCollection('collectionName').find({'ArrayName.1': {$exists: true}})

1 is number, if you want to fetch record greater than 50 then do ArrayName.50
Thanks.

路弥 2024-12-17 18:02:19

MongoDB 3.6 包含 $expr
https://docs.mongodb.com/manual/reference/operator/query/ expr/

您可以使用 $expr 来计算 $match 或 find 中的表达式。

{ $match: {
           $expr: {$gt: [{$size: "$yourArrayField"}, 0]}
         }
}

或找到

collection.find({$expr: {$gte: [{$size: "$yourArrayField"}, 0]}});

MongoDB 3.6 include $expr
https://docs.mongodb.com/manual/reference/operator/query/expr/

You can use $expr in order to evaluate an expression inside a $match, or find.

{ $match: {
           $expr: {$gt: [{$size: "$yourArrayField"}, 0]}
         }
}

or find

collection.find({$expr: {$gte: [{$size: "$yourArrayField"}, 0]}});
暖风昔人 2024-12-17 18:02:19

虽然上面的答案都有效,但您最初尝试做的是正确的方法,但是您只是向后使用了语法(切换“$size”和“$gt”)..

正确:

db.collection.find({items: {$gt: {$size: 1}}})

Although the above answers all work, What you originally tried to do was the correct way, however you just have the syntax backwards (switch "$size" and "$gt")..

Correct:

db.collection.find({items: {$gt: {$size: 1}}})
始终不够 2024-12-17 18:02:19

以上都不适合我。这个人做到了,所以我分享它:

db.collection.find( {arrayName : {$exists:true}, $where:'this.arrayName.length>1'} )

None of the above worked for me. This one did so I'm sharing it:

db.collection.find( {arrayName : {$exists:true}, $where:'this.arrayName.length>1'} )
天气好吗我好吗 2024-12-17 18:02:19
db.accommodations.find({"name":{"$exists":true, "$ne":[], "$not":{"$size":1}}})
db.accommodations.find({"name":{"$exists":true, "$ne":[], "$not":{"$size":1}}})
給妳壹絲溫柔 2024-12-17 18:02:19

我找到了这个解决方案,查找数组字段大于特定长度的项目

db.allusers.aggregate([
    { $match: { username: { $exists: true } } },
    { $project: { count: { $size: "$locations.lat" } } },
    { $match: { count: { $gt: 20 } } },
]);

第一个 $match 聚合使用一个对所有文档都适用的参数。没有它,我会得到一个错误异常

"errmsg" : "exception: The argument to $size must be an Array, but was of type: EOO"

I found this solution, to find items with an array field greater than certain length

db.allusers.aggregate([
    { $match: { username: { $exists: true } } },
    { $project: { count: { $size: "$locations.lat" } } },
    { $match: { count: { $gt: 20 } } },
]);

The first $match aggregate uses an argument that's true for all the documents. Without it, I would get an error exception

"errmsg" : "exception: The argument to $size must be an Array, but was of type: EOO"
初熏 2024-12-17 18:02:19

您可以通过 MongoDB 聚合来完成该任务:

db.collection.aggregate([
  {
    $addFields: {
      arrayLength: {$size: '$array'}
    },
  },
  {
    $match: {
      arrayLength: {$gt: 1}
    },
  },
])

You can MongoDB aggregation to do the task:

db.collection.aggregate([
  {
    $addFields: {
      arrayLength: {$size: '$array'}
    },
  },
  {
    $match: {
      arrayLength: {$gt: 1}
    },
  },
])
可是我不能没有你 2024-12-17 18:02:19

这对你有用

db.collection.find({
  $expr: {
    $gt: [{ $size: "$arrayField" }, 1]
  }
})

this will work for you

db.collection.find({
  $expr: {
    $gt: [{ $size: "$arrayField" }, 1]
  }
})
清晰传感 2024-12-17 18:02:19

这也适用于指南针。这是我在没有索引的情况下尝试过的最快的。

 {$expr: {
            $gt: [
              {
                $size: { "$ifNull": [ "$name", [] ] }
              },
              1
            ]
          }}

This will work in Compass also. This is the fastest of all i have tried without indexing.

 {$expr: {
            $gt: [
              {
                $size: { "$ifNull": [ "$name", [] ] }
              },
              1
            ]
          }}
夏の忆 2024-12-17 18:02:19

你可以使用 $expr 来覆盖这个

// $expr: Allows the use of aggregation expressions within the query language.
// syntax: {$expr: {<expression>}}

db.getCollection("person_service").find(
    { 
        "$expr" : { 
            // services is Array, find services length gt 3
            "$gt" : [
                { 
                    "$size" : "$services"
                }, 
                3.0
            ]
        }
    }
)

you can use $expr to cover this

// $expr: Allows the use of aggregation expressions within the query language.
// syntax: {$expr: {<expression>}}

db.getCollection("person_service").find(
    { 
        "$expr" : { 
            // services is Array, find services length gt 3
            "$gt" : [
                { 
                    "$size" : "$services"
                }, 
                3.0
            ]
        }
    }
)
我家小可爱 2024-12-17 18:02:19

这对我有用:

{ $where: function() { return this.name && this.name.length > 2;}}

技巧是在检查长度之前先检查该值是否存在。

我不断收到错误:

find 命令 :: 期间由 :: 引起的执行器错误 TypeError: this.name is undefined : @:1:15

那是因为并非所有文档都有 name 字段(或者您称之为的任何字段,请替换) name 与您感兴趣的数组,例如scores)。

不幸的是,这个错误非常模糊,在意识到这是问题所在之前,我尝试了很多其他方法。

注意:我使用的是 MongoDB (Atlas) v6.0.10

This worked for me:

{ $where: function() { return this.name && this.name.length > 2;}}

The trick was the check for if the value existed first before checking for the length.

I kept getting an error:

Executor error during find command :: caused by :: TypeError: this.name is undefined : @:1:15

That's because not all the documents had a name field (or whatever field you call it, replace name with the array you are interested in, e.g scores).

Unfortunately the error was very vague and I tried a whole bunch of other things before realising this was the issue.

Note: I'm using MongoDB (Atlas) v6.0.10

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文