首先推荐一个Studio 3T 的操作工具
1:支持批量插入
var persons = [{ name:"jim", age:25, email:"75431457@qq.com", c:89,m:96,e:87, country:"USA", books:["JS","C++","EXTJS","MONGODB"] }, { name:"tom", age:25, email:"214557457@qq.com", c:75,m:66,e:97, country:"USA", books:["PHP","JAVA","EXTJS","C++"] }, { name:"lili", age:26, email:"344521457@qq.com", c:75,m:63,e:97, country:"USA", books:["JS","JAVA","C#","MONGODB"] }, { name:"zhangsan", age:27, email:"2145567457@qq.com", c:89,m:86,e:67, country:"China", books:["JS","JAVA","EXTJS","MONGODB"] }, { name:"lisi", age:26, email:"274521457@qq.com", c:53,m:96,e:83, country:"China", books:["JS","C#","PHP","MONGODB"] }, { name:"wangwu", age:27, email:"65621457@qq.com", c:45,m:65,e:99, country:"China", books:["JS","JAVA","C++","MONGODB"] }, { name:"zhaoliu", age:27, email:"214521457@qq.com", c:99,m:96,e:97, country:"China", books:["JS","JAVA","EXTJS","PHP"] }, { name:"piaoyingjun", age:26, email:"piaoyingjun@uspcat.com", c:39,m:54,e:53, country:"Korea", books:["JS","C#","EXTJS","MONGODB"] }, { name:"lizhenxian", age:27, email:"lizhenxian@uspcat.com", c:35,m:56,e:47, country:"Korea", books:["JS","JAVA","EXTJS","MONGODB"] }, { name:"lixiaoli", age:21, email:"lixiaoli@uspcat.com", c:36,m:86,e:32, country:"Korea", books:["JS","JAVA","PHP","MONGODB"] }, { name:"zhangsuying", age:22, email:"zhangsuying@uspcat.com", c:45,m:63,e:77, country:"Korea", books:["JS","JAVA","C#","MONGODB"] }] for(var i = 0;i<persons.length;i++){ db.persons.insert(persons[i]) } var persons = db.persons.find({name:"jim"}) while(persons.hasNext()){ obj = persons.next(); print(obj.books.length) }
2:查询
find:db.集合名.find({条件},{键指定}) 1:只查找部分字段 1.1 查询出所有数据的指定键(name ,age ,country) db.persons.find({},{name:1,age:1,country:1,_id:0}); 2.查询条件 $gte $gt $lte $lt $ne 2.1查询出年龄在25到27岁之间的学生 db.persons.find({age: {$gte:25,$lte:27},{_id:0,age:1}) 2.2查询出所有不是韩国籍的学生的数学成绩 db.persons.find({country:{$ne:” Korea”}},{_id:0,m:1}) 3.包含或不包含 $in或$nin 2.3查询国籍是中国或美国的学生信息 db.persons.find({country:{$in:[“USA”,“China”]}}) 2.4查询国籍不是中国或美国的学生信息 db.persons.find({country:{$nin:[“USA”,“China”]}}) 4.OR查询 $or 2.4查询语文成绩大于85或者英语大于90的学生信息 db.persons.find({$or:[{c:{$gte:85}},{e:{$gte:90}}]},{_id:0,c:1,e:1}) 5.Null 把中国国籍的学生上增加新的键sex db.person.update({country:”China”},{$set:{sex:”m”}}) 2.5查询出sex 等于 null的学生 db.persons.find({sex:{$in:[null]}},{country:1}) 或者db.persons.find({sex:{$eq:null}}) 6.正则查询 2.6查询出名字中存在”li”的学生的信息 db.persons.find({name:/li/i},{_id:0,name:1}) 7.$not的使用 $not可以用到任何地方进行取反操作 2.7查询出名字中不存在”li”的学生的信息 db.persons.find({name:{$not:/li/i}},{_id:0,name:1}) $not和$nin的区别是$not可以用在任何地方儿$nin是用到集合上的 8.数组查询$all和index应用 2.8查询喜欢看MONGOD和JS的学生 db.persons.find({books:{$all:[“MONGOBD”,”JS”]}},{books:1,_id:0}) 2.8.2查询第二本书是JAVA的学习信息 db.persons.find({“books.1”:”JAVA”}) 9.查询指定长度数组$size它不能与比较查询符一起使用(这是弊端) 2.9查询出喜欢的书籍数量小于4本的学生 db.persons.find({$where:"this.books.length《4"}) 2.9.2查询出lili喜欢看的书的数量 var lili = db.persons.find({name:"lili"}) while(lili.hasNext()){ obj=lili.next() print(obj.books.length) } 10.$slice操作符返回文档中指定数组的内部值 2.10查询出Jim书架中第2~4本书 db.persons.find({name:"jim"},{books:{"$slice":[1,3]}}) 2.10.2查询出最后一本书 db.persons.find({name:"jim"},{books:{"$slice":-1},_id:0,name:1})
3:文档查询
var jim = [{ school :"K", score:"A" },{ school :"L", score:"B" },{ school :"J", score:"A+" }] db.persons.update({name:"jim"},{$set:{school:jim}})
3.文档查询 3.1查询出在K上过学的学生 db.persons.find({"school.school":"K"}) 3.2查询出在K上过学的学生,而且成绩中获得过A的学生 db.persons.find({"school.school":"K","school.score":"A"}) 3.3查询出在K校成绩为A的学生 db.persons.find({school:{$elemMatch:{"school":"K","score":"A"}}}) 4.$where 4.查询年龄大于22岁,喜欢看C#书,在K学校上过学的学生信息 db.persons.find({age:22,books:{$in:["C#"]},"school.school":"K"}) 或者 db.persons.find({"$where":function(){ //得到查询结果的每一条文档 var books = this.books; //得到文档中的school对象 var school = this.school; //如果年纪>=22 if(this.age > 22){ var php = null; //遍历书籍 for ( var i = 0; i < books.length; i++) { if(books[i] == "C++"){ php = books[i]; //如果学校是真 if(school){ for (var j = 0; j < school.length; j++) { //判断是不是在K上学 if(school[j].school == "K"){ //返回是真 return true; } } break; } } } } }}) 5.Limit返回指定的数据条数 5.1查询出persons文档中前5条数据 db.persons.find({},{_id:0,name:1}).limit(5) 6.Skip返回指定数据的跨度 6.1查询出persons文档中5~10条的数据 db.persons.find({},{_id:0,name:1}).limit(5).skip(5) 7.Sort返回按照年龄排序的数据[1,-1] db.persons.find({},{_id:0,name:1,age:1}).sort({age:1}) 注意:mongodb的key可以存不同类型的数据排序就也有优先级 最小值 null 数字 字符串 对象/文档 数组 二进制 对象ID 布尔 日期 时间戳 正则 最大值 8:分页(跳过多少条,去多少条) db.persons.find().limit(5).skip(5).sort({id:1}) 注意:分页的性能不好,建议通过,筛选条件+limit(),获取分页 示例:根据自增的id取数据(每页5条,显示第二页) 方法一:db.persons.find().limit(5).skip(5) 方法二:db.persons.find({id:{$gt:5}}).limit(5) 而且,应该把重点放到便捷和精确查询上而不是分页的性能上 因为用户最多不会翻查过2页的
来源:https://www.cnblogs.com/YK2012/p/10987694.html