`

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数据库的基本操作

    然后是“查”操作,查询数据是MongoDB中最常用的功能。`find()`方法用于查找匹配条件的文档。比如,找出所有用户: ```javascript db.users.find() ``` 如果需要根据特定条件查询,可以传递一个查询对象,如查找...

    MongoDB数据库管理工具

    4. CRUD操作:创建(Create)、读取(Read)、更新(Update)、删除(Delete)是数据库操作的基础,MongoDB提供了丰富的命令来支持这些操作。 5. 查询语言:MongoDB使用MQL(MongoDB Query Language)进行查询,它...

    MongoDB常用数据库命令大全

    本文将详细讲解MongoDB的一些常用数据库命令,包括数据库操作、集合操作、用户管理和查询操作。 1. **数据库操作** - **查看命令提示**:`help` 和 `db.help()` 可用于获取数据库层面的帮助信息,而`db.yourColl....

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

    以下是对MongoDB数据库和集合常用操作的详细说明: 1. **创建数据库** - 在MongoDB中,创建数据库非常简单,只需使用`use`命令指定数据库名称。例如,`use myDatabase`会切换到名为`myDatabase`的数据库。如果该...

    mongodb 数据库基本操作 使用MongoDB Python操作NoSQL数据库

    Python是与MongoDB交互的常用编程语言之一,提供了PyMongo库,使得在Python环境中操作MongoDB变得简单易行。在这个话题中,我们将探讨如何使用Python进行MongoDB的基本操作,包括连接数据库、创建集合、插入文档、...

    MongoDB数据库常用命令.docx

    MongoDB 数据库常用命令大全 MongoDB 是 NoSQL 数据库系统中比较流行的数据库之一。它也是最接近关系型数据库的,一个数据库可以包含多个集合(Collection),类似于关系数据库中的表;而每个集合中可以存储一组由...

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

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

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

    本文将深入探讨在MongoDB中执行的一些常用操作命令,这对于理解和操作MongoDB数据库至关重要。 1. **查看当前连接的数据库** 使用 `db.getMongo()` 命令可以获取当前MongoDB实例的连接信息,包括服务器地址和端口...

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

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

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

    本文将深入探讨在MongoDB中执行的一些常用操作命令,这些命令对于管理和维护MongoDB数据库至关重要。 1. **创建用户和权限管理** - `db.addUser("name")`: 这个命令用于在当前数据库中创建一个新的用户。"name"是...

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

    本文将深入探讨在MongoDB中常见的数据库操作命令,这些命令对于理解和操作MongoDB至关重要。 首先,让我们来看一下如何根据年龄对数据进行排序。在MongoDB中,可以使用`sort()`函数来对查询结果进行排序。例如,...

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

    以下是对MongoDB数据库常用操作命令的详细解释: 1. **读取当前集合的所有index信息** 使用`db.collection.reIndex()`命令可以重新构建集合的所有索引。这并非获取索引信息,但可以确保索引是最新的。要查看当前...

    mongodb 数据库基本操作

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

    mongodb 数据库基本操作.docx

    最常用的方法是通过MongoDB Shell,这是一个命令行工具,可以用来执行各种数据库操作。连接MongoDB的基本命令如下: ```bash mongo --host localhost --port 27017 ``` 这里指定了MongoDB服务运行的主机地址`...

    ### MongoDB 数据库基本操作

    ### MongoDB 数据库基本操作 **MongoDB** 是一种非关系型数据库管理...以上介绍的是 MongoDB 的一些常用基本操作,它们对于管理 MongoDB 数据库非常重要。熟练掌握这些命令有助于高效地管理和操作 MongoDB 数据库。

    MongoDB常用SQL操作

    以下是一些关于MongoDB常用SQL操作的关键知识点: 1. **数据模型**:MongoDB基于JSON(JavaScript Object Notation)格式的文档存储数据,这使得它能够存储复杂的数据结构,如嵌套对象和数组。 2. **连接MongoDB**...

    MongoDB数据库介绍及安装

    - 数据库操作:在shell中,可以使用`use &lt;database&gt;`切换数据库,`db`命令用于访问当前数据库,`db.collection.find()`执行查询。 4. **03-学会find.ppt**可能涵盖了以下内容: - `find`操作:这是MongoDB中最...

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

    以下将详细介绍MongoDB的一些常用操作命令。 1. **查询所有记录**: 在MongoDB中,使用`db.collection.find()`方法可以查询指定集合(如`userInfo`)的所有记录。这与SQL中的`SELECT * FROM userInfo`类似。默认...

    MongoDB常用的操作命令.txt

    ### MongoDB常用操作命令详解 #### 一、启动与配置MongoDB...以上就是关于MongoDB常用操作命令的详细介绍,涵盖了启动配置、基本数据库操作及数据操作等方面的内容。希望这些信息能帮助您更好地理解和使用MongoDB。

Global site tag (gtag.js) - Google Analytics