0

I can query for all the documents in a Mongo collection based on the number of elements in a list like this:

db.myCollection.find( { arrayField : { $size : 0 } } )

But this doesn't work for Maps. How can I query the number of elements in a Map?

Hoepfully this example of 2 records in Mongo gives more information into what I am asking.

{
   "_id" : 1,
   stringField : "someValue",
   arrayField : [ 1, 2, 3 ],
   mapField : {
       "key1" : "value1",
       "key2" : "value2"
   }
}
{
   "_id" : 2,
   stringField : "someValue2",
   arrayField : [ 4 ],
   mapField : {

   }
}

What is the query to retrieve all the documents in the above collection using the fact that mapField has 0 elements? In the above example it should return the record with id of 2.

wxkevin
  • 1,634
  • 2
  • 25
  • 44
  • Look this...if no work in you case, let me know... http://stackoverflow.com/questions/7223273/get-n-th-element-of-an-array-in-mongo?rq=1 – felixmpa Jun 03 '14 at 16:21
  • http://stackoverflow.com/questions/3308119/querying-array-elements-with-mongo?rq=1 – felixmpa Jun 03 '14 at 16:22
  • Neither of those links answer my question. – wxkevin Jun 03 '14 at 18:03
  • Can you put some more information in your question? What is your query for map? What is the result? – displayName Jun 03 '14 at 19:37
  • I guess what you mean here is a key/value pair, or "Hash", "Map", "Dict" whatever floats your boat. But the question could do with some clarification by editing lest it drift away into obscurity. Best solved by a sample document and your expected outcome. – Neil Lunn Jun 04 '14 at 05:23

1 Answers1

2

mapField can be considered as an empty document. You can then query all such documents by db.collection.find({"mapField" : {}})

Tushar Mishra
  • 1,370
  • 1
  • 12
  • 20