前言:MongoDB.aspx' >MongoDB自带一个javascript shell客户端,因此支持javascript语法及大部分函数。
MongoDB常用基本数据类型介绍:
- null:空值或者不存在的字段
- Boolean:true,false
- 数值型:MongoDB中数值型默认为Double,可以使用NumberInt()函数及NumberLong()函数分别指定某个字段为整型和长整型。
- 字符串:
- 日期:new Date().getTime();//获取当前时间的时间戳
- 数组:var uArray=[ "张三","李四","王五"];
MongoDB基本操作
增加数据:
单体插入:
var p1 ={ "name":"张杰", "age":NumberInt("28") ,//MongoDB中数值类型默认为Double "sex":false}db.user.insert(p1);
View Code
批量插入:
1. 直接插入一个数组:
var p2=[ { "name":"张杰", "age":NumberInt("28") ,//MongoDB中数值类型默认为Double "sex":false }, { "name":"李娜", "age":NumberInt("33") ,//MongoDB中数值类型默认为Double "sex":true }, { "name":"唐嫣", "age":NumberInt("28") ,//MongoDB中数值类型默认为Double "sex":true }]db.user.insert(p2);
View Code
2. 循环插入
var p2=[ { "name":"李晨", "age":NumberInt("28") ,//MongoDB中数值类型默认为Double "sex":false }, { "name":"邓超", "age":NumberInt("33") ,//MongoDB中数值类型默认为Double "sex":false }, { "name":"郑凯", "age":NumberInt("28") ,//MongoDB中数值类型默认为Double "sex":false }]for(var i=0;i<p2.length;i++){ db.user.insert(p2[i]);}
View Code
删除数据
删除user集合中的所有文档:
db.user.remove();//删除user集合中的所有文档
View Code
删除指定集合:
var where={ "name":"郑凯"};db.user.remove(where);//删除user集合中所有"name"为"郑凯"的文档
View Code
直接删除集合
db.user.drop();//删除user集合
View Code
原标题:MongoDB学习笔记(一)
关键词:MongoDB