Query for documents where array size is greater than 1

后端 未结 14 1915
北海茫月
北海茫月 2020-11-22 03:02

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

{
  \"_id\" : ObjectId(\"4e8ae86d08101908e1000001\"),
  \"name\" : [\"Name\"],
  \"zipcod         


        
相关标签:
14条回答
  • 2020-11-22 03:28

    I believe this is the fastest query that answers your question, because it doesn't use an interpreted $where clause:

    {$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" ] }
    >
    
    0 讨论(0)
  • 2020-11-22 03:34

    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.

    2.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).

    0 讨论(0)
  • 2020-11-22 03:36

    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}}})
    

    Incorrect:

    db.collection.find({items: {$size: {$gt: 1}}})
    
    0 讨论(0)
  • 2020-11-22 03:37

    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]}})
    
    0 讨论(0)
  • 2020-11-22 03:40

    There's a more efficient way to do this in MongoDB 2.2+ now that you can use numeric array indexes 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}}}
    );
    
    0 讨论(0)
  • 2020-11-22 03:40

    I know its old question, but I am trying this with $gte and $size in find. I think to find() is faster.

    db.getCollection('collectionName').find({ name : { $gte : {  $size : 1 } }})
    
    0 讨论(0)
提交回复
热议问题