准备数据的脚本
本文参考http://www.jb51.net/article/48217.htm,在此基础上整理成自己的一套mongodb练习文档
db.users.drop();
var user1 = {
"name" : "xiaobu",
"country" : "china",
"comments" :
[
{
"userId" : "001",
"content" : "评论1",
"commentTime" : "2018-04-22 22:50:48.39"
},
{
"userId" : "002",
"content" : "评论2",
"commentTime" : "2018-04-22 22:51:48.39"
},
{
"userId" : "003",
"content" : "评论3",
"commentTime" : "2018-04-22 22:52:48.39"
},
{
"userId" : "004",
"content" : "评论4",
"commentTime" : "2018-04-22 22:53:48.39"
},
{
"userId" : "005",
"content" : "评论5",
"commentTime" : "2018-04-22 22:54:48.39"
},
{
"userId" : "006",
"content" : "评论6",
"commentTime" : "2018-04-22 22:55:48.39"
},
{
"userId" : "007",
"content" : "评论7",
"commentTime" : "2018-04-22 22:56:48.39"
},
{
"userId" : "008",
"content" : "评论8",
"commentTime" : "2018-04-22 22:57:48.39"
},
{
"userId" : "009",
"content" : "评论9",
"commentTime" : "2018-04-22 22:58:48.39"
}
],
"address" : {
"aCode" : "001",
"aName" : "长沙"
},
"favorites" : {
"books" : [
"西游记",
"红楼梦",
"三国演义",
"水浒传"
],
"cites" : [
"韶关",
"深圳",
"佛山"
],
"fruits" : [
"apple",
"banana",
"watermelon"
]
},
"age" : 26.0,
"salary" : NumberDecimal("18889.09"),
"height" : 1.7
};
var user2 = {
"name" : "juyi",
"country" : "china",
"comments" : [
{
"userId" : "101",
"content" : "评论101",
"commentTime" : "2018-04-22 22:50:48.39"
},
{
"userId" : "102",
"content" : "评论102",
"commentTime" : "2018-04-22 22:51:48.39"
}
],
"address" : {
"aCode" : "002",
"aName" : "韶关"
},
"favorites" : {
"movies" : [
"肖生克的救赎",
"阿甘正传",
"头号玩家"
],
"cites" : [
"衡阳",
"南宁",
"上海",
"深圳"
],
"fruits" : [
"durian",
"watermelon"
]
},
"age" : 25.0,
"salary" : NumberDecimal("18889.09"),
"height" : 1.5
};
var user3 = {
"name" : "tom",
"country" : "USA",
"comments":{
"$ref" : "comments",
"$id" : ObjectId("5ae20a927aec117179c68468"),
"$db" : "test"
},
"address" : {
"aCode" : "003",
"aName" : "纽约"
},
"favorites" : {
"movies" : [
"头号玩家",
"肖生克的救赎",
"阿甘正传"
],
"cites" : [
"旧金山",
"纽约"
],
"fruits" : [
"durian",
"watermelon"
]
},
"age" : 18.0,
"height" : 1.88
};
var user4 = {
"name" : "tom",
"country" : "USA",
"address" : {
"aCode" : "001",
"aName" : "纽约"
},
"favorites" : {
"movies" : [
"头号玩家",
"肖生克的救赎",
"阿甘正传"
],
"cites" : [
"旧金山",
"纽约"
],
"fruits" : [
"durian",
"watermelon"
]
},
"age" : 18.0,
"height" : 1.88
};
var user5 = {
"name" : "tony",
"country" : "USA",
"address" : {
"aCode" : "004",
"aName" : "洛杉矶"
},
"favorites" : {
"books" : [
"权利的游戏",
"飘",
"谁动了我的奶酪"
],
"cites" : [
"芝加哥",
"洛杉矶"
],
"fruits" : [
"durian",
"watermelon"
]
},
"age" : 28.0,
"height" : 1.79
};
var user6 = {
"name" : "xiaoxiaobu",
"country" : "USA",
"address" : {
"aCode" : "005",
"aName" : "费城"
},
"favorites" : {
"books" : [
"权利的游戏",
"教父"
],
"cites" : [
"芝加哥",
"芝加哥",
"洛杉矶"
],
"fruits" : [
"durian",
"watermelon"
]
},
"age" : 20.0,
"height" : 1.71
};
var user7 = {
"name" : "lily",
"country" : "USA",
"address" : {
"aCode" : "001",
"aName" : "费城"
},
"favorites" : {
"books" : [
"权利的游戏",
"飘",
"谁动了我的奶酪"
],
"cites" : [
"芝加哥",
"芝加哥",
"洛杉矶"
],
"fruits" : [
"durian",
"watermelon"
]
},
"age" : 20.0,
"height" : 1.71
};
var user8 = {
"name" : "xiaoxiao",
"country" : "USA",
"address" : {
"aCode" : "001",
"aName" : "费城"
},
"favorites" : {
"books" : [
"权利的游戏",
"飘",
"谁动了我的奶酪"
],
"cites" : [
"芝加哥",
"芝加哥",
"洛杉矶"
],
"fruits" : [
"durian",
"watermelon"
]
},
"age" : 21.0,
"height" : 1.71
};
db.users.insert(user1);
db.users.insert(user2);
db.users.insert(user3);
db.users.insert(user4);
db.users.insert(user5);
db.users.insert(user6);
db.users.insert(user7);
db.users.insert(user8);
内容评论表
var comment1 = {
"_id" : ObjectId("5ae20a927aec117179c68468"),
"lists":
[
{
"userId" : "tom1",
"content" : "tom评论1",
"commentTime" : "2018-04-22 22:50:48.39"
},
{
"userId" : "tom2",
"content" : "tom评论2",
"commentTime" : "2018-04-22 22:51:48.39"
},
{
"userId" : "tom3",
"content" : "tom评论3",
"commentTime" : "2018-04-22 22:52:48.39"
}
]
};
var comment2 = {
"_id" : ObjectId("5ae20a947aec117179c68469"),
"lists":
[
{
"userId" : "tony1",
"content" : "tony评论1",
"commentTime" : "2018-04-22 22:50:48.39"
},
{
"userId" : "tony2",
"content" : "tony评论2",
"commentTime" : "2018-04-22 22:51:48.39"
},
{
"userId" : "tony3",
"content" : "tony评论3",
"commentTime" : "2018-04-22 22:52:48.39"
}
]
};
db.comments.drop();
db.comments.insert(comment1);
db.comments.insert(comment2);
二、查询练习(pretty只是为了输出好看点)
1、查询所有记录
db.users.find().pretty();
相当于:select * from users;
2、查询去掉后的当前聚集集合中的某列的重复数据
db.users.distinct(“name”);
相当于:select distinct name from users;
3、查询age = 25的记录
db.users.find({“age”:25}).pretty();
相当于:select * from users where age = 25;
4、查询age > 25的记录(大于等于使用$gte)
db.users.find({“age”:{$gt:25}}).pretty();
相当于:select * from users where age > 25;
5、查询age < 25的记录(小于等于使用$lte)
db.users.find({“age”:{$lt:20}}).pretty();
相当于:select * from users where age < 25;
6、查询age >= 25 并且 age <= 26
db.users.find({“age”:{ gte:25, g t e : 25 , lte:26}}).pretty();
相当于:select * from users where age >= 25 and age <= 26;
7、查询name中包含”bu”的数据
db.users.find({“name”:/bu/}).pretty();
相当于:select * from users where name like “%bu%”;
8、查询name中以”ju”开头的
db.users.find({“name”:/^ju/}).pretty();
相当于:select * from users where name like “ju%”;
9、查询指定列name、age数据(只显示name、age的数据)
db.users.find({},{“name”:1,”age”:1}).pretty();
相当于:select name,age from users;
10、查询除了comments以外的数据(只不显示comments的数据)
db.users.find({},{“comments”:0}).pretty();
相当于:select 其它字段 from users;
11、查询指定列name、age数据, age > 25
db.users.find({“age”:{$gt:25}},{“age”:1,”name”:1}).pretty();
相当于:select age,name from users where age > 25;
12、按照年龄排序
升序:db.users.find().sort({“age”:1}).pretty();
相当于:select * from users order by age;
降序:db.users.find().sort({“age”:-1}).pretty();
相当于:select * from users order by age desc;
13、查询前三条数据
db.users.find().limit(3).pretty();
相当于:select * from users limit 3;
14、查询3条以后的数据
db.users.find().skip(3).pretty();
相当于:select * from users where id not in (select id from users limit 3);
15、查询在4-6之间的数据
db.users.find().skip(3).limit(3).pretty();
相当于:select * from users limit 3,3;
16、查询age=25或者age=26的数据
db.users.find({$or:[{“age”:25},{“age”:26}]}).pretty();
相当于:select * from users where age = 25 or age = 26;
17、查询age=25和age=26的数量
db.users.find({$or:[{“age”:25},{“age”:26}]}).count();
相当于:select count(*) from users where age = 25 or age = 26;
18、查询存在评论的数据
db.users.find({“comments”:{$exists:true}}).pretty();
相当于:select * from users where comments is not null;
19、查询favorites.movies中,有”头号玩家”和”肖生克的救赎”的数据
db.users.find({“favorites.movies”:{$all:[“肖生克的救赎”,”头号玩家”]}}).pretty();
相当于:无;
20、查询favorites.movies中,有”头号玩家”,”肖生克的救赎”,”阿甘正传”的数据,且要按该顺序排列的
db.users.find({“favorites.movies”:[“头号玩家”,”肖生克的救赎”,”阿甘正传”]}).pretty();
相当于:无;
21、查询name存在”bu”字符,并且age=25或者26
db.users.find({$and:[{name:{$regex:”.bu.“}},{$or:[{age:25},{age:26}]}]}).pretty();
相当于:无;
22、查询所有的age(数据不重复)
db.users.distinct(“age”);
相当于:select distinct age from users;
22、查询所有name=”xiaobu”的comments前三条数据
db.users.find({“name”:”xiaobu”},{“comments”:{$slice:[0,3]}}).pretty();
相当于:无;
23、查询所有name=”xiaobu”,且comments只显示4-6的评论,且只显示comments和id字段,其它信息不显示
方法一:db.users.find({“name”:”xiaobu”},{“comments”:{$slice:[3,3]},”$elemMatch”:”“}).pretty();
方法二:db.users.find({“name”:”xiaobu”},{“comments”:{“$slice”:[3,3]},”$id”:1}).pretty();(推荐)
相当于:无;
24、精确查询(必须comments的所有字段都匹配才能查询数据来,实际情况基本用不到)
db.users.find({“comments”:{“userId”:”101”,”content”:”评论101”,”commentTime” : “2018-04-22 22:50:48.39”}}).pretty();
相当于:无;
25、查询被userId=101或者userId=001评论过的数据
db.users.find({“comments.userId”:{$in:[“001”,”101”]}}).pretty();
相当于:无;
26、查询userId=101,评语为”评论1”的user
db.users.find({“comments”:{$elemMatch:{“userId”:”101”,”content”:”评论101”}}}).pretty();
相当于:无;
27、查询”_id”等于ObjectId(“5ae20b5e7aec117179c6846c”)的用户评论(dbref查询)
var tom = db.users.findOne({“_id”:ObjectId(“5ae20b5e7aec117179c6846c”)});
var dbref = tom.comments;
db[dbref.$ref].findOne({“_id”:dbref.$id});
相当于子查询