當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


MongoDB $isArray用法及代碼示例

MongoDB 提供了不同類型的中使用的 rray 表達式運算符聚合管道階段$isArray 操作符就是其中之一。這操作員用於檢查是否指定的表達式是否為數組。或者換句話說,這個運算符用於檢查操作數是一個數組。此運算符將返回真正如果指定的表達式是一個數組。否則,它將返回false。

用法:

{ $isArray:[ <expression> ] }

這裏,表達式必須是有效的表達式。

例子:

在以下示例中,我們正在使用:



Database: GeeksforGeeks

Collection:arrayExample

Document: three documents that contain the details in the form of field-value pairs.

使用 $isArray 運算符:

在此示例中,我們將使用 $isArray 運算符檢查 vegiie 和 name 字段的值是否為數組。這裏,checkResult1 的值為真,因為 vegiie 是一個數組,而 checkResult2 的值為假,因為 name 不是一個數組。

db.arrayExample.aggregate([
... {$match:{name:"Bongo"}},
... {$project:{
... checkResult1:{$isArray:"$vegiie"},
... checkResult2:{$isArray:"$name"}}}])

在嵌入式文檔中使用 $isArray 運算符:

在本例中,在本例中,我們將使用 $isArray 運算符檢查 favGame.indoorGames 字段的值是否為數組。這裏,checkResult1 的值為真,因為 favGame.indoorGames 是一個數組,而 checkResult2 的值也為真,因為 $isArray 運算符中的給定表達式是一個數組(即 [1, 3, 4])。

db.arrayExample.aggregate([
... {$match:{name:"Piku"}},
... {$project:{
... checkResult1:{$isArray:"$favGame.indoorGames"},
... checkResult2:{$isArray:[[1, 3, 4]]}}}])

相關用法


注:本文由純淨天空篩選整理自ankita_saini大神的英文原創作品 MongoDB $isArray Operator。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。