In Mongodb, how to search for documents by the size of an Array

Asked

Viewed 1,023 times

0

I have a collection of documents that are nodes of a graph, where each node has its adjacency list:

{
'_id': ObjectId('547ce6f4ffaba82f360fc525'),
'adj': ['no_2'],
'nome': 'no_1'
}
{
'_id': ObjectId('547ce6f4ffaba82f360fc526'),
'adj': ['no_1', 'no_3'],
'nome': 'no_2'
}
{
'_id': ObjectId('547ce6f4ffaba82f360fc527'),
'adj': ['no_1'],
'nome': 'no_3'
}

I want the list of all documents (nodes) where the size of the adj array is greater than or equal to an X value.

I tried to solve using $gt and $size but both the operator $size only returns by exact value, when it does not seem possible to use the two operators together.

1 answer

2

(1) I found a solution using the $Where:

db.grafo.find( { $where: "this.adj.length > X" } );

The problem with this solution is that this strategy makes no use of indexes or any other strategy to make the search efficient: the operator $Where basically runs the operator for all documents in the collection.

(2) A second strategy with more "Nosql face" is to create a new field that stores the size of the array. This way you can index the value, but it is more a value to update when the array is modified.

(3) A very interesting third solution: ask if a certain array position exists!

Y=X-1
db.grafo.find( { "adj.Y" : {"$exists": true} } );

OBS.1: I just couldn’t tell if there is any difference in efficiency between (1) and (3). It seems to me that.

OBS.2: Both solution (1) and (3) are only possible in Mongo 2.2+

  • The solution (1) uses an interpreted query in all Collection documents, so it will probably be much slower than the solution (3), which is the most recommended. I have already used the solution (3) in production, no problems. It is a common practice.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.