MongoDB是文档型数据库,每个文档(doc)表示数据的一项记录。相比关系型DB的row只能使用简单的数据类型,doc能够使用复杂的数据类型:内嵌doc,数组。MongoDB的数组是一系列元素的集合,使用中括号 [] 表示数组,例如:[1,2,3]的元素是整数值,[{name:"t5"}, {name:"t7"}],[ {name:"t5", age:21}, {name:"t7", age:22} ]的元素是doc。
在MongoDB中,数组元素允许重复,元素的位置是固定的。如果两个数组相等,那么这两个数组的元素和及其位置都相同。
创建示例collection,使用db.collection.insert()函数和数组参数,一次性向集合中插入3个doc。
user1={ name:"t1", age:21}
user2={ name:"t2", age:22}
user3={ name:"t3", age:23}
db.users.insert([user1,user2,user3])一,使用dot标记法(dot notation)访问数组元素
MongoDB使用 dot 访问数组的元素,或内嵌doc字段。
MongoDB uses the dot notation to access the elements of an array and to access the fields of an embedded document.
延伸阅读
学习是年轻人改变自己的最好方式