码迷,mamicode.com
首页 > 数据库 > 详细

MongoDB学习笔记(一)

时间:2015-10-31 21:28:17      阅读:235      评论:0      收藏:0      [点我收藏+]

标签:

前言:MongoDB自带一个javascript shell客户端,因此支持javascript语法及大部分函数。 

MongoDB常用基本数据类型介绍:

  1. null:空值或者不存在的字段
  2. Boolean:true,false
  3. 数值型:MongoDB中数值型默认为Double,可以使用NumberInt()函数及NumberLong()函数分别指定某个字段为整型和长整型。
  4. 字符串:
  5. 日期:new Date().getTime();//获取当前时间的时间戳
  6. 数组: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学习笔记(一)

标签:

原文地址:http://www.cnblogs.com/Jabben/p/4926158.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!