本文参考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);
db.users.find().pretty();
相当于:select * from users;
db.users.distinct(“name”);
相当于:select distinct name from users;
db.users.find({“age”:25}).pretty();
相当于:select * from users where age = 25;
db.users.find({“age”:{$gt:25}}).pretty();
相当于:select * from users where age > 25;
db.users.find({“age”:{$lt:20}}).pretty();
相当于:select * from users where age < 25;
db.users.find({“age”:{ gte:25, g t e : 25 , lte:26}}).pretty();
相当于:select * from users where age >= 25 and age <= 26;
db.users.find({“name”:/bu/}).pretty();
相当于:select * from users where name like “%bu%”;
db.users.find({“name”:/^ju/}).pretty();
相当于:select * from users where name like “ju%”;
db.users.find({},{“name”:1,”age”:1}).pretty();
相当于:select name,age from users;
db.users.find({},{“comments”:0}).pretty();
相当于:select 其它字段 from users;
db.users.find({“age”:{$gt:25}},{“age”:1,”name”:1}).pretty();
相当于:select age,name from users where age > 25;
升序: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;
db.users.find().limit(3).pretty();
相当于:select * from users limit 3;
db.users.find().skip(3).pretty();
相当于:select * from users where id not in (select id from users limit 3);
db.users.find().skip(3).limit(3).pretty();
相当于:select * from users limit 3,3;
db.users.find({$or:[{“age”:25},{“age”:26}]}).pretty();
相当于:select * from users where age = 25 or age = 26;
db.users.find({$or:[{“age”:25},{“age”:26}]}).count();
相当于:select count(*) from users where age = 25 or age = 26;
db.users.find({“comments”:{$exists:true}}).pretty();
相当于:select * from users where comments is not null;
db.users.find({“favorites.movies”:{$all:[“肖生克的救赎”,”头号玩家”]}}).pretty();
相当于:无;
db.users.find({“favorites.movies”:[“头号玩家”,”肖生克的救赎”,”阿甘正传”]}).pretty();
相当于:无;
db.users.find({$and:[{name:{$regex:”.bu.“}},{$or:[{age:25},{age:26}]}]}).pretty();
相当于:无;
db.users.distinct(“age”);
相当于:select distinct age from users;
db.users.find({“name”:”xiaobu”},{“comments”:{$slice:[0,3]}}).pretty();
相当于:无;
方法一:db.users.find({“name”:”xiaobu”},{“comments”:{$slice:[3,3]},”$elemMatch”:”“}).pretty();
方法二:db.users.find({“name”:”xiaobu”},{“comments”:{“$slice”:[3,3]},”$id”:1}).pretty();(推荐)
相当于:无;
db.users.find({“comments”:{“userId”:”101”,”content”:”评论101”,”commentTime” : “2018-04-22 22:50:48.39”}}).pretty();
相当于:无;
db.users.find({“comments.userId”:{$in:[“001”,”101”]}}).pretty();
相当于:无;
db.users.find({“comments”:{$elemMatch:{“userId”:”101”,”content”:”评论101”}}}).pretty();
相当于:无;
var tom = db.users.findOne({“_id”:ObjectId(“5ae20b5e7aec117179c6846c”)});
var dbref = tom.comments;
db[dbref.$ref].findOne({“_id”:dbref.$id});
相当于子查询