1

I have a document with the following structure

{testfields:[{"test_id":1,"test_name":"xxxx"}]},

but , my application don't no whether "testfield" is array or plain object, now if I apply "$unwind" in aggregate query and filed is not of array type "mongodb" throws error, that cannot unwind. I wanted to know is their a way I can check if the field is of type array than apply unwind, else treat is as normal object.

Phalguni Mukherjee
  • 623
  • 3
  • 11
  • 29

1 Answers1

0

I think this could help you.

Before using unwind, try to use a match to filter documents with testfields field is an array:

$match: { $where : "Array.isArray(this.testfields)" }

Then, you could use unwind without errors.

danidelvalle
  • 146
  • 1
  • 7
  • You're right. I got confused because documentation says "The $match query syntax is identical to the read operation query syntax", but i didn't see last warning 'You cannot use $where in $match queries as part of the aggregation pipeline'. My apologies. – danidelvalle Aug 30 '13 at 06:27
  • How about querying on $type 4 http://docs.mongodb.org/manual/reference/operator/query/type/ ? – Alan Spencer Nov 22 '13 at 17:55