2013-08-30 135 views
1
I have a collection having following data: 

{"_id" : ObjectId("5220222f8188d30ce85d61cc"), 

「testfields」:[{ 「爲test_id」:1, 「TEST_NAME」: 「XXXX」 }] }

when I query : 
db.testarray.find({ "testfields" : { "$type" : 4 } }) 

it returns no data,but same returns data when I do: 
db.testarray.find({ "$where" : "Array.isArray(this.testfields)" }) 

It returns data, do the type:4 identifies some other kind of list? 

回答

1

因爲testfieldsArray,$type : 4將對testfields中的每個元素執行「is array」檢查,而不是testfields本身。由於您的testfields只包含一個Object,因此不會返回。

如果在另一方面,你插入以下到您的收藏,

db.testarray.insert({ "testfields" : [ { "test_id" : 1, "test_name" : "xxxx" }, 
             [ "a", "b" ] ] }); 

它會得到恢復,因爲現在的testfields的要素之一是Array

更多信息可在docs解釋。

+0

thanx,但我想只檢查它的testfields。我無法使用$,因爲它不適用於$匹配。 –