`
desert3
  • 浏览: 2139715 次
  • 性别: Icon_minigender_1
  • 来自: 合肥
社区版块
存档分类
最新评论

MongoDB常用数据库操作

阅读更多
function find(query, fields, limit, skip)
1,多个where条件
// i.e., select * from things where x=3 and y="foo"
db.things.find( { x : 3, y : "foo" } );

db.things.find({j: {$ne: 3}, k: {$gt: 10} });

2,查询部分字段
// select z from things where x="john"(查询字段z)
db.things.find( { x : "john" }, { z : 1 } );

// get all posts about 'tennis' but without the comments field(查询除了comments以外的所有字段)
db.posts.find( { tags : 'tennis' }, { comments : 0 } );

默认查询出来的结果包含_id字段,如果不想要,你可以排除他
db.things.find( { x : "john" }, { z : 1 , _id : 0} );

// 查询子文档的部分字段
t.find({})
"_id" : ObjectId("4c23f0486dad1c3a68457d20"), "x" : { "y" : 1, "z" : [ 1, 2, 3 ] } }
t.find({}, {'x.y':1})
"_id" : ObjectId("4c23f0486dad1c3a68457d20"), "x" : { "y" : 1 } }

// 数组截取
db.posts.find({}, {comments:{$slice: 5}}) // first 5 comments
db.posts.find({}, {comments:{$slice: -5}}) // last 5 comments
db.posts.find({}, {comments:{$slice: [20, 10]}}) // skip 20, limit 10
db.posts.find({}, {comments:{$slice: [-20, 10]}}) // 20 from end, limit 10

3,比较操作符($gt,$lt,$gte,$lte,$ne)
db.collection.find({ "field" : { $gt: value } } );   // greater than  : field > value
db.collection.find({ "field" : { $lt: value } } );   // less than  :  field < value
db.collection.find({ "field" : { $gte: value } } );  // greater than or equal to : field >= value
db.collection.find({ "field" : { $lte: value } } );  // less than or equal to : field <= value
db.things.find( { x : { $ne : 3 } } );               // not equals : field != value

db.collection.find({ "field" : { $gt: value1, $lt: value2 } } );    // value1 < field < value

4,必须包含$all
数组可以比$all操作符对应的数据含有更加多的元素,$all表示满足条件元素的最小集合
{ a: [ 1, 2, 3 ] }
db.things.find( { a: { $all: [ 2, 3 ] } } ); // match
db.things.find( { a: { $all: [ 2, 3, 4 ] } } ); // not match

5,是否存在$exists
db.things.find( { a : { $exists : true } } ); // return object if a is present
db.things.find( { a : { $exists : false } } ); // return if a is missing

6,取模运算符$mod
db.things.find( "this.a % 10 == 1");
db.things.find( { a : { $mod : [ 10 , 1 ] } } ) ; //better

7,IN,NOTIN运算符$in,$nin
db.collection.find( { "field" : { $in : array } } );
db.things.find({j:{$in: [2,4,6]}});

db.things.find({j:{$nin: [2,4,6]}});

8,OR,NOT OR, NOT运算符$or,$nor,$not
db.foo.find( { $or : [ { a : 1 } , { b : 2 } ] } )
db.foo.find( { name : "bob" , $or : [ { a : 1 } , { b : 2 } ] } )

db.customers.find( { name : { $not : /acme.*corp/i } } );
db.things.find( { a : { $not : { $mod : [ 10 , 1 ] } } } );

9,数组元素个数运算符$size
$size不支持范围操作,只能是size = 操作
db.things.find( { a : { $size: 1 } } );

10,BSON元素类型判断运算符$type
db.things.find( { a : { $type : 2 } } ); // matches if a is a string
db.things.find( { a : { $type : 16 } } ); // matches if a is an int

Type Name 	Type Number
Double 	1
String 	2
Object 	3
Array 	4
Binary data 	5
Object id 	7
Boolean 	8
Date 	9
Null 	10
Regular expression 	11
JavaScript code 	13
Symbol 	14
JavaScript code with scope 	15
32-bit integer 	16
Timestamp 	17
64-bit integer 	18
Min key 	255
Max key 	127 

11,正则表达式
db.customers.find( { name : /acme.*corp/i } );

12,值是否在数组,值是否在嵌套对象
db.things.find( { colors : "red" } );  // To look for the value "red" in an array field colors:
db.postings.find( { "author.name" : "joe" } ); // For example, to look author.name=="joe" in a postings collection with embedded author objects:

13,遍历数组$elemMatch
Use $elemMatch to check if an element in an array matches the specified match expression.
t.find( { x : { $elemMatch : { a : 1, b : { $gt : 1 } } } } )

{ "_id" : ObjectId("4b5783300334000000000aa9"),
  "x" : [ { "a" : 1, "b" : 3 }, 7, { "b" : 99 }, { "a" : 11 } ]
}

t.find( { "x.a" : 1, "x.b" : { $gt : 1 } } )

14,JavaScript表达式和$where
下面的4个表达式等价!!JavaScript表达式的执行效率比使用上述操作符的执行时间差些,不过使用起来更加灵活些
db.myCollection.find( { a : { $gt: 3 } } );
db.myCollection.find( { $where: "this.a > 3" } );
db.myCollection.find("this.a > 3");
f = function() { return this.a > 3; } db.myCollection.find(f);

15,count函数
对查询结果计数,在MongoDB Server端计数比MongoDB Client端更加快速和有效
nstudents = db.students.find({'address.state' : 'CA'}).count();
nstudents = db.students.find({'address.state' : 'CA'}).toArray().length; // VERY BAD: slow and uses excess memory

count方法默认忽略count前面使用的skip,limit函数,可以使用count(true)来使得skip,limit起作用
n = db.students.find().skip(20).limit(10).count(true);

16,limit函数
db.students.find().limit(10).forEach( function(student) { print(student.name + "<p>"); } );

17,skip函数
function printStudents(pageNumber, nPerPage) {
   print("Page: " + pageNumber);
   db.students.find().skip((pageNumber-1)*nPerPage).limit(nPerPage).forEach( function(student) { print(student.name + "<p>"); } );
}

18,sort函数
db.myCollection.find().sort( { ts : -1 } ); // sort by ts, descending order

19,min,max函数(查询条件)
db.f.find().min({name:"barry"}}.max({name:"larry"}).hint({name:1}); // 包含小的,不包含大的
db.f.find().min({name:"barry"}}.max({name:"larry"});
db.f.find().min({last_name:"smith",first_name:"john"}};

db.f.find({$min: {name:"barry"}, $max: {name:"larry"}, $query:{}});
分享到:
评论

相关推荐

    MongoDB数据库常用操作命令4.pdf

    MongoDB数据库常用操作命令4.pdf 学习资料 复习资料 教学资源

    MongoDB数据库常用操作命令5.pdf

    MongoDB数据库常用操作命令5.pdf 学习资料 复习资料 教学资源

    MongoDB数据库常用操作命令7.pdf

    MongoDB数据库常用操作命令7.pdf 学习资料 复习资料 教学资源

    MongoDB数据库常用操作命令8.pdf

    MongoDB数据库常用操作命令8.pdf 学习资料 复习资料 教学资源

    MongoDB数据库常用操作命令3.pdf

    MongoDB数据库常用操作命令3.pdf 学习资料 复习资料 教学资源

    MongoDB数据库常用操作命令11.pdf

    MongoDB数据库常用操作命令11.pdf 学习资料 复习资料 教学资源

    MongoDB数据库常用操作命令1.pdf

    MongoDB数据库常用操作命令1.pdf 学习资料 复习资料 教学资源

    大数据实验报告,1-8合集 熟悉常用的HBase操作 熟悉常用的mongoDB数据库操作等等

    大数据实验报告,1-8合集 熟悉常用的HBase操作 熟悉常用的mongoDB数据库操作等等 大数据实验报告(实验一到八) 实验一: 熟悉常用的Linux操作和Hadoop操作 实验二: 熟悉常用的HDFS操作 实验三: 熟悉常用的HBase...

    【MongoDB】数据库的基本操作01

    目录 1.1基础入门 1.1.1应用场景 1.1.2环境搭建 1.1.3MySQL和MongoDB ... Mongodb是一个内存数据库,数据都存放再内存中 非关系型数据库是一种文档型的数据库,即可以存放xml、json、bson类型的数据,数据结

    mongodb 数据库基本操作

    附件是mongodb 数据库基本操作,包含最常用的 15 条命令,非常适合MongoDB入门级学习使用,文件绿色安全,仅供学习交流使用,无任何商业目的,欢迎大家下载使用!

    MongoDB常用数据库命令大全

    一、MongoDB 数据库常用操作命令 1、Help查看命令提示 help db.help(); db.yourColl.help(); 2、切换/创建数据库 use raykaeso; 当创建一个集合(table)的时候会自动创建当前数据库 3、查询所有数据库 show dbs; 4...

    mongodb 数据库常用命令大全手册

    附件是mongodb 数据库基本操作,包含最常用的 15 条命令,非常适合MongoDB入门级学习使用,文件绿色安全,仅供学习交流使用,无任何商业目的,欢迎大家下载使用!

    MongoDB常用操作命令大全

    MongoDB常用操作命令大全 数据库常用命令 Collection聚集集合 用户相关 聚集集合查询

    MongoDB数据库基础操作与常用命令详2.zip

    MongoDB作为一个灵活且强大的NoSQL数据库,为开发者提供了丰富的功能和操作选项。通过本文的介绍,相信读者已经对MongoDB的基本操作和常用命令有了较为全面的了解。在实际应用中,建议结合具体需求和数据特点,合理...

    mongodb 数据库基本操作.zip

    mongodb 数据库基本操作,聚合框架用于处理数据集合,返回计算后的结果。常用的聚合管道操作包括: $match:过滤数据 $group:分组 $sort:排序 $project:投影,类似于SQL里的SELECT,可以用来选择特定的字段 � �...

    实验四:NoSQL和关系数据库的操作比较

    (2)熟练使用4种数据库操作常用的 Shell命令。 (3)熟悉4种数据库操作常用的Java API。 A.4.2实验平台 (1)操作系统:Linux(建议Ubuntu 16.04)。(2)Hadoop版本:2.7.1。 (3)MySQL版本:5.7.15。(4)HBase版本:1.1.2。...

    MongoDB常用语法/Mongodb的基本使用

    MongoDB的基本常用语法:1、创建、查询数据库 创建数据库 查询数据库 2、创建集合、查看 创建集合插入数据 查看集合 查看集合中的数据 3、删除集合、删除数据库 删除集合 删除数据库 4、操作符 4、增、删、改、查 1...

    MongoDB常用命令手册.zip

    附件是mongodb 数据库基本操作,包含最常用的 15 条命令,非常适合MongoDB入门级学习使用,文件绿色安全,仅供学习交流使用,无任何商业目的,欢迎大家下载使用!

    MongoDB常用命令汇总

    包含对数据库、集合、文档的常用操作。

    MongoDB常用操作汇总

    查看当前数据库中所有的集合,使用命令 show collections 或使用 show tables 创建集合有两种方式,显示创建和隐式创建 显示创建可以使用命令 db.createCollection(“集合名称") 隐式创建可以使用命令 db.集合...

Global site tag (gtag.js) - Google Analytics