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

MongoDB数据库的操作,增删改查

时间:2017-01-07 12:14:10      阅读:269      评论:0      收藏:0      [点我收藏+]

标签:log   employee   南京   牛津大学   逻辑   运算   turn   lin   pre   

在student集合中插入一些数据

db.student.insert({
"学号":10010,
"姓名":"德莱文",
"年龄":26,
"地址":"淄博市"
});


var stuData1={
"学号":10020,
"姓名":"奥巴马",
"年龄":27,
"地址":"艾泽拉斯"
};
db.student.insert(stuData1);


var stuData2={
"学号":10030,
"姓名":"女枪",
"年龄":27,
"地址":"艾泽拉斯"
};
db.student.insert(stuData2);

var stuData3={
"学号":10050,
"姓名":"瑞雯",
"年龄":18,
"地址":"超神学院"
};
db.student.insert(stuData3);

  插入数组(数据为json)

var stuData=[
{"姓名":"Anner","age":26},
{"姓名":"James","age":28},
{"姓名":"Allen","age":31},
{"姓名":"Smith","age":17}
];
db.student.insert(stuData);

  插入insert(),用JavaScript操作

//添加1万条数据
for(var i=0;i<10000;i++){
db.student.insert({"vid":"www.oracle.com"+i+"line"});
};

  查看数据

db.student.find();

  ●数据查询

   示例1:查询学号为10020的记录

db.student.find({"学号":10020});
db.student.find({"学号":10020}).pretty();

  示例2:不显示_id的查询

db.student.find({"学号":10020},{"_id":0});

  ●关系运算

> $gt
< $lt
>= $gte
<= $lte
!= $ne
= key:value $eq

定义一个员工信息集合

db.employee.drop();
db.employee.insert([
{"name":"Smith","age":25,"sex":"男","job":"教授","addr":"北京大学","cellphone":"17166660001"},
{"name":"Clark","age":29,"sex":"男","job":"讲师","addr":"燕京大学","cellphone":"17166660002"},
{"name":"Allen","age":28,"sex":"女","job":"主任","addr":"南京大学","cellphone":"17166660003"},
{"name":"Anner","age":15,"sex":"女","job":"宣传","addr":"东京大学","cellphone":"17166660004"},
{"name":"James","age":19,"sex":"男","job":"教务","addr":"西京大学","cellphone":"17166660005"}
]);
db.employee.find();
db.employee.find().pretty();

  示例3:查询姓名为Anner的记录

db.employee.find({"name":"Anner"}).pretty();

  示例4:查询性别为男的记录

db.employee.find({"sex":"男"}).pretty();

  示例5:查询年龄大于19岁的记录

db.employee.find({"age":{"$gt":19}}).pretty();

  示例6:查询年龄小于等于19岁的记录

db.employee.find({"age":{"$lte":19}}).pretty();

  示例7:查询性别不是女的的记录

db.employee.find({"sex":{"$ne":"女"}}).pretty();

  ●逻辑运算

与 $and
或 $or
非 $not $nor

  示例8:查询年龄在10~20之间的记录

db.employee.find({"age":{"$gt":10,"$lte":20}});
db.employee.find({"age":{"$gt":10,"$lte":20}}).pretty();

  示例9:查询年龄不是19岁的记录

db.employee.find({"age":{"$ne":19}});

  示例10:查询年龄大于19岁,或者性别为女的记录

db.employee.find({"age":{"$gt":19}},);

db.employee.find({"$or":[
	{"age":{"$gt":19}},
	{"sex":{"$eq":"女"}}
]});

  示例11:对上一题求反

db.employee.find({"$nor":[
	{"age":{"$gt":19}},
	{"sex":{"$eq":"女"}}
]});

  ●模运算 "$mod"

  示例12:求年龄mod27余1的记录,也就是28

db.employee.find({"age":{"$mod":[27,1]}});

  

  ●范围查询
  $in $nin

  示例13:查询姓名是Allen,Anner,James的记录

db.employee.find({"name":{"$in":["Allen","Anner","James"]}});

  示例14:查询姓名不是Allen,Anner,James的记录

db.employee.find({"name":{"$nin":["Allen","Anner","James"]}})

  构建一个集合 employee集合

db.employee.insert([
{"name":"Jones","age":31,"sex":"男","job":"技术员","addr":"哈佛大学","cellphone":"15166660001"},
{"name":"Blake","age":35,"sex":"男","job":"销售员","addr":"牛津大学","cellphone":"15166660002"},
{"name":"Martin","age":21,"sex":"男","job":"销售员","addr":"剑桥大学","cellphone":"15166660003"},
{"name":"Turner","age":25,"sex":"女","job":"后勤","addr":"北京大学","cellphone":"15166660004"},
{"name":"Miller","age":23,"sex":"女","job":"行政","addr":"南京大学","cellphone":"15166660005"}
]);

  示例15:查询job不是"技术员"的记录

db.emp.find({"jod":{"$nin":["技术员"]}});

  

MongoDB数据库的操作,增删改查

标签:log   employee   南京   牛津大学   逻辑   运算   turn   lin   pre   

原文地址:http://www.cnblogs.com/void-m/p/6258980.html

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