首页 文章

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

提问于
浏览
507

我有一个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 } }})

如何选择大小大于1的 name 数组的所有文档(最好不必修改当前数据结构)?

10 回答

  • 391

    现在有一种更有效的方法可以在MongoDB 2.2中执行此操作,因为您可以在查询对象键中使用数字数组索引 .

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

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

    db.accommodations.createIndex(
        {'name.1': 1},
        {partialFilterExpression: {'name.1': {$exists: true}}}
    );
    
  • -5

    Update:

    对于mongodb版本 2.2+ 更有效的方法来执行 @JohnnyHK 描述的另一个answer .


    1.使用$where

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

    但...

    Javascript执行速度比本页列出的本机运算符慢,但非常灵活 . 有关更多信息,请参阅服务器端处理页面 .

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

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

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

  • 1078

    你也可以使用聚合:

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

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

  • 42
    db.accommodations.find({"name":{"$exists":true, "$ne":[], "$not":{"$size":1}}})
    
  • 11

    尝试做这样的事情:

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

    1是数字,如果你想获取大于50的记录然后做ArrayName.50谢谢 .

  • 29

    以上都不适合我 . 这个人这样做了我分享它:

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

    虽然上面的答案都可行,但你最初尝试做的是正确的方法,但是你只需要向后语法(切换“$ size”和“$ gt”) .

    正确:

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

    不正确:

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

    我相信这是回答您问题的最快查询,因为它不使用解释的 $where 子句:

    {$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" ] }
    >
    
  • 10

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

    比较query operatorsaggregation comparison operators .

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

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

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

    第一个$ match聚合使用对所有文档都为true的参数 . 如果空白,我会得到

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

相关问题