MongoDB实验——数据库基本操作(头歌)

第1关:创建数据库

命令行

mongo
use mydb
db.mydb.insert({_id:1,name:"李小红"})

第2关:创建集合

命令行输入:

mongo
 use Testdb2
db. t_stu.insert([{_id:1,name:"小明",sex: "男",hobbies: ["乒乓球","羽毛球"]},
{_id:2,name:"小红",sex: "女",hobbies: ["画画","唱歌"]}])

第3关:文档操作一

命令行输入:

mongo
use Testdb3
 document=([{_id:1,name:"张小华",sex: "男",phone: "12356986594",hobbies: ["打篮球","踢足球","唱歌"]}]);
db.stu1.insert(document)
db.stu2.insert(document)    
db.stu3.insert(document)    
db.stu2.update({phone: "12356986594"},{$set:{ phone: "18356971462"}})    
db.stu3.save({_id:1,name:"张晓晓",sex: "女",phone: "12365498704",hobbies: ["跳舞","羽毛球","唱歌"]})

第4关:文档操作二

命令行

mongo

document=([{
name:'张小华',
sex:'男',
age:20,
phone:'12356986594',
hobbies:['打篮球','踢足球','唱歌']
},{
name:'李小红',
sex:'女',
age:18,
phone:'12355487536',
hobbies:['跳舞','唱歌']
}])
db.stu1.insert(document)

代码文件

#********* Begin *********#

echo "

document=([

    {_id:1,name:'西西',sex:'女',age:23,national:'汉族'},

    {_id:2,name:'东东',sex:'男',age:20,national:'苗族'},

    {_id:3,name:'北北',sex:'男',age:19,national:'汉族'},

    {_id:4,name:'南南',sex:'女',age:15,national:'傣族'}]);

db.stu1.insert(document);

db.stu2.insert(document);

db.stu1.find({'age':{\$gte:15},'sex':'女'});

db.stu1.find({'national':'苗族'});db.stu1.find({'age':{\$lt:20},'sex':'男'});

db.stu2.remove({});

"

#********* End *********#

第5关:综合实训——学生信息管理

(先复制代码文件,再来按顺序输命令行)

命令行(分步骤顺序运,不然会报错)

1.   mongo --port 27017
2.   use Testdb5
3.   document=([{_id:1,name:"王晓丽",age:19,sex:"女",major:"计算机"},  {_id:2,name:"张明",age:21,sex:"男",major:"计算机"},
{_id:3,name:"秋雅",age:20,sex:"女",major:"播音主持"},
{_id:4,name:"张欣欣",age:18,sex:"女",major:"计算机"},
{_id:5,name:"袁华",age:20,sex:"男",major:"播音主持"},
{_id:6,name:"马冬梅",age:20,sex:"女",major:"表演"},
{_id:7,name:"夏洛",age:22,sex:"男",major:"表演"},
{_id:8,name:"李大春",age:23,sex:"男",major:"表演"}]);

4.   db.student_t.insert(document);
 

代码文件

#********* Begin *********#

echo "

db.student_t.save({'_id':4,'name':'张欣欣','age':18,'sex':'女','major':'播音主持'});db.student_t.find({'age':{\$gte:20}});db.student_t.find({'sex':'男'});db.student_t.find({'major':'播音主持'});db.student_t.find({'major':'表演','sex':'女'});db.student_t.find({'_id':5});db.student_t.find({'name':'王晓丽'});

"

#********* End *********#

你可能感兴趣的:(数据库)